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

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,
        userInfo: {}
      };

      componentDidUpdate() {
        if (this.state.visible === true) {
            Util.changeEnter()
        }
      }

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

      // 显示弹出层
      showModal = () => {
        this.setState({
            visible: true,
        }, () => {
          if (this.props.flag === 'compile') {
            this.getRowData(this.props.userInfo.ID)
          }
        });
      }

      // 取消
      handleCancel = () => {
        this.setState({
            visible: false,
        }, () => {
            if (this.props.flag === 'compile') {
                this.setState({userInfo: {}})
                this.resetFields()
            }
        });
      }
      
      // 重置表单
      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
                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();
                        } else {
                          if (this.props.flag === 'compile') {
                            // 编辑可能失败，但还是需要重置表单
                            this.resetFields();
                          }
                        }
                    },
                    error:function () {
                        if (this.props.flag === 'compile') {
                          // 编辑可能失败，但还是需要重置表单
                          this.resetFields();
                        }
                    }
                })
            }
        });
      }

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

      // 按照id查询单条数据
      getRowData(id) {
        let data ={
          params: [{
            ID: id
          }]
        }
        $http.post('urlS', this, {
          code: "2205",
          data: data,
          success: function (res) { 
            if (+res.errorCode === 0) {
              this.setState({userInfo: res.result})
            } else {
              this.setState({userInfo: {}})
            }
          }
        })
      }

      render() {
        const { getFieldDecorator } = this.props.form;
        let userInfo = this.state.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 ? userInfo.Code: undefined,
                        rules: [{ required: true, message: '代码不能为空' }]
                    })(
                        <Input placeholder="请输入代码" className='input' />
                    )}
                </FormItem>
                <FormItem label="描述">
                    {getFieldDecorator('Descripts', {
                        initialValue:userInfo.Descripts ? userInfo.Descripts: undefined,
                        rules: [{ required: true, message: '描述不能为空' }]
                    })(
                        <Input placeholder="请输入描述" className='input' />
                    )}
                </FormItem>
                <FormItem label="英文描述">
                    {getFieldDecorator('ENDesc', {
                        initialValue:userInfo.ENDesc ? userInfo.ENDesc: undefined,
                    })(
                        <Input placeholder="请输入英文描述" className='input' />
                    )}
                </FormItem>
            </Form>
          </Modal>
        );
      }
    }
  );
export default FormHandel;

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