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

const FormItem = Form.Item;

class AddGeneralNamePrescription extends React.Component {
    constructor() {
        super();
        this.state = {
            userInfo: {}, 
            rowId: '',
            treeData: [], // 药理学分类数据
            ChineseEnglishSize:{}, //中英翻译
        }
    }
    componentDidMount() {
        this.props.onRef(this);
        this.getTreeData();
    }

    componentWillReceiveProps(newData) {
        if (JSON.stringify(this.state.ChineseEnglishSize) !== JSON.stringify(newData.ChineseEnglishSize)) {
            this.setState({ ChineseEnglishSize: newData.ChineseEnglishSize})
        }
        if (newData.rowData&&(newData.rowData.generID !== this.state.userInfo.generID)) {
            this.setState({userInfo: newData.rowData, rowId: newData.rowID})
        }
    }

    // 过滤药理学分类数据
    filterTreeData = (data) => {
        let filerData = data&&data.map(item => {
            return {
                ...item,
                value: item.key,
                children: this.filterTreeData(item.children)
            }
        })
        return filerData;
    }

    // 获取药理学分类数据
    getTreeData = () => {
        var data = {
            params: [{}],
        }
        $http.post('url', this, {
            code: "02020214",
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    let treeData = this.filterTreeData(res.result)
                    this.setState({treeData})
                }
            }
        });
    }

    // 保存
    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.generID = rowId ? userInfo.generID : '';
                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: '02020219',
                    data: data,
                    success: function (res) {   
                        //console.log(res)
                        if (+res.errorCode === 0 ) {
                            message.success(!rowId ? ChineseEnglishSize.DrugMaintenanceaddSuc ? ChineseEnglishSize.DrugMaintenanceaddSuc.descripts:'新增成功' :  ChineseEnglishSize.DrugMaintenanceeditSuc ? ChineseEnglishSize.DrugMaintenanceeditSuc.descripts:'编辑成功');
                            // 新增或编辑成功重置form表单
                            this.resetFields();
                            this.setState({userInfo: {}, rowId: ''})
                            // 刷新列表数据
                            this.props.handleQuery();
                        }
                    }
                })
            }
        })
    }
    
    // 重置表单
    resetFields() {
        this.props.form.resetFields();
    }

    render() {
        let {userInfo, treeData, ChineseEnglishSize} = this.state;
        const { getFieldDecorator } = this.props.form;
        const formItemItem  = {
            labelCol: {span: 6},
            wrapperCol: {span: 18}
        };
        let selectData = this.props.selectData;

        // 化学通用名
        let chemicDescOption = selectData&&selectData.chemistry&&selectData.chemistry.map((item, index) => {
            return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
        })

        // 剂型
        let formDescOption = selectData&&selectData.form&&selectData.form.map((item, index) => {
            return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
        })
        return (
            <Form>
                <FormItem label={ChineseEnglishSize.DrugMaintenancepharmacologyClassification ? ChineseEnglishSize.DrugMaintenancepharmacologyClassification.descripts:'药理学分类'} {...formItemItem}>
                    {getFieldDecorator('phCatID', {
                        initialValue: userInfo&&userInfo.phCatID ? userInfo.phCatID : undefined,
                    })(
                        <TreeSelect
                            style={{width: '50%'}}
                            dropdownStyle={{ maxHeight: 400, overflow: 'auto'}}
                            treeData={treeData}
                            placeholder={ChineseEnglishSize.DrugMaintenancepleaseChoose ? ChineseEnglishSize.DrugMaintenancepleaseChoose.descripts:'请选择'}
                            treeDefaultExpandAll
                            showSearch
                            treeNodeFilterProp="title"
                            allowClear
                        />
                    )}
                </FormItem>
                <FormItem label={ChineseEnglishSize.DrugMaintenancechemicCommonName ? ChineseEnglishSize.DrugMaintenancechemicCommonName.descripts:'化学通用名'} {...formItemItem}>
                    {getFieldDecorator('chemicID', {
                        initialValue: userInfo&&userInfo.chemicID ? userInfo.chemicID + '' : undefined,
                    })(
                        <Select style={{width: '50%'}} placeholder={ChineseEnglishSize.DrugMaintenancepleaseChoose ? ChineseEnglishSize.DrugMaintenancepleaseChoose.descripts:'请选择'} showSearch optionFilterProp="seatchprop" allowClear>
                            {chemicDescOption}
                        </Select>
                    )}
                </FormItem>
                {/* <FormItem label="剂型：" {...formItemItem}>
                    {getFieldDecorator('formID', {
                        initialValue:  userInfo&&userInfo.formID ? userInfo.formID : undefined,
                    })(
                        <Select style={{width: '50%'}} placeholder="请选择" showSearch optionFilterProp="seatchprop" allowClear>
                            {formDescOption}
                        </Select>
                    )}
                </FormItem> */}
                <FormItem label={ChineseEnglishSize.DrugMaintenancegenerCode ? ChineseEnglishSize.DrugMaintenancegenerCode.descripts:'处方通用名代码'} {...formItemItem}>
                    {getFieldDecorator('generCode', {
                        initialValue:  userInfo&&userInfo.generCode ? userInfo.generCode : undefined,
                        rules:[{ required : ChineseEnglishSize.DrugMaintenancegenerCode ? ChineseEnglishSize.DrugMaintenancegenerCode.required =='Y'? true:false: false, message: ChineseEnglishSize.DrugMaintenancegenerCode ? ChineseEnglishSize.DrugMaintenancegenerCode.message:'处方通用名代码不能为空'}]
                    })(
                        <Input style={{width: '50%'}} placeholder={ChineseEnglishSize.DrugMaintenanceinputTips ? ChineseEnglishSize.DrugMaintenanceinputTips.descripts:'请输入'} />
                    )}
                </FormItem>
                <FormItem label={ChineseEnglishSize.DrugMaintenancegenerDesc ? ChineseEnglishSize.DrugMaintenancegenerDesc.descripts:'处方通用名描述'} {...formItemItem}>
                    {getFieldDecorator('generDesc', {
                        initialValue:  userInfo&&userInfo.generDesc ? userInfo.generDesc : undefined,
                        rules:[{ required : ChineseEnglishSize.DrugMaintenancegenerDesc ? ChineseEnglishSize.DrugMaintenancegenerDesc.required =='Y'? true:false: false, message: ChineseEnglishSize.DrugMaintenancegenerDesc ? ChineseEnglishSize.DrugMaintenancegenerDesc.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 : ChineseEnglishSize.DrugMaintenancestartDate ? ChineseEnglishSize.DrugMaintenancestartDate.required =='Y'? true:false: false, 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 AddGeneralNamePrescriptionForm = Form.create()(AddGeneralNamePrescription);
export default AddGeneralNamePrescriptionForm;