import React, { useState, useEffect } from 'react';
import { Input, Menu, Dropdown, Button, Icon, message, Table, Tooltip, Tabs, Radio, Divider, Tag, Select, Form, Alert, Modal } from 'antd';
import request from '../../../utils/request';
import apis from '../../../services/apis';
import CustomerChange from './CustomerChange'
import BuildSelect from '@/components/SelectButton/BuildSelect'

let building = null
const BuildingSelection = (props) => {
    const [visible, setVisible] = useState(false) 
    const { userData } = props;

     //授权项目改变
  const consultantBuildingChange =(e) => {
    building = e
    if(userData.isConsultant && props.value !== e){
      request({ ...apis.staff.check, params: { userId: userData.userId, personId: userData.consultantPersonId, buildingId: userData.buildingId } }).then(res => {
        if(res.length > 0){
          Modal.confirm({
            title: '此置业顾问下有私客，是否转移私客？',
            okText: '确认',
            cancelText: '取消',
            onOk () {
              setVisible(true)
            },
          });
        }else{
            props.onChange(e)
        }
     })
    }else{
        props.onChange(e)
    }
  }

   //迁移私客成功回调
   const moveSuccess = (e) => {
       if(e === 'success'){
        props.onChange(building)
       }
       setVisible(false)
  }

     return (<>
        <BuildSelect value={props.value} onChange={consultantBuildingChange} />
        <Modal
              visible={visible}
              title="分配归属"
              onCancel={() => setVisible(false)}
              footer={null}
              width={900}
            >
                <CustomerChange userId={userData.userId} consultantPersonId={userData.consultantPersonId} buildingId={userData.buildingId} onSuccess={moveSuccess} />
            </Modal>
     </>)
  }
  
  export default BuildingSelection;