import React from 'react';
import PropTypes from 'prop-types';
import { $http } from '../../containers/config/https';
import { Util } from '../../tools';
import { Modal, Form, Input, message,DatePicker,Select} from 'antd';
import Moment from 'moment';
const FormItem = Form.Item;
const Option = Select.Option;




const TarTypeModal=Form.create()(

class  extends React.Component{
    state = { 
        visible: false,
        typeList:[] ,
        hospList:[],
    };

    componentDidMount() {
        this.props.onRef(this);
        let sessionInfo = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        this.setState({hospID:sessionInfo.hospID}, () => {
            this.handleTypeQuery();
            this.handleHospQuery();
        });
      }
    componentWillUnmount() {
        this.setState = (state, callback) => {
            return
           }
    }

    


    // 收费项大类类型下拉框
    handleTypeQuery = () => {
        let data = {}
        let {hospID}=this.state ;
        let sessionInfo = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        data = {
            params: [{
            code: "",
            descripts: "",
            hospDr:hospID
            }]
        }
        $http.post('urlS', this, {
            code: '05100049',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0 && res.type) {
                    let tempList = [];
                    for (var i = 0; i < res.type.length; i++) {
                        tempList.push(<Option key={res.type[i].code}>{res.type[i].descripts}</Option>)
                    }
                    this.setState({
                    typeList: tempList
                    })
                } 
                /*else {
                    setTimeout(() => {
                        this.setState({ typeList: [] })
                    }, 1000)
                }*/
            }
        })
    }   
    

    handleHospQuery = () => {
        let data = {}
        let sessionInfo = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        data = {
            params: [{
            hospitalId:"",
            logonGroupID:sessionInfo.groupID,
            logonHospID:sessionInfo.hospID,
            hospRequired:"1",
            }]
        }
        $http.post('urlS', this, {
            code: '01040001',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0 && res.depDoctor) {
                    let tempList =[];
                    for (var i = 0; i < res.depDoctor.length; i++) {
                        tempList.push(<Option key={res.depDoctor[i].id}>{res.depDoctor[i].descripts}</Option>)
                    }
                    this.setState({
                    hospList: tempList
                    })
                } 
            }
        })
    }   
    
    onChangeHospID= (value) => {
        this.setState({hospID:value});
      }
    // 显示弹出层
    showModal = () => {
        this.setState({
            visible: true,
        });
      }
    // 关闭弹框
    handleCancel = () => {
        this.setState({
            visible: false,
        });
    }
    // 重置表单
    resetFields() {
        this.props.form.resetFields(); //清空所有
    }
    // 确定
    handleCreate = () => {
        let { flag } = this.props;
        
        if (flag === 'add') {
          this.handelFunction('05100015', '新增');
        } else if (flag === 'compile') {
            this.handelFunction('05100016', '编辑');
        }  
    }

    // 取消
    handleCancel = () => {
        this.setState({
            visible: false,
        }, () => {
            if (this.props.flag === 'compile') {
                this.props.handelThis.resetUserInfo()
            }
        });
    }

    //增修
    handelFunction(code, msg) {
        const { form: { validateFields } } = this.props;
        validateFields((errors, values) => {
            // 验证成功
            if (errors === null) {
                let data = {
                    params: []
                }
                let obj = values;
                if(obj.startDate){obj.startDate=obj.startDate.format('YYYY-MM-DD');}
                if(obj.stopDate){ obj.stopDate=obj.stopDate.format('YYYY-MM-DD');}
                //获取sessionStorage的数据
                let sessionInfo = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
                obj.createUserID =sessionInfo.userID;
                obj.updateUserID =sessionInfo.userID;
                obj.tarTypeID= this.props.flag === 'add' ? '' : this.props.userInfo.tarTypeID;
                // 处理需要传递的obj
                data.params.push(Util.disposeData(obj));
                $http.post('urlADS', this, {
                    code: code,
                    data: data,
                    success: function (res) { 
                        if (+res.errorCode === 0) {
                          // 提示
                          message.success(msg + '成功');
                          // 新增或编辑成功重置form表单
                          this.resetFields();
                          // 关闭弹框
                          this.handleCancel();
                          // 刷新列表数据
                          this.props.handelThis.handleQuery();
                        }
                    },
                    error:function (error) {
                        message.error(msg + '失败：' + error);
                    }
                })
            }
        });
      }

     
    render() {
      const { getFieldDecorator } = this.props.form;
      let userInfo = this.props.userInfo? this.props.userInfo : {};
      return (
        <Modal
          visible={this.state.visible}
          title={this.props.title + "大类数据"}
          okText="确定"
          onCancel={this.handleCancel}
          onOk={this.handleCreate}
        >
        <Form layout="vertical">
        <FormItem label="医院">
                  {getFieldDecorator('hospID', {
                      initialValue:userInfo.hospID,
                      rules: [{message: '请选择' }]
                  })(
                    <Select
                    allowClear
                    showSearch
                    onChange={this.onChangeHospID}
                    placeholder="请选择"
                    optionFilterProp="children"
                    filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                >{this.state.hospList}</Select>
                  )}
                  </FormItem>
               <FormItem label="类型">
                  {getFieldDecorator('type', {
                      initialValue:userInfo.type ? String(userInfo.type):null,
                      rules: [{ required: true, message: '请选择' }]
                  })(
                    <Select
                    showSearch
                    allowClear
                    placeholder="请输入"
                    optionFilterProp="children"
                    filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                >{this.state.typeList}</Select>
                  )}</FormItem>
              <FormItem label="收费大类代码">
                  {getFieldDecorator('code', {
                      initialValue:userInfo.code,
                      rules: [{ required: true, message: '请选择' }]
                  })(
                      <Input />
                  )}
              </FormItem>
              <FormItem label="收费大类描述">
                  {getFieldDecorator('descripts', {
                      initialValue:userInfo.descripts,
                      rules: [{ required: true, message: '请选择' }]
                  })(
                      <Input />
                  )}
              </FormItem>
             {/*<FormItem label="生效日期">
                  {getFieldDecorator('stDate', {
                      initialValue: userInfo.startDate ? Moment(userInfo.startDate, 'YYYY-MM-DD') : null,
                      rules: [{ required: true, message: '请选择' }],
                      })(<DatePicker style={{width: '100%'}} format="YYYY-MM-DD"  /> )}
                  </FormItem>
              <FormItem label="失效日期">
                  {getFieldDecorator('stopDate', {
                      initialValue: userInfo.stopDate ? Moment(userInfo.stopDate, 'YYYY-MM-DD') : null,
                      rules: [{ required: false, message: '请选择' }],
                      })(<DatePicker style={{width: '100%'}} format="YYYY-MM-DD"  /> )}
                  </FormItem>
                    */}
          </Form>
        </Modal>
      );
    }
  

}





);
export default TarTypeModal; 
TarTypeModal.propTypes = {
    // 验证userInfo的属性
    userInfo: PropTypes.object
  }