import React from 'react';
import {
  Form, Select, Input, Button,Row,Col,message
} from 'antd';
import { $http } from '../../containers/config/https';

const { Option } = Select;

let user;
class DepartmentSettingAdd extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      hospitalData: [], // 医院
       // 医院名称
       hospitalId: undefined,
       locDesc: undefined,
       DepartmentInformation: [], // 科室名称下拉框
       hospitalData: [], // 医院
       selectData:{},
       
    },
    user= window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
    this.myRef1 = React.createRef();
    this.myRef2 = React.createRef();
    this.myRef3 = React.createRef();
  }
  componentDidMount() {
     this.loadHospitalMsg();
     this.getSelectData();
   //  this.getDepartmentInformation();

}

  	// 下拉框选中事件
		handleOnSelect = (num) => {
			switch(num){
				case 1:
					setTimeout(() => {
            this.getDepartmentInformation(this.myRef1.current.props.value);
          }, 300)
         
					break;
				case 2:
					setTimeout(() => {
						this.myRef3.current.focus();
					}, 300)
					break;
 
				}
    }
    onFocusLocDesc = () => {
      if (this.state.hospitalId === undefined) {
        message.info("请先选择医院");
      }
    }
  
    // 查询条件 --- 医院
    onChangehospitalId = (e) => {
      // console.log(e.target.value)
      this.setState({ hospitalId: e, locDesc: undefined }, () => {
        this.getDepartmentInformation(e)
      });
    }
  

	// 查询条件 --- 科室名称
	onChangeLocDesc = (e) => {
		this.setState({ locDesc: e });
  }
  
     // 新增科室岗位关联
     handelSave = () => {
      let data = {
          params: [{
              hospID:this.myRef1.current.props.value,//医院ID
              locID:this.myRef2.current.props.value,//科室id
              postID:this.myRef3.current.props.value,//岗位id
              createUserID: user.userID//创建人
          }]
      }
      $http.post('urlS', this, {
          code: "01050007",
          data: data,
          success: (res) => {
          
              if (+res.errorCode === 0) {
                  message.success('新增成功');
                  this.props.getTableData();
                 
              }
          }
      })
  }
   // 3.13.23.获取科室信息
	getDepartmentInformation(id) {
		if (id) {
			let data = {
				params: [
					{ hospID: id}
				]
			}
			$http.post('urlS', this, {
				code: "04020008",
				data: data,
				success: function (res) {
					if (+res.errorCode === 0) {
            
            this.setState({ DepartmentInformation: res.result.Data },
              ()=>{
                this.myRef2.current.focus();
              })
					} else {
						this.setState({ DepartmentInformation: [] })
					}
				}
			})
		} else {
			this.setState({ DepartmentInformation: [] })
		}
	}
	//获取所属医院
	loadHospitalMsg() {
		var data = {
			params: [{
				hospID: '', // 医院id
				logonGroupID: user.groupID, //登录角色id
				logonHospID: user.hospID, // 登录医院id
			}]
		}
		$http.post('urlS', this, {
			code: "01040001",
			data: data,
			success: function (res) {
				if (+res.errorCode === 0) {
					this.setState({
						hospitalData: res.depDoctor,
						// hospitalId: res.depDoctor ? res.depDoctor[0].id : undefined
					})
					var result = res.depDoctor && res.depDoctor.some(item => {
						if (item.id === user.hospID) {
								return true
						}
					})
					if (!result) {
						this.setState({
								hospitalId: undefined
						})
					} else {
						this.setState({hospitalId: user.hospID ? user.hospID : undefined})
						this.getDepartmentInformation(user.hospID)
					}
				}
			}
		});
	}
	// 岗位下拉框数据
	getSelectData() {
		let data = {
			params: []
		}
		$http.post('urlS', this, {
			code: "01040012",
			data: data,
			success: function (res) { 
				if (+res.errorCode === 0) {
					this.setState({selectData: res})
				} else {
					this.setState({selectData: {}})
				}
			}
		})
	}
  render() {
    const { getFieldDecorator } = this.props.form;
    let userInfo = this.props.userInfo ? this.props.userInfo : {};
    const formItemInput  = {
      labelCol: {span: 5},
      wrapperCol: {span: 19}
    };
    return (
      <div style={{padding:'30px 30px 0',boxSizing:'border-box'}}>
        <Form style={{width:'40%'}} layout="vertical"  onSubmit={this.handleSubmit}>
                <Row gutter={16}>
                    <Form.Item label="医院名称：" {...formItemInput}>
                        {getFieldDecorator('HospDr', {
                          	    initialValue: userInfo.HospDr ? userInfo.HospDr : '',
                                rules: [{ required: true, message: '请选择医院名称' }],
                        })(
                          <Select  showSearch optionFilterProp="title" allowClear ref={this.myRef1} onChange={() => this.handleOnSelect(1)} style={{marginTop:"-10px"}}>
                         	{this.state.hospitalData && this.state.hospitalData.map(item => {
												return <Option key={item.id} value={item.id} title={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>
											})}
                        </Select>
                        )}
                    </Form.Item>
                </Row>
                <Row gutter={16}>
                      <Form.Item label="科室名称"{...formItemInput}>
                        {getFieldDecorator('Descripts', {
                            initialValue: userInfo.Descripts ? userInfo.Descripts : '',
                            rules: [{ required: true, message: '请输入科室名称' }],
                        })(
                          <Select  showSearch optionFilterProp="title" allowClear ref={this.myRef2} onChange={() => this.handleOnSelect(2)} style={{marginTop:"-10px"}}>
                         {this.state.DepartmentInformation && this.state.DepartmentInformation.map(item => {
                                    return <Option key={item.id} value={item.id} title={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>
                                })}
                        </Select>
                        )}
                      </Form.Item>
                </Row>
                <Row gutter={16}>
                  <Form.Item label="岗位：" {...formItemInput}>
                          {getFieldDecorator('PostDr', {
                              initialValue: userInfo.PostDr ? userInfo.PostDr : '',
                              rules: [{ required: true, message: '请选择岗位' }],
                          })(
                              <Select ref={this.myRef3} allowClear onSelect={() => this.handleOnSelect(3)} style={{marginTop:"-10px"}}>
                             	{this.state.selectData.post && this.state.selectData.post.map(item => {
																return <Option value={item.id} key={item.id}>{item.descripts}</Option>
															})}
                              </Select>
                          )}
                    </Form.Item>
                </Row>
            <Form.Item
              style={{height:"40px"}}
              wrapperCol={{ span: 12, offset: 10}}
            >
              <Button type="primary" onClick={this.handelSave} >
                保存
              </Button>
            </Form.Item>
      </Form>
      </div>
    );
  }
}

export default Form.create({ name: 'coordinated' })(DepartmentSettingAdd);
