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 Option = Select.Option;
const FormItem = Form.Item;




const TarSubTypeModal=Form.create()(

class  extends React.Component{
    /******初始化变量******/
    state = { 
      visible: false ,
      tarTypeList:[],
      hospList:[],
      hospID:''
    };



     /******生命周期方法******/
    componentDidMount() {
        this.props.onRef(this);
        let sessionInfo = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        this.setState({hospID:sessionInfo.hospID}, () => {
            this.handleTarTypeQuery();
            this.handleHospQuery();
        });
        
    }
    componentWillUnmount() {
        this.setState = (state, callback) => {
            return
        }
    }
    


    /******下拉框方法******/
     // 收费项大类下拉框
    handleTarTypeQuery = () => {
        let data = {}
        let {hospID}=this.state

        data = {
            params: [{
            code: "",
            descripts: "",
            hospID:hospID
            }]
        }
        $http.post('urlS', this, {
            code: '05100014',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0 && res.tarType) {
                    let tempList =[];
                    for (var i = 0; i < res.tarType.length; i++) {
                        tempList.push(<Option key={res.tarType[i].tarTypeID}>{res.tarType[i].typeAndTarTypeDesc}</Option>)
                    }
                    this.setState({
                    tarTypeList: tempList
                    })
                } 
            }
        })
    }

        //医院下拉框
        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}, () => {
            this.handleTarTypeQuery();
        });
      }

   
    // 显示弹出层
    showModal = () => {
        this.setState({
            visible: true,
        });
      }
    // 关闭弹框
    handleCancel = () => {
        this.setState({
            visible: false,
        });
      }
    // 确定
    handleCreate = () => {
        let { flag } = this.props;
        
        if (flag === 'add') {
          this.handelFunction('05100019', '新增');
        } else if (flag === 'compile') {
            this.handelFunction('05100020', '编辑');
        }
        
      }
    // 取消
    handleCancel = () => {
        this.setState({
            visible: false,
        }, () => {
            if (this.props.flag === 'compile') {
                this.props.handelThis.resetUserInfo()
            }
        });
    }
    // 重置表单
    resetFields() {
        this.props.form.resetFields(); //清空所有
    }





    //增修
    handelFunction(code, msg) {
        const { form: { validateFields } } = this.props;
        validateFields((errors, values) => {
            // 验证成功
            if (errors === null) {
                let data = {
                    params: []
                }
                let obj = values;
                if(obj.stDate){obj.stDate=obj.stDate.format('YYYY-MM-DD');}
                if(obj.stopDate){ obj.stopDate=obj.stopDate.format('YYYY-MM-DD');}
                ///obj.createUserDr = this.props.flag === 'add' ? 1 : this.props.userInfo.CreateUserName;
                ///obj.updateUserDr = this.props.flag === 'add' ? 1 : this.props.userInfo.CreateUserName;
                //获取sessionStorage的数据
                let sessionInfo = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
                obj.createUserID =sessionInfo.userID;
                obj.updateUserID =sessionInfo.userID;
                //父组件的值
                obj.tarSubTypeID= this.props.flag === 'add' ? '' : this.props.userInfo.tarSubTypeID;
                // 处理需要传递的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 : {}
      let {hospID}=this.state;
      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:hospID,
                      rules: [{message: '请选择' }]
                  })(
                    <Select
                    showSearch
                    allowClear
                    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('tarTypeID', {
                      initialValue:userInfo.tarTypeID ? String(userInfo.tarTypeID):null,
                      rules: [{ required: true, message: '请选择' }]
                  })(
                    <Select
                    showSearch
                    allowClear
                    placeholder="请输入"
                    optionFilterProp="children"
                    filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                >{this.state.tarTypeList}</Select>
                  )}
                  </FormItem>
              {/*} <FormItem label="类型">
                  {getFieldDecorator('type', {
                      initialValue:userInfo.Type,
                      rules: [{ required: true, message: '请选择' }]
                  })(
                      <Input />
                  )}
                  </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 TarSubTypeModal; 
TarSubTypeModal.propTypes = {
    // 验证userInfo的属性
    userInfo: PropTypes.object
}