// 新增药理学分类
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 AddDrugInformation extends React.Component {
    constructor() {
        super();
        this.state = {
            treeRowData: {},
            flag: 'add',
            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.treeRowData&&(newData.treeRowData.phCatID !== this.state.treeRowData.phCatID)) {
            this.setState({treeRowData: newData.treeRowData, flag: newData.flag})
        }
    }

    // 保存
    handleSave = () => {
        let {treeRowData, flag, 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.phCatID = flag === 'add' ? '' : (treeRowData&&treeRowData.phCatID ? treeRowData.phCatID : '');
                obj.phCatParID = flag === 'add' ? (treeRowData&&treeRowData.phCatID ? treeRowData.phCatID : '') : '';
                data.params.push(obj);
                $http.post('urlADS', this, {
                    code: '02020215',
                    data: data,
                    success: function (res) {   
                        //console.log(res)
                        if (+res.errorCode === 0 ) {
                            message.success(flag === 'add' ? ChineseEnglishSize.DrugMaintenanceaddSuc ? ChineseEnglishSize.DrugMaintenanceaddSuc.descripts:'新增成功' : ChineseEnglishSize.DrugMaintenanceeditSuc ? ChineseEnglishSize.DrugMaintenanceeditSuc.descripts:'编辑成功');
                            // 新增或编辑成功重置form表单
                            this.resetFields();
                            this.setState({treeRowData: {}, flag: 'add'})
                            // 刷新列表数据
                            this.props.handleQuery();
                        }
                    }
                })
            }
        })
    }
    
    // 重置表单
    resetFields() {
        this.props.form.resetFields(); //清空所有
    }
    
    render() {
        let {treeRowData, flag, ChineseEnglishSize} = this.state;
        const { getFieldDecorator } = this.props.form;
        const formItemItem  = {
            labelCol: {span: 6},
            wrapperCol: {span: 18}
        };
        return (
            <Form>
                <FormItem label={flag === 'add' ? ChineseEnglishSize.DrugMaintenancecategoryParent ? ChineseEnglishSize.DrugMaintenancecategoryParent.descripts:'上级分类'
                    : ChineseEnglishSize.DrugMaintenancepreModificationDescription ? ChineseEnglishSize.DrugMaintenancepreModificationDescription.descripts:'改前描述'} {...formItemItem}>
                    {getFieldDecorator('LastCat', {
                        initialValue: treeRowData.phCatDesc ? treeRowData.phCatDesc : undefined,
                    })(
                        <Input style={{width: '50%'}} disabled />
                    )}
                </FormItem>
                <FormItem label={ChineseEnglishSize.DrugMaintenancephCatCode ? ChineseEnglishSize.DrugMaintenancephCatCode.descripts:'分类代码'} {...formItemItem}>
                    {getFieldDecorator('phCatCode', {
                        initialValue: flag === 'add' ? undefined : (treeRowData.phCatCode ? treeRowData.phCatCode : undefined),
                        rules:[{ required : ChineseEnglishSize.DrugMaintenancephCatCode ? ChineseEnglishSize.DrugMaintenancephCatCode.required =='Y'? true:false: false, message: ChineseEnglishSize.DrugMaintenancephCatCode ? ChineseEnglishSize.DrugMaintenancephCatCode.message:'分类代码不能为空'}]
                    })(
                        <Input style={{width: '50%'}} placeholder={ChineseEnglishSize.DrugMaintenanceinputTips ? ChineseEnglishSize.DrugMaintenanceinputTips.descripts:'请输入'} />
                    )}
                </FormItem>
                <FormItem label={ChineseEnglishSize.DrugMaintenancephCatDesc ? ChineseEnglishSize.DrugMaintenancephCatDesc.descripts:'分类描述'} {...formItemItem}>
                    {getFieldDecorator('phCatDesc', {
                        initialValue: flag === 'add' ? undefined : (treeRowData.phCatDesc ? treeRowData.phCatDesc : undefined),
                        rules:[{ required : ChineseEnglishSize.DrugMaintenancephCatDesc ? ChineseEnglishSize.DrugMaintenancephCatDesc.required =='Y'? true:false: false, message: ChineseEnglishSize.DrugMaintenancephCatDesc ? ChineseEnglishSize.DrugMaintenancephCatDesc.message:'分类描述不能为空'}]
                    })(
                        <Input style={{width: '50%'}} placeholder={ChineseEnglishSize.DrugMaintenanceinputTips ? ChineseEnglishSize.DrugMaintenanceinputTips.descripts:'请输入'} />
                    )}
                </FormItem>
                <Row>
                    <Col span={6} style={{textAlign: 'right'}}>
                        <Button onClick={this.handleSave}>{ChineseEnglishSize.DrugMaintenancesave ? ChineseEnglishSize.DrugMaintenancesave.descripts:'保存'}</Button>
                    </Col>
                </Row>
            </Form>
        )
    }
};
let AddDrugInformationForm = Form.create()(AddDrugInformation);
export default AddDrugInformationForm;