// 医嘱项---药学相关信息
import React from 'react';
import { Form, Select, Input, Row, Col, Switch, Card, message } from 'antd';
import { $http } from 'containers/config/https';
const FormItem = Form.Item;
let { Option } = Select;

class ADAPharmaceuticalInformation extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            doseUnitDesc: [], //WHO下拉
            DDD: '', //DDD值
        }
    }
    componentDidMount() {
        this.props.onRef(this);
    }

    // 获取并验证输入框当中的值
    handleValidateFields = () => {
        const { form: { validateFields } } = this.props;
        validateFields((errors, values) => {
            // 验证成功
            if (errors === null) {
                let obj = JSON.parse(JSON.stringify(values));
                // console.log( obj)
                obj.antidFlag = obj.antidFlag ? 'Y' : 'N';
                obj.assistFlag = obj.assistFlag ? 'Y' : 'N';
                obj.countryFlag = obj.countryFlag ? 'Y' : 'N';
                obj.provFlag = obj.provFlag ? 'Y' : 'N';
                obj.cityFlag = obj.cityFlag ? 'Y' : 'N';
                obj.monitFlag = obj.monitFlag ? 'Y' : 'N';
                obj.formID = obj.formID ? obj.formID : "";
                obj.glucocorticoidFlag = obj.glucocorticoidFlag ? 'Y' : 'N';
                this.props.receiveSubcomponentsValue(obj, 'pharmaceuticalInformation')
            }
        })
    }

    // 表单赋值
    setFieldsValue = (values) => {
        this.props.form.setFieldsValue({ ...values });
    }

    // 重置数据
    resetFields = () => {
        this.setState({
            DDD: ""
        })
        this.props.form.resetFields();
    }

    // 获取医嘱项名table数据
    getMedicalOrder = (id) => {
        this.setState({ loading: true });
        let data = {
            params: [{
                medID: id ? id : ''
            }],

        }
        $http.post('urlS', this, {
            code: '02020323',
            data: data,
            success: function (res) {
                console.log("res", res)
                if (+res.errorCode == 0 && res.result.row) {
                    for (var i = 0; i < res.result.row.length; i++) {
                        res.result.row[i].key = i
                    }
                    this.setState({
                        doseUnitDesc: res.result.row
                    });
                } else {
                    this.setState({ loading: false })
                }
            },
            error: function () {
                this.setState({ loading: false })
            }
        })
    }

    handleWHODDDChange = (e) => {
        if (this.state.doseUnitDesc.length > 0) {
            let WHOUnitID = this.props.form.getFieldValue("WHOUnitID");
            console.log("WHOUnitID---", WHOUnitID)
            if (WHOUnitID) {
                let op = this.state.doseUnitDesc.filter(item => { return item.doseUnitID == WHOUnitID })
                let WHODDD = e.target.value;
                let Num = op.length > 0 ? op[0].doseQty ? op[0].doseQty : 1 : 1;
                let tatol = (WHODDD ? Number(WHODDD) : 0) / Num
                let number = Math.ceil(tatol);
                this.setState({ DDD: number })
            }
        }
    }

    WHOUnitData = (v) => {
        if (this.state.doseUnitDesc.length > 0) {
            let op = this.state.doseUnitDesc.filter(item => { return item.doseUnitID == v })
            let WHODDD = this.props.form.getFieldValue("WHODDD");
            let Num = op.length > 0 ? op[0].doseQty ? op[0].doseQty : 1 : 1;
            let tatol = (WHODDD ? Number(WHODDD) : 0) / Num
            this.setState({ DDD: tatol })
        }

    }

    render() {
        const { getFieldDecorator } = this.props.form;
        const formItemItem = {
            labelCol: { span: 11 },
            wrapperCol: { span: 13 }
        };
        const formItemCheckbox = {
            labelCol: { span: 14 },
            wrapperCol: { span: 10 }
        };
        let userInfo = this.props.userInfo;
        let selectData = this.props.selectData;

        // 用法
        let usageOption = selectData && selectData.usage && selectData.usage.map((item, index) => {
            return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
        })

        // 频次
        let freqOption = selectData && selectData.freq && selectData.freq.map((item, index) => {
            return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
        })

        // 剂型
        let formOption = 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>)
        })

        // 管制分类
        let poisonOption = selectData && selectData.poison && selectData.poison.map((item, index) => {
            return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
        })

        // 危重级别
        let criticalOption = selectData && selectData.critical && selectData.critical.map((item, index) => {
            return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
        })

        // 最大疗程数单位
        let medDurationIDOption = selectData && selectData.duration && selectData.duration.map((item, index) => {
            return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
        })
        //WHO单位
        let WHOUnitOption = this.state.doseUnitDesc.length > 0 && this.state.doseUnitDesc.map((item, index) => {
            return (<Option key={index} value={item.doseUnitID} title={item.doseUnitDesc} seatchprop={item.doseUnitDesc}>{item.doseUnitDesc}</Option>)
        })
        //WHONET码
        let WHONETOption = selectData && selectData.WHONET && selectData.WHONET.map((item, index) => {
            return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
        })
        return (
            <Card
                type="inner"
                title="药学相关信息"
                size="small"
            >
                <Form style={{ padding: '0 12px' }}>
                    <Row>
                        <Col span={8}>
                            <FormItem label="含量规格：" {...formItemItem}>
                                {getFieldDecorator('medSpec', {
                                    initialValue: userInfo.medSpec ? userInfo.medSpec : undefined,
                                    rules: [{ required: true, message: '请输入含量规格' }]
                                })(
                                    <Input style={{ width: '100%' }} />
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label="默认用法：" {...formItemItem}>
                                {getFieldDecorator('medUsageID', {
                                    initialValue: userInfo.medUsageID ? userInfo.medUsageID : undefined,
                                })(
                                    <Select style={{ width: '100%' }} showSearch optionFilterProp="seatchprop" allowClear>
                                        {usageOption}
                                    </Select>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label="默认频次：" {...formItemItem}>
                                {getFieldDecorator('medFreqID', {
                                    initialValue: userInfo.medFreqID ? userInfo.medFreqID : "",
                                })(
                                    <Select style={{ width: '100%' }} showSearch optionFilterProp="seatchprop" allowClear>
                                        {freqOption}
                                    </Select>
                                )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={8}>
                            <FormItem label="剂型：" {...formItemItem}>
                                {getFieldDecorator('formID', {
                                    initialValue: userInfo.formID ? userInfo.formID : undefined,
                                })(
                                    <Select style={{ width: '100%' }} showSearch optionFilterProp="seatchprop" allowClear>
                                        {formOption}
                                    </Select>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label="商品名：" {...formItemItem}>
                                {getFieldDecorator('tradeName', {
                                    initialValue: userInfo.tradeName ? userInfo.tradeName : undefined,
                                })(
                                    <Input style={{ width: '100%' }} />
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label="管制分类：" {...formItemItem}>
                                {getFieldDecorator('poisonID', {
                                    initialValue: userInfo.poisonID ? userInfo.poisonID : undefined,
                                })(
                                    <Select style={{ width: '100%' }} showSearch optionFilterProp="seatchprop" allowClear>
                                        {poisonOption}
                                    </Select>
                                )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={8}>
                            <FormItem label="危重级别：" {...formItemItem}>
                                {getFieldDecorator('criLevID', {
                                    initialValue: userInfo.criLevID ? userInfo.criLevID : undefined,
                                })(
                                    <Select style={{ width: '100%' }} showSearch optionFilterProp="seatchprop" allowClear>
                                        {criticalOption}
                                    </Select>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label="默认疗程数" {...formItemItem}>
                                {getFieldDecorator('medDurQty', {
                                    initialValue: userInfo.medDurQty ? userInfo.medDurQty : undefined,
                                })(
                                    <Input style={{ width: '100%' }} type="number" className="input-number" />
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label="默认疗程单位" {...formItemItem}>
                                {getFieldDecorator('medDurationID', {
                                    initialValue: userInfo.medDurationID ? userInfo.medDurationID : undefined,
                                })(
                                    <Select style={{ width: '100%' }} showSearch optionFilterProp="seatchprop" allowClear>
                                        {medDurationIDOption}
                                    </Select>
                                )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={8}>
                            <FormItem label="WHODDD值：" {...formItemItem}>
                                {getFieldDecorator('WHODDD', {
                                    initialValue: userInfo.WHODDD ? userInfo.WHODDD : undefined,
                                })(
                                    <Input style={{ width: '100%' }} onChange={this.handleWHODDDChange} />
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label="WHO单位：" {...formItemItem}>
                                {getFieldDecorator('WHOUnitID', {
                                    initialValue: userInfo.WHOUnitID ? userInfo.WHOUnitID : undefined,
                                })(
                                    <Select
                                        style={{ width: '100%' }}
                                        showSearch
                                        optionFilterProp="seatchprop"
                                        allowClear
                                        onChange={this.WHOUnitData}
                                    >
                                        {WHOUnitOption}
                                    </Select>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label="DDD值：" {...formItemItem}>
                                {getFieldDecorator('DDD', {
                                    initialValue: userInfo.DDD ? userInfo.DDD : this.state.DDD ? this.state.DDD : undefined,
                                })(
                                    <Input style={{ width: '100%' }} />
                                )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={8}>
                            <FormItem label="WHONET码：" {...formItemItem}>
                                {getFieldDecorator('WHONET', {
                                    initialValue: userInfo.WHONET ? userInfo.WHONET : undefined,
                                })(
                                    <Select style={{ width: '100%' }} showSearch optionFilterProp="seatchprop" allowClear>
                                        {WHONETOption}
                                    </Select>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label="YPID：" {...formItemItem}>
                                {getFieldDecorator('YPID', {
                                    initialValue: userInfo.YPID ? userInfo.YPID : undefined,
                                })(
                                    <Input style={{ width: '100%' }} />
                                )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={8}>
                            <FormItem label="是否为国家基本药物：" {...formItemCheckbox}>
                                {getFieldDecorator('countryFlag', {
                                    valuePropName: 'checked',
                                    initialValue: userInfo.countryFlag === 'Y' ? true : false,
                                })(
                                    <Switch checkedChildren="是" unCheckedChildren="否" />
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label="是否为省级基本药物：" {...formItemCheckbox}>
                                {getFieldDecorator('provFlag', {
                                    valuePropName: 'checked',
                                    initialValue: userInfo.provFlag === 'Y' ? true : false,
                                })(
                                    <Switch checkedChildren="是" unCheckedChildren="否" />
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label="是否为市级基本药物：" {...formItemCheckbox}>
                                {getFieldDecorator('cityFlag', {
                                    valuePropName: 'checked',
                                    initialValue: userInfo.cityFlag === 'Y' ? true : false,
                                })(
                                    <Switch checkedChildren="是" unCheckedChildren="否" />
                                )}
                            </FormItem>
                        </Col>

                    </Row>
                    <Row>
                        <Col span={8}>
                            <FormItem label="是否为重点监控标志：" {...formItemCheckbox}>
                                {getFieldDecorator('monitFlag', {
                                    valuePropName: 'checked',
                                    initialValue: userInfo.monitFlag === 'Y' ? true : false,
                                })(
                                    <Switch checkedChildren="是" unCheckedChildren="否" />
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label="是否为抗菌药物标志：" {...formItemCheckbox}>
                                {getFieldDecorator('antidFlag', {
                                    valuePropName: 'checked',
                                    initialValue: userInfo.antidFlag === 'Y' ? true : false,
                                })(
                                    <Switch checkedChildren="是" unCheckedChildren="否" />
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label="是否为辅助用药标志：" {...formItemCheckbox}>
                                {getFieldDecorator('assistFlag', {
                                    valuePropName: 'checked',
                                    initialValue: userInfo.assistFlag === 'Y' ? true : false,
                                })(
                                    <Switch checkedChildren="是" unCheckedChildren="否" />
                                )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={8}>
                            <FormItem label="糖皮质激素类药品标志：" {...formItemCheckbox}>
                                {getFieldDecorator('glucocorticoidFlag', {
                                    valuePropName: 'checked',
                                    initialValue: userInfo.glucocorticoidFlag === 'Y' ? true : false,
                                })(
                                    <Switch checkedChildren="是" unCheckedChildren="否" />
                                )}
                            </FormItem>
                        </Col>
                    </Row>
                </Form>
            </Card>
        )
    }
};
let ADAPharmaceuticalInformationForm = Form.create()(ADAPharmaceuticalInformation);
export default ADAPharmaceuticalInformationForm;