/*
 * Creator:     ML
 * CreatDate:   2020-02-28
 * descripts：  抗菌药物信息维护 添加/编辑
 * */
import React from 'react';
import { Form, Row, Col, Input, Select, Button, DatePicker, Modal, Table, Icon, Pagination, message, Divider, Tooltip, Checkbox, Switch } from 'antd';
import moment from 'moment';
import { $http } from '../../../containers/config/https';

const FormItem = Form.Item;
const paddRightCol = {
    padding: "0px"
}
const formItemLayout = { // label 和input宽度
    labelCol: { span: 9 },
    wrapperCol: { span: 15 },
}
const AntimicrobialTabInformation = Form.create()(
    class AntimicrobialTabInformation extends React.Component {
        constructor(props) {
            super(props)
            var userData = JSON.parse(sessionStorage.getItem('userData'))
            this.state = {
                userData: userData,
                visible: false,
                baseParms: {},
                edit: -1,
                params: {},
                id: "",
                defaultFlag: false
            }
        }
        componentDidMount() {
            // this.props.OnChildrefs(this);
            // this.getSelectData();
            this.getAllSelectData();
            // this.getMainTableColumns();
            if ((this.props.medData && this.props.medData.medItemID ? this.props.medData.medItemID : '') !== this.state.medID) {
                this.setState({ medID: this.props.medData && this.props.medData.medItemID ? this.props.medData.medItemID : '' }, () => {
                    if (this.state.medID) {
                        this.getTableData();
                    }

                })
            } else {
                // this.child.handleLoading();
            }
        }

        componentWillReceiveProps(nextProps) {
            if ((nextProps.medData && nextProps.medData.medItemID ? nextProps.medData.medItemID : '') !== this.state.medID) {
                this.setState({ medID: nextProps.medData && nextProps.medData.medItemID ? nextProps.medData.medItemID : '' }, () => {
                    if (this.state.medID) {
                        this.getTableData();
                    }
                    this.resetFields();
                })
            }
        }
        // 重置
        resetFields = () => {
            this.props.form.resetFields(); //清空所有
            this.setState({
                rowData: {}
            })
        }
        // 获取方式下拉框数据
        getAllSelectData = () => {
            let { user, medID } = this.state;
            let data = {
                params: [{
                    unitFlag: "Y",
                    antTypeFlag: "Y",
                }]
            }
            $http.post('urlS', this, {
                code: "03050212",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({ unitData: res.result.unit, patCatData: res.result.antType })
                    }
                }
            })
        }
        // 请求表单数据
        getTableData() {
            this.setState({ loading: true });
            let { page, pageSize, status, hosp, user, medID } = this.state;
            let data = {
                params: [{
                    medItemID: medID, // 医嘱项id
                }],
            }
            $http.post('urlS', this, {
                code: "03050205",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0 && res.result) {
                        this.setState({ params: res.result, id: res.result.ID, loading: false }, () => {
                            // 数据加载成功关闭loading
                            // this.child.handleLoading();
                        })
                    } else {
                        this.setState({ params: {}, id: "", loading: false }, () => {
                            // 数据加载成功关闭loading
                            // this.child.handleLoading();
                        })
                    }
                },
                error: function () {
                    this.setState({ params: {}, id: "", loading: false }, () => {
                        // 数据加载成功关闭loading
                        // this.child.handleLoading();
                    })
                }
            })
        }
        //国际化
        setMessage(name, message) {
            return this.props.localeProvider ? this.props.localeProvider[name][message] : ''
        }
        showInfo = (record, num) => {
            this.setState({
                visible: true,
            })
            if (record && num && num == "1") {
                let arr = []
                this.setState({
                    edit: num,
                    params: record,
                    id: record.ID,
                    defaultFlag: record.pregnantFlag == "Y" ? true : false,
                })
            }
        }

        handleCancel = () => {
            this.props.form.resetFields();
            this.setState({ visible: false, edit: -1, });
        }
        //添加/编辑
        handleCreat = () => {
            if(!this.state.medID){
                message.warning("请先选中左侧抗菌药物")
                return
            }
            this.props.form.validateFields((err, values) => {
                if (!err) {
                    let data = {}
                    data = {
                        params: [{
                            updateUserID: JSON.parse(sessionStorage.userData).userID || '',
                            ID: this.state.id && this.state.id ? this.state.id : "",//修改
                            doseUnitID: values.doseUnitID || "",
                            antibactTypeID: values.antibactTypeID || "",
                            note: values.note || "",
                            dayMaxDose: values.dayMaxDose || "",
                            medItemID: this.state.medID // 医嘱项id
                        }]
                    }
                    $http.post('urlS', this, {
                        code: '03050206',
                        data: data,
                        success: function (res) {
                            if (+res.errorCode === 0) {
                                if (this.state.id) {
                                    message.success(this.props.localeProvider && this.props.localeProvider.PatientCatUpdate ? this.setMessage('PatientCatUpdate', 'descripts') : '更新成功');
                                    // this.props.getNewData();
                                    // 刷新列表数据
                                    this.getTableData();
                                } 
                                // else {
                                //     message.success(this.props.localeProvider && this.props.localeProvider.PatientCatAddS ? this.setMessage('PatientCatAddS', 'descripts') : '添加成功');
                                //     // this.props.getNewData(res.Id);
                                //     // 刷新列表数据
                                //     this.getTableData();
                                // }
                                this.setState({
                                    // visible: false,
                                    // edit: -1,
                                    // params: {},
                                    // id: ""
                                })
                                // this.props.form.resetFields();
                            }
                        }
                    })
                }
            })
        }
        handleDel = () => {
            let { user } = this.state;
            let data = {
                params: [{
                    ID: this.state.id,
                    updateUserID: JSON.parse(sessionStorage.userData).userID || '',
                }]
            }
            $http.post('urlADS', this, {
                code: '03050207',
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        message.success('删除成功');
                        // 刷新列表数据
                        this.getTableData();
                    }
                }
            })
        }
        onChangeSwith = (v) => {
            this.setState({ defaultFlag: v })
        }
        render() {
            const { getFieldDecorator, } = this.props.form;
            const { params } = this.state
            const formLayoutItem = {
                labelCol: { span: 9 },
                wrapperCol: { span: 13 },
            }
            const formLayoutItemNote= {
                labelCol: { span: 7 },
                wrapperCol: { span: 13 },
            }
           const formLayoutItemNoteDose= {
            labelCol: { span: 10 },
            wrapperCol: { span: 14 },
           }
            // 病人分类
            let patCatOp = this.state.patCatData && this.state.patCatData.map((item, index) => {
                return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
            })
            // 单位
            let usageOp = this.state.unitData && this.state.unitData.map((item, index) => {
                return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
            })

            return (
                <div style={{ padding: '4px 2px' }} className="bedDom">
                    <div className="department-header">
                        <Form>
                            <Row gutter={60} style={{ margin: '0' }}>
                                <Col span={6} style={paddRightCol}>
                                    <Form.Item label="病人分类：" {...formLayoutItem}>
                                        {getFieldDecorator('antibactTypeID', {
                                            initialValue: params.antibactTypeID ? params.antibactTypeID : undefined,
                                            rules: [{ required: true, message: '请选择病人分类' }]
                                        })(
                                            <Select showSearch optionFilterProp="seatchprop"  placeholder="请选择">
                                                {patCatOp}
                                            </Select>
                                        )}
                                    </Form.Item>
                                </Col>
                                <Col span={6} style={paddRightCol}>
                                    <FormItem label={this.props.localeProvider && this.props.localeProvider.PatientCatNote ? this.setMessage('PatientCatNote', 'descripts') : "备注"} {...formLayoutItemNote}>
                                        {getFieldDecorator('note', {
                                            initialValue: params && params.note ? params.note : undefined,
                                            rules: [{ required: false, message: '' }],

                                        })(
                                            <Input allowClear />
                                        )}
                                    </FormItem>
                                </Col>
                                <Col span={6} style={paddRightCol}>
                                    <FormItem label={this.props.localeProvider && this.props.localeProvider.PatientCatEnglishDescription ? this.setMessage('PatientCatEnglishDescription', 'descripts') : "单日最大剂量"} {...formLayoutItemNoteDose}>
                                        {getFieldDecorator('dayMaxDose', {
                                            initialValue: params && params.dayMaxDose ? params.dayMaxDose : undefined,
                                            rules: [{ required: false, message: '' }],

                                        })(
                                            <Input allowClear type="number" min={0}/>
                                        )}
                                    </FormItem>
                                </Col>
                                <Col span={6} style={paddRightCol}>
                                    <Form.Item label="单位：" {...formLayoutItemNote}>
                                        {getFieldDecorator('doseUnitID', {
                                            initialValue: params.doseUnitID ? params.doseUnitID : undefined,
                                            rules: [{ required: false, message: '请选择单位' }]
                                        })(
                                            <Select showSearch optionFilterProp="seatchprop" allowClear style={{ width: '150px', marginRight: '8px' }} placeholder="请选择">
                                                {usageOp}
                                            </Select>
                                        )}
                                    </Form.Item>
                                </Col>
                            </Row>
                            <Row style={{ textAlign: "center",marginTop:"12px" }}>
                                <Button key="submit" type="primary" loading={this.setState.loading} disabled={this.state.rowId == ""} onClick={this.handleCreat}>更新</Button>
                                {/* <Button style={{ margin: "0 16px" }} key="del" type="danger" disabled={!this.state.id} loading={this.setState.loading} onClick={this.handleDel}>{this.props.localeProvider && this.props.localeProvider.PatientCatCancel ? this.setMessage('PatientCatCancel', 'descripts') : "删除"}</Button> */}
                                {/* <Button key="cancel" type="primary" loading={this.setState.loading} onClick={this.handleCancel}>{this.props.localeProvider && this.props.localeProvider.PatientCatCancel ? this.setMessage('PatientCatCancel', 'descripts') : "清空"}</Button> */}
                            </Row>
                        </Form>
                    </div>
                </div>
            )
        }
    })
export default AntimicrobialTabInformation  