// 主菜单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, DatePicker,Switch,Icon} from 'antd';
import Moment from 'moment';

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


const MedicalGroupModel = Form.create()(
    // eslint-disable-next-line
    class extends React.Component {
      state = { visible: false };

      componentDidMount() {
        this.props.onRef(this);
      }

      // 显示弹出层
      showModal = () => {
        this.setState({
            visible: true,
        });
      }

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

      handelFunction(code, msg) {
        const { form: { validateFields } } = this.props;
        validateFields((errors, values) => {
            // 验证成功
            if (errors === null) {
                let data = {
                    params: []
                }

                let obj = values;
                obj.UpdateUserDr = this.props.flag === 'add' ? 1 : this.props.userInfo.UpdateUserDr;
                obj.ID = this.props.flag === 'add' ? '' : this.props.userInfo.ID;
                obj.LocID=this.props.handelThis.state.locID;
                if (obj.StartDate) obj.StartDate=obj.StartDate.format('YYYY-MM-DD')
                if (obj.StopDate) obj.StopDate=obj.StopDate.format('YYYY-MM-DD')
                if (obj.IPFlag===true ) obj.IPFlag='Y';
                if (obj.OPFlag===true ) obj.OPFlag='Y';
                if (obj.ShowDesc===true ) obj.ShowDesc='Y';

                // 处理需要传递的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.handelThis.handleQuery();
                        }
                    },
                    error:function (error) {
                        message.error(msg + '失败：' + error);
                    }
                })
            }
        });
      }

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

      render() {
        const { getFieldDecorator } = this.props.form;
        const formItemLayout = {
            labelCol: {span: 10},
            wrapperCol: {span: 14}
          };
        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('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('ENDesc', {
                        initialValue:userInfo.ENDesc,
                        rules: [{ required: true, message: '请选择' }]
                    })(
                        <Input />
                    )}
                </FormItem>
                <FormItem label="开始日期">
                    {getFieldDecorator('StartDate', {
                        initialValue:userInfo.StartDate ? Moment(userInfo.StartDate, 'YYYY-MM-DD') : null,
                        rules: [{ required: true, message: '请选择' }]
                    })(
                        <DatePicker style={{ width: '100%' }}/>
                    )}
                </FormItem>
                <FormItem label="失效日期">
                    {getFieldDecorator('StopDate', {
                        initialValue:userInfo.StopDate? Moment(userInfo.StopDate, 'YYYY-MM-DD') : null,
                        rules: [{ required: true, message: '请选择' }]
                    })(
                        <DatePicker style={{ width: '100%' }}/>
                    )}
                </FormItem>
                <FormItem label="显示名称" {...formItemLayout}> 
                    {getFieldDecorator('ShowDesc', {
                        valuePropName: 'checked',
                        initialValue:userInfo.ShowDesc==="Y" ? true:false,
                    })(
                        <Switch checkedChildren={<Icon type="check" />} unCheckedChildren={<Icon type="close" />} />
                    )}
                </FormItem>
                <FormItem label="门诊标志" {...formItemLayout}>
                    {getFieldDecorator('OPFlag', {
                        valuePropName: 'checked',
                        initialValue:userInfo.OPFlag==="Y" ? true:false,
                    })(
                        <Switch checkedChildren={<Icon type="check" />} unCheckedChildren={<Icon type="close" />} />
                    )}
                </FormItem>
                <FormItem label="住院标志" {...formItemLayout}>
                    {getFieldDecorator('IPFlag', {
                        valuePropName: 'checked',
                        initialValue:userInfo.IPFlag==="Y" ? true:false,
                    })(
                        <Switch checkedChildren={<Icon type="check" />} unCheckedChildren={<Icon type="close" />} />
                    )}
                </FormItem>
            </Form>
          </Modal>
        );
      }
    }
  );
export default MedicalGroupModel;

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