//发票类型弹出框的模型
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} from 'antd';
import Moment from 'moment';
const FormItem = Form.Item;
// const Option = Select.Option;




const InvoiceTypeModal = 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() {
        this.props.form.resetFields(); //清空所有
      }

      handelFunction(code, msg) {
        const { form: { validateFields } } = this.props;
        validateFields((errors, values) => {
            // 验证成功
            if (errors === null) {
                let data = {
                    params: []
                }
                let obj = values;
                ///values[itemField.Code].format('YYYY-MM-DD')
                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;
                obj.id= this.props.flag === 'add' ? '' : this.props.userInfo.ID;
                // 处理需要传递的obj
                data.params.push(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);
                    }
                })
            }
        });
      }

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

      render() {
          const formItemLayout = {
            labelCol: {span: 10},
            wrapperCol: {span: 14}
          };
        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('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('printTemplet', {
                        initialValue:userInfo.PrintTemplet,
                    })(
                        <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 InvoiceTypeModal;

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