// 主菜单form表单
import React from 'react';
import PropTypes from 'prop-types';
import { $http } from '../../containers/config/https';
import { disposeData } from '../../tools';
import { Modal, Form, Input, message ,Select ,DatePicker} from 'antd';
import moment from 'moment';

const FormItem = Form.Item;
// const Option = Select.Option;


// 下拉菜单数据
// const invTypeData = ['挂号', '门诊', '住院', '体检'];

const FormHandel = Form.create()(
    // eslint-disable-next-line
    class extends React.Component {
      state = { visible: false };
      MedItemData=[];
      
      componentDidMount() {
        this.props.onRef(this);
        this.GetMedItem();
      }
      componentWillMount() {
      }
      GetMedItem=()=>{
          let data = {
              params: [{
                blank:""
              }]
          }
          $http.post("urlS",this, {
              code: "tc001",
              data: data,
              success: function (res) { 
                // console.log(res);
                  if (+res.errorCode === 0) {
                      this.MedItemData=res.result
                  } else {
                      message.error('失败：' + res.errorMessage);
                  }
              }
          }) 
      }
      // 显示弹出层
      showModal = () => {
        this.setState({
            visible: true,
        });
      }

      // 取消
      handleCancel = () => {
        this.setState({
            visible: false,
        }, () => {
            if (this.props.flag === 'compile') {
                this.props.handleThis.resetUserInfo()
            }
        });
      }
      // 重置表单
      resetFields() {
        // console.log("触发了")
        this.props.form.resetFields(); //清空所有
      }

      handleFunction(code, msg) {
        const { form: { validateFields } } = this.props;
        // let userInfo = this.props.userInfo;
        validateFields((errors, values) => {
            // 验证成功
            
            if (errors === null) {
                let data = {
                    params: []
                }
                if(values.StartDate){
                  values.StartDate=values.StartDate.format('YYYY-MM-DD');
                }
                if(values.StopDate){
                  values.StopDate=values.StopDate.format('YYYY-MM-DD');
                }
                let obj = values;
                obj.UpdateUserDr = 1 /* this.props.flag === 'add' ? 1 : this.props.userInfo.UpdateUser */;
                obj.ID = this.props.flag === 'add' ? '' : this.props.userInfo.MedGroupItemID;
                // if(this.props.flag === 'compile'){
                //   console.log(userInfo);
                //   obj.MedItemDr = userInfo.medItemDr;
                // }

                // 处理需要传递的obj
                data.params.push(disposeData(obj));
                console.log(data);
                $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.handleThis.handleQuery();
                        }
                    },
                    error:function (error) {
                        message.error(msg + '失败：' + error);
                    }
                })
            }
        });
      }

      // 确定
      handleCreate = () => {
        let { flag } = this.props;
        if (flag === 'add') {
          this.handleFunction('tc002', '新增');
        } else if (flag === 'compile') {
            this.handleFunction('tc005', '编辑');
        }
        
      }

      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('medGroupID', {
                        initialValue:this.props.handleThis.state.code?this.props.handleThis.state.code:'',
                        rules: [{ required: true, message: '请选择' }]
                    })(                        
                        <Select placeholder="请选择" 
                        style={{width: '150px', marginRight: '10px'}}
                        disabled
                        showSearch
                        >
                        {this.props.handleThis.state.list}
                        </Select>               
                    )}
                </FormItem>
                <FormItem label="医嘱">
                    {getFieldDecorator('medItemDr', {
                        initialValue:userInfo.medItemDr,
                        rules: [{ required: true, message: '请选择' }]
                    })(
                        <Select placeholder="请选择" style={{width: '200px', marginRight: '10px'}} 
                        /* onChange={this.saveID} */
                        showSearch
                        >
                            { 
                                this.MedItemData.map(d => <Select.Option key={d.ID} >{d.medItemDesc}</Select.Option>) 
                            }   
                        </Select> 
                    )}
                </FormItem>
                <FormItem label="开始日期">
                    {getFieldDecorator('StartDate', {
                        initialValue: moment(userInfo.StartDate),
                        rules: [{ required: true, message: '请选择' }]
                    })(
                        <DatePicker />
                    )}
                </FormItem>
                <FormItem label="失效日期">
                    {getFieldDecorator('StopDate', {
                        initialValue:userInfo.StartDate ? moment(userInfo.StopDate):null,
                        rules: [{ required: true, message: '请选择' }]
                    })(
                        <DatePicker />
                    )}
                </FormItem>
            </Form>
          </Modal>
        );
      }
    }
  );
export default FormHandel;

FormHandel.propTypes = {
  // 验证userInfo的属性
  userInfo: PropTypes.object
}