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

const FormItem = Form.Item;
const paddRightCol = {
    paddingRight: "0px"
}
const formItemLayout = { // label 和input宽度
    labelCol: { span: 9 },
    wrapperCol: { span: 15 },
}
const formItemLayoutType = {
    labelCol: { span: 10 },
    wrapperCol: { span: 14 },
}

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

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

        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
                        })
                    }
                }
            })
        }

        //获取房间类型
        getRoomType = () => {
            let data = {
                params: [{
                    roomTypeFlag: 1
                }],
            }
            $http.post('urlS', this, {
                code: '01040022',
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({
                            roomArr: res.roomType
                        })
                    }
                }
            })
        }

        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 : '',//修改
                            hospID: values.hospID || '',
                            qty: values.qty || '',
                            medItemID: this.state.medItemID,
                            feeTypeID: values.feeTypeID || '',//费用类型	
                            roomTypeID: values.roomTypeID || '',//床位费类型
                            priorityID: values?.priorityID || '',
                            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: '01040133',
                        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();
                            }
                        }
                    })
                }
            })
        }

        //清空医嘱项
        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 roomOp = []
            this.state.roomArr && this.state.roomArr.map((item) => {
                roomOp.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 gutter={60} style={{ margin: '0' }}>
                                    <Col span={8} style={paddRightCol}>
                                        <FormItem {...formItemLayout} label='医院'>
                                            {getFieldDecorator('hospID', {
                                                initialValue: params && params.hospID ? params.hospID : undefined,
                                                rules: [{ required: true, message: '请选择医院' }],
                                            })(
                                                <Select placeholder='请选择医院'
                                                >
                                                    {hosOp}
                                                </Select>
                                            )}
                                        </FormItem>
                                    </Col>

                                    <Col span={8} style={paddRightCol}>
                                        <FormItem {...formItemLayoutType} label='床位费类别'>
                                            {getFieldDecorator('feeTypeID', {
                                                initialValue: params && params.feeTypeID ? params.feeTypeID : undefined,
                                                rules: [{ required: true, message: '请选择床位费类别' }],
                                            })(
                                                <Select onChange={this.handleValue.bind(this, 'bedTypeID')}
                                                >
                                                    {feeOp}
                                                </Select>
                                            )}
                                        </FormItem>
                                    </Col>

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

                                    <Col span={8} style={paddRightCol}>
                                        <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} style={paddRightCol}>
                                        <FormItem label='房间类型' {...formItemLayout}>
                                            {getFieldDecorator('roomTypeID', {
                                                initialValue: params && params.roomTypeID ? params.roomTypeID : undefined,
                                                rules: [{ required: true, message: '请选择房间类型' }],
                                            })(
                                                <Select
                                                >
                                                    {roomOp}
                                                </Select>
                                            )}
                                        </FormItem>
                                    </Col>
                                    <Col span={8} style={paddRightCol}>
                                        <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={8} style={paddRightCol}>
                                        <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={8} style={paddRightCol}>
                                        <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={8} style={paddRightCol}>
                                        <FormItem label="计费时间" {...formItemLayout}>
                                            {getFieldDecorator('billTime', {
                                                initialValue: params.billTime ? moment(params.billTime, 'HH:mm') : undefined,
                                            })(
                                                <TimePicker format="HH:mm" style={{ width: '100%' }} />
                                            )}
                                        </FormItem>
                                    </Col>
                                </Row>
                            </Form>
                        </div>
                    </Modal>
                </div>
            )
        }
    });

export default BedCostToBedTypeModal;