// 新增化学通用名
import React from 'react';
import {Form, Button, Input, Row, Col, message, DatePicker} from 'antd';
import { $http } from 'containers/config/https';
import Moment from 'moment';

const FormItem = Form.Item;

class AddGeneralNameChemistry extends React.Component {
    constructor() {
        super();
        this.state = {
            userInfo: {},
            rowId: '',
            ChineseEnglishSize: {} ,//中英翻译
        }
    }
    componentDidMount() {
        this.props.onRef(this);
    }

    componentWillReceiveProps(newData) {
        if (JSON.stringify(this.state.ChineseEnglishSize) !== JSON.stringify(newData.ChineseEnglishSize)) {
            this.setState({ ChineseEnglishSize: newData.ChineseEnglishSize})
        }
        if (newData.chemistryRowData&&(newData.chemistryRowData.chemicID !== this.state.userInfo.chemicID)) {
            this.setState({userInfo: newData.chemistryRowData, rowId: newData.chemistryRowId})
        }
    }

    // 保存
    handleSave = () => {
        let {userInfo, rowId, ChineseEnglishSize} = this.state;
        const { form: { validateFields } } = this.props;
        validateFields((errors, values) => {
            // 验证成功
            if (errors === null) {
                let data = {
                    params: []
                }
                let obj = JSON.parse(JSON.stringify(values));
                obj.chemicID = rowId ? (userInfo&&userInfo.chemicID ? userInfo.chemicID : '') : '';
                obj.startDate = obj.startDate ? Moment(obj.startDate).format("YYYY-MM-DD") : '';
                obj.stopDate = obj.stopDate ? Moment(obj.stopDate).format("YYYY-MM-DD") : '';
                data.params.push(obj);
                $http.post('urlADS', this, {
                    code: '02020217',
                    data: data,
                    success: function (res) {   
                        //console.log(res)
                        if (+res.errorCode === 0 ) {
                            message.success(rowId ? ChineseEnglishSize.DrugMaintenanceeditSuc ? ChineseEnglishSize.DrugMaintenanceeditSuc.descripts:'编辑成功' : ChineseEnglishSize.DrugMaintenanceaddSuc ? ChineseEnglishSize.DrugMaintenanceaddSuc.descripts:'新增成功');
                            // 新增或编辑成功重置form表单
                            this.resetFields();
                            this.setState({userInfo: {}, rowId: ''})
                            // 刷新列表数据
                            this.props.handleQuery();
                        }
                    }
                })
            }
        })
    }
    
    // 重置表单
    resetFields() {
        this.props.form.resetFields(); //清空所有
    }

    render() {
        const { getFieldDecorator } = this.props.form;
        let {userInfo, ChineseEnglishSize} = this.state;
        const formItemItem  = {
            labelCol: {span: 6},
            wrapperCol: {span: 18}
        };
        return (
            <Form>
                <FormItem label={ChineseEnglishSize.DrugMaintenancechemicCode ? ChineseEnglishSize.DrugMaintenancechemicCode.descripts:'化学通用名代码'} {...formItemItem}>
                    {getFieldDecorator('chemicCode', {
                        initialValue: userInfo&&userInfo.chemicCode ? userInfo.chemicCode : undefined,
                        rules:[{ required : ChineseEnglishSize.DrugMaintenancechemicCode ? ChineseEnglishSize.DrugMaintenancechemicCode.required =='Y'? true:false: false, message: ChineseEnglishSize.DrugMaintenancechemicCode ? ChineseEnglishSize.DrugMaintenancechemicCode.message:'化学通用名代码不能为空'}]
                    })(
                        <Input style={{width: '50%'}} placeholder={ChineseEnglishSize.DrugMaintenanceinputTips ? ChineseEnglishSize.DrugMaintenanceinputTips.descripts:'请输入'} />
                    )}
                </FormItem>
                <FormItem label={ChineseEnglishSize.DrugMaintenancechemicDesc ? ChineseEnglishSize.DrugMaintenancechemicDesc.descripts:'化学通用名描述'} {...formItemItem}>
                    {getFieldDecorator('chemicDesc', {
                        initialValue: userInfo&&userInfo.chemicDesc ? userInfo.chemicDesc : undefined,
                        rules:[{ required : ChineseEnglishSize.DrugMaintenancechemicDesc ? ChineseEnglishSize.DrugMaintenancechemicDesc.required =='Y'? true:false: false, message: ChineseEnglishSize.DrugMaintenancechemicDesc ? ChineseEnglishSize.DrugMaintenancechemicDesc.message:'化学通用名描述不能为空'}]
                    })(
                        <Input style={{width: '50%'}} placeholder={ChineseEnglishSize.DrugMaintenanceinputTips ? ChineseEnglishSize.DrugMaintenanceinputTips.descripts:'请输入'} />
                    )}
                </FormItem>
                <Form.Item label={ChineseEnglishSize.DrugMaintenancestartDate ? ChineseEnglishSize.DrugMaintenancestartDate.descripts:'生效日期'} {...formItemItem}>
                    {getFieldDecorator('startDate', {
                        initialValue: userInfo&&userInfo.startDate ? Moment(userInfo.startDate, 'YYYY-MM-DD') : Moment(),
                        rules:[{ required :true, message: ChineseEnglishSize.DrugMaintenancestartDate ? ChineseEnglishSize.DrugMaintenancestartDate.message:'生效日期不能为空'}]
                    })(
                        <DatePicker style={{width: '50%'}} format="YYYY-MM-DD" allowClear />
                    )}
                </Form.Item>
                <Form.Item label={ChineseEnglishSize.DrugMaintenancestopDate ? ChineseEnglishSize.DrugMaintenancestopDate.descripts:'失效日期'} {...formItemItem}>
                    {getFieldDecorator('stopDate', {
                        initialValue: userInfo&&userInfo.stopDate ? Moment(userInfo.stopDate, 'YYYY-MM-DD') : undefined,
                    })(
                        <DatePicker style={{width: '50%'}} format="YYYY-MM-DD" allowClear />
                    )}
                </Form.Item>
                <Row>
                    <Col span={6} style={{textAlign: 'right'}}>
                        <Button onClick={this.handleSave}>{ChineseEnglishSize.DrugMaintenancesave ? ChineseEnglishSize.DrugMaintenancesave.descripts:'保存'}</Button>
                    </Col>
                </Row>
            </Form>
        )
    }
};
let AddGeneralNameChemistryForm = Form.create()(AddGeneralNameChemistry);
export default AddGeneralNameChemistryForm;