/*
 * Creator:     ML
 * CreatDate:   2019-12-13
 * descripts：  医院关联床位费添加/编辑
 * */
import React from 'react';
import { Form, Row, Col, Input, Select, Button, DatePicker, TimePicker, Modal, message } from 'antd';
import moment from 'moment';
import { $http } from '../../../containers/config/https';

const FormItem = Form.Item;
const Option = Select.Option;
const formItemLayout = { // label 和input宽度
    labelCol: { span: 6 },
    wrapperCol: { span: 16 },
}

const BedToHosModal = Form.create()(
    class BedToHosModal extends React.Component {
        constructor(props) {
            super(props)
            this.state = {
                visible: false,
                baseParms: {},
                edit: -1,
                params: {},
                id: '',
            }
        }

        componentDidMount() {
            this.props.OnChildrefs(this);
            this.getHopSelect()
            this.getFeeType()
        }

        showModal = (record, num) => {
            this.setState({
                visible: true,
            })
            if (record && num && num == "1") {
                let arr = []
                this.setState({
                    edit: num,
                    params: record,
                    id: record.ID,
                    medItemID: record.medItemID
                })
            }
        }

        //获取医院下拉框
        getHopSelect = () => {
            let data = {
                params: [{
                    hospID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).hospID : '',
                    logonGroupID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).groupID : '',
                    logonHospID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).hospID : '',
                }],
            }
            $http.post('urlS', this, {
                code: '01040001',
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({
                            hospArr: res.result
                        })
                    }
                }
            })
        }

        //获取费用类别
        getFeeType = () => {
            let data = {
                params: [{

                }],
            }
            $http.post('urlS', this, {
                code: '01040130',
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({
                            feeArr: res.result.data
                        })
                    }
                }
            })
        }

        handleCancel = () => {
            this.props.form.resetFields();
            this.setState({ visible: false, params: {}, edit: -1, id: '' });
        }

        //添加/编辑卡
        handleCreat = () => {
            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 : '',//修改
                            hospitalID: values.hospID || '',
                            qty: values.Qty || '',
                            medItemID: this.state.medItemID,//医嘱项目
                            feeTypeID: values.feeTypeID || '',//费用类型	
                            priorityID: values.priorityID || '',
                            createDate: values.createDate ? moment(values.createDate).format('YYYY-MM-DD') : undefined, // 创建日期
                            startDay: values.startDay ? moment(values.startDay).format('YYYY-MM-DD') : undefined, // 指定天开始
                            stopDay: values.stopDay ? moment(values.stopDay).format('YYYY-MM-DD') : undefined, // 指定天结束
                            startDate: values.dateFrom ? moment(values.dateFrom).format('YYYY-MM-DD') : undefined, // 生效日期
                            stopDate: values.dateTo ? moment(values.dateTo).format('YYYY-MM-DD') : undefined, // 失效日期
                            billTime: values.billTime ? moment(values.billTime).format('HH:mm') : '', // 计费时间
                        }]
                    }
                    $http.post('urlS', this, {
                        code: '01040011',
                        data: data,
                        success: function (res) {
                            if (+res.errorCode === 0) {
                                if (this.state.edit == "1") {
                                    message.success('修改成功');
                                    this.props.getNewData();
                                } else {
                                    message.success('添加成功');
                                    this.props.getNewData(res.Id);
                                }
                                this.setState({
                                    visible: false,
                                    edit: -1,
                                    params: {},
                                    id: ''
                                })
                                this.props.form.resetFields();
                            }
                        }
                    })
                }
            })
        }

        //子组件 搜索医嘱项
        OnChildrefs = (ref) => {
            this.child = ref
        }

        //接收子组件 医嘱项id 描述
        handleParms = (record) => {
            this.props.form.setFieldsValue({ medItemID: record.descripts })
            this.setState({
                medItemID: record.id
            })
        }

        //搜索医嘱项
        handleOrderItem = () => {
            // console.log("this.props.form.getFieldValue('orderItem')",this.props.form.getFieldValue("orderItem"))
            if (this.props.form.getFieldValue("medItemID") && this.props.form.getFieldValue('medItemID').length == 0) {
                message.warning("请输入医嘱项名称")
            } else {
                //调起
                this.child.showModal(this.props.form.getFieldValue("medItemID"))
            }
        }

        //清空医嘱项
        handleValue(name, e) {
            this.props.form.resetFields('medItemID', undefined);
            this.setState({
                [name]: e,
                medItemID: undefined
            }, () => {
                this.getTableData()
            })
        }

        handleSelectOption(name, e) {
            this.setState({
                [name]: e
            })
        }

        getTableData() {
            let data = {}
            data = {
                params: [{
                    hospID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).hospID : '',
                    bedTypeID: this.state.bedTypeID,
                }]
            }
            $http.post('urlS', this, {
                code: '01040227',
                data: data,
                success: function (res) {
                    if (res.errorCode == '0') {
                        this.setState({ tableData: res.result })
                    }
                }
            })
        }

        render() {
            const { prioritySelectData } = this.props;
            const { getFieldDecorator } = this.props.form;
            const { params } = this.state
            //医院
            let hosOp = []
            this.state.hospArr && this.state.hospArr.map((item) => {
                hosOp.push(<Option key={item.id} value={String(item.id)}>{item.descripts}</Option>)
            })
            //费用类别
            let feeOp = []
            this.state.feeArr && this.state.feeArr.map((item) => {
                feeOp.push(<Option key={item.id} value={String(item.id)}>{item.descripts}</Option>)
            })

            let ordNoteListtOption = []
            this.state.tableData && this.state.tableData.map((item, index) => {
                ordNoteListtOption.push(<Option key={index} value={item.id}>{item.descripts}</Option>)
            })


            return (
                <div style={{ padding: '4px 2px' }} className="bedDom">
                    <Modal
                        className='findPatients'
                        width='1000px'
                        visible={this.state.visible}
                        title={this.state.edit == "1" ? "编辑医院关联床位费" : "添加医院关联床位费"}
                        onOk={this.handleOk}
                        onCancel={this.handleCancel}
                        footer={[
                            <Button key="submit" type="primary" loading={this.setState.loading} disabled={this.state.rowId == ''} onClick={this.handleCreat}>确定</Button>,
                            <Button key="cancel" loading={this.setState.loading} onClick={this.handleCancel}>取消</Button>,
                        ]}>
                        <div className="department-header">
                            <Form>
                                <Row style={{ margin: '0' }}>
                                    <Col span={12}>
                                        <FormItem {...formItemLayout} label="医院">
                                            {getFieldDecorator('hospID', {
                                                initialValue: params && params.hospID ? String(params.hospID) : undefined,
                                                rules: [{ required: true, message: '请选择医院' }],
                                            })(
                                                <Select placeholder='请选择医院' >
                                                    {hosOp}
                                                </Select>
                                            )}
                                        </FormItem>
                                    </Col>

                                    <Col span={12}>
                                        <FormItem {...formItemLayout} label="费用类别">
                                            {getFieldDecorator('feeTypeID', {
                                                initialValue: params && params.feeTypeID ? String(params.feeTypeID) : undefined,
                                                rules: [{ required: true, message: '请选择费用类别' }],
                                            })(
                                                <Select placeholder='请选择费用类别' onChange={this.handleValue.bind(this, 'bedTypeID')} >
                                                    {feeOp}
                                                </Select>
                                            )}
                                        </FormItem>
                                    </Col>

                                    <Col span={12}>
                                        <FormItem {...formItemLayout} label="医嘱项">
                                            {getFieldDecorator('medItemID', {
                                                initialValue: params && params.medItemDescripts ? params.medItemDescripts : this.state.medItemID,
                                                rules: [{ required: true, message: '请输入检索医嘱项' }],
                                            })(
                                                <Select placeholder="输入内容回车检索" allowClear
                                                    onSelect={this.handleSelectOption.bind(this, 'medItemID')}>
                                                    {ordNoteListtOption}
                                                </Select>
                                            )}
                                        </FormItem>
                                    </Col>


                                    <Col span={12}>
                                        <FormItem label="费用数量" {...formItemLayout}>
                                            {getFieldDecorator('Qty', {
                                                initialValue: params && params.qty ? params.qty : undefined,
                                                rules: [{ required: true, message: '请输入数量' }],
                                                getValueFromEvent: (event) => {
                                                    return event.target.value.replace(/\D/g, '')
                                                },
                                            })(
                                                <Input allowClear placeholder='请输入数量' />
                                            )}
                                        </FormItem>
                                    </Col>
                                    {/* <Col span={8}>
                                        <FormItem label='创建日期' {...formItemLayout}>
                                            {getFieldDecorator('createDate', {
                                                initialValue: params.createDate ? moment(params.createDate, "YYYY-MM-DD") : moment(),
                                                rules: [{ required: true, message: '请选择创建日期' }],
                                            })(
                                                <DatePicker style={{ width: '100%' }} disabledDate={(curDate) => {
                                                    return curDate && curDate > moment().endOf('day');
                                                }} />
                                            )}
                                        </FormItem>
                                    </Col> */}
                                    {/* </Row>
                                <Row gutter={60} style={{ margin: '0' }}> */}
                                    <Col span={12}>
                                        <FormItem label="生效日期" {...formItemLayout}>
                                            {getFieldDecorator('dateFrom', {
                                                initialValue: params.startDate ? moment(params.startDate, "YYYY-MM-DD") : moment(),
                                                rules: [{ required: true, message: '请选择生效日期' }],
                                            })(
                                                <DatePicker style={{ width: '100%' }} disabledDate={(curDate) => {
                                                    return curDate && curDate > moment().endOf('day');
                                                }} />
                                            )}
                                        </FormItem>
                                    </Col>
                                    <Col span={12}>
                                        <FormItem label="失效日期" {...formItemLayout}>
                                            {getFieldDecorator('dateTo', {
                                                initialValue: params.stopDate ? moment(params.stopDate, "YYYY-MM-DD") : undefined,
                                                rules: [{ required: false, message: '' }],
                                            })(
                                                <DatePicker style={{ width: '100%' }} />
                                            )}
                                        </FormItem>
                                    </Col>
                                    <Col span={12}>
                                        <FormItem label="指定天开始" {...formItemLayout}>
                                            {getFieldDecorator('startDay', {
                                                initialValue: params.startDay ? moment(params.startDay, "YYYY-MM-DD") : moment(),
                                                rules: [{ required: false, message: '' }],
                                            })(
                                                <DatePicker style={{ width: '100%' }} disabledDate={(curDate) => {
                                                    return curDate && curDate > moment().endOf('day');
                                                }} />
                                            )}
                                        </FormItem>
                                    </Col>
                                    <Col span={12}>
                                        <FormItem label="指定天结束" {...formItemLayout}>
                                            {getFieldDecorator('stopDay', {
                                                initialValue: params.stopDay ? moment(params.stopDay, "YYYY-MM-DD") : undefined,
                                                rules: [{ required: false, message: '' }],
                                            })(
                                                <DatePicker style={{ width: '100%' }}
                                                />
                                            )}
                                        </FormItem>
                                    </Col>
                                    <Col span={12}>
                                        <FormItem label="医嘱优先级" {...formItemLayout}>
                                            {getFieldDecorator('priorityID', {
                                                initialValue: params && params.priorityID ? params.priorityID : undefined,
                                            })(
                                                <Select
                                                    allowClear
                                                    showSearch
                                                    placeholder="请选择"
                                                    optionFilterProp="search"
                                                >
                                                    {React.$SelectOptions(prioritySelectData)}
                                                </Select>
                                            )}
                                        </FormItem>
                                    </Col>
                                    <Col span={12}>
                                        <FormItem label="计费时间" {...formItemLayout}>
                                            {getFieldDecorator('billTime', {
                                                initialValue: params.billTime ? moment(params.billTime, 'HH:mm') : undefined,
                                                rules: [{ required: false, message: '' }],
                                            })(
                                                <TimePicker format="HH:mm" style={{ width: '100%' }} />
                                            )}
                                        </FormItem>
                                    </Col>
                                </Row>
                            </Form>
                        </div>
                    </Modal>
                </div>
            )
        }
    });

export default BedToHosModal;