/** 
 * Creator:     JYW
 * CreatDate:   2019-03-22
 * descripts：  分床
*/
import React from 'react';
import { Modal, Row, Col, Select, message } from 'antd';
import PatientInfo from '../Patient/PatientInfo';
import { $http } from '../../../containers/config/https';

import './CSS/bedchart.less'

let user;
class DivideBed extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            visible: false,
            admID: '',
            bedID: '',
            patientInfo: '',
            nurID: '',
            docID: '',
            nurse: [],
            doctor: [],
            emptyBed: [],
            patStatus: '',
            divideBedConfig: 'N',
            docGroupID: '',
            docGroupList: [],
            supDoctor: [],
            supDoctorID: '',
            admLocID: '',
            attendDocList: [],//当前科室主治医生
            attendDoctorID: '',
            departmentHead: [], // 科室主任
            departmentHeadID: '',
            separateBedsType: '', // 分床标志 【before - 预分床】
        }
        user = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
    }

    componentWillUnmount() {
        this.setState = (state, callback) => {
            return
        }
    }

    componentWillReceiveProps(nextProps) {
        if ((nextProps.admID !== '') && (nextProps.admID)) {
            this.setState({
                admID: nextProps.admID,
            })
        }
    }

    getDivideInfo = () => {
        const { separateBedsType } = this.state;
        let data = {
            params: [{
                admID: this.state.admID,
                bedID: this.state.bedID,
                wardID: this.props.locID,
                bedType: separateBedsType === 'before' ? 'pre' : '', // 预分床标志
            }]
        }
        $http.post('urlDeault', this, {
            code: '04020010',
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    this.setState({
                        nurse: res.result.nurse,
                        doctor: res.result.doctor,
                        emptyBed: res.result.emptyBed,
                        docID: res.result.docID,
                        nurID: res.result.nurID,
                        divideBedConfig: res.result.divideBedConfig,
                        admLocID: res.result.admLocID,
                        docGroupID: res.result.docGroupID,
                        supDoctor: res.result.supDoctor,
                        supDoctorID: res.result.patSupDocID, // 主任医师
                        departmentHead: res.result.departmentHead,
                        departmentHeadID: res.result.departmentHeadID, // 科室主任
                        attendDocList: res.result.attendDocList,
                        attendDoctorID: res.result.patAttendDocID, // 主治
                    }, () => {
                        this.getDocGroupInfo(this.state.supDoctorID, this.state.admLocID);
                    })
                } else {
                    message.error(res.errorMessage);
                }
            }
        })
    }

    /**
     * @shizi: 床位图分床与病人基本信息详情添加科室主任操作，主管主任主治校验必填，并调整样式
     * Create 2021-06-14
     */
    handleOk = () => {
        if (!(this.props.admID)) {
            message.error('请先选择患者!');
            return
        }
        if (!(this.state.bedID)) {
            message.error('请选择新床位!');
            return
        }
        if (this.state.divideBedConfig == 'Y' && (this.state.docID == '' || this.state.nurID == '')) {
            message.error('请选择床位责任人!');
            return
        }
        if (!this.state.attendDoctorID) {
            message.error('请选择科主治医师!');
            return
        }
        if (!this.state.supDoctorID) {
            message.error('请选择主任医师!');
            return
        }
        if (!this.state.departmentHeadID) {
            message.error('请选择科室主任!');
            return
        }
        const { separateBedsType } = this.state;
        let data = {
            params: [{
                bedID: this.state.bedID,
                userID: user.userID,
                admID: this.state.admID,
                hospID: user.hospID,
                transType: '1',
                status: '1',
                transStatus: '1',
                docID: this.state.docID,
                nurID: this.state.nurID,
                patStatus: this.state.patStatus,
                docGroupID: this.state.docGroupID,
                supDoctorID: this.state.supDoctorID,
                attendDoctorID: this.state.attendDoctorID,
                departmentHeadID: this.state.departmentHeadID, // 科室主任
            }]
        }
        $http.post('urlDeault', this, {
            code: separateBedsType === 'before' ? '04020027' : '04020003',
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    message.success(res?.errorMessage || (separateBedsType === 'before' ? '操作成功' : '分床完成!'));
                    setTimeout(() => {
                        this.props.updateData({
                            admID: '',
                            PatID: '',
                            selectBedID: '',
                            otherSelectID: '',
                            mouseInID: '',
                            roomLoading: true,
                        })
                    }, 100)
                    this.props.getPatients();
                    this.handleCancel();
                    if (this.props.child) {
                        this.props.child.getRooms();
                    }
                } else {
                    this.props.getPatients();
                }
            }
        })
    }

    handleCancel = () => {
        this.setState({
            visible: false,
        })
    }

    onChange = (flag, value) => {
        this.setState({
            [flag]: value,
        }, () => {
            if (flag === 'supDoctorID') {
                this.setState({
                    docGroupID: '',
                }, () => {
                    this.getDocGroupInfo(this.state.supDoctorID, this.state.admLocID);
                });
            }
        });
    }

    // 符有权 获取人员分组信息
    getDocGroupInfo = (docID, locID) => {
        let data = {
            params: [{
                userID: docID,
                locID: locID
            }]
        }
        $http.post('urlDeault', this, {
            code: "01040093",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    let docgroupList = []
                    for (let i = 0; i < res.result.length; i++) {
                        let obj = res.result[i];
                        docgroupList.push(obj);
                    }
                    this.setState({
                        docGroupList: docgroupList,
                    }, () => {
                        if (!this.state.docGroupID) {
                            if (this.state.docGroupList.length === 1) {
                                this.setState({ docGroupID: this.state.docGroupList[0].medGroupLogID });
                            } else {
                                this.setState({ docGroupID: '' });
                            }
                        }
                    });
                } else {
                    message.error('获取人员分组信息失败!:' + res.errorMessage);
                }
            }
        })
    }

    render() {
        const { separateBedsType } = this.state;
        return (
            <Modal
                title={separateBedsType === 'before' ? '分配临时床位' : '分床'}
                width="864px"
                className="DivideBed"
                visible={this.state.visible}
                onOk={this.handleOk}
                onCancel={this.handleCancel}
                okText="确认"
                cancelText="取消"
                destroyOnClose
                maskClosable={false}
            >
                <div>
                    <Row>
                        <Row className="Discharge patInfo" style={{ width: '100%', height: '40px', lineHeight: '30px' }}>
                            <PatientInfo admID={this.state.admID} refs="PatientInfo" />
                        </Row>
                        <Col className="DivideBed row">
                            <span className="db-form-title formRequired"> 新   床  位:</span>
                            <Select
                                value={this.state.bedID}
                                onChange={this.onChange.bind(this, 'bedID')}
                                style={{ width: '25%', marginLeft: '5px' }}
                                optionFilterProp="role"
                            >
                                {
                                    this.state.emptyBed.map(
                                        (item) => {
                                            return (
                                                <Select.Option
                                                    key={item.id}
                                                    value={item.id}
                                                    title={item.bedDescripts}
                                                    role={item.bedDescripts}
                                                >
                                                    {item.bedDescripts}
                                                </Select.Option>
                                            )
                                        }
                                    )
                                }
                            </Select>
                        </Col>
                        <Col className="DivideBed row">
                            <span className="db-form-title formRequired">主管医师:</span>
                            <Select
                                onChange={this.onChange.bind(this, 'docID')}
                                style={{ width: '25%' }}
                                value={this.state.docID}
                                optionFilterProp="role"
                                allowClear
                                showSearch
                            >
                                {
                                    this.state.doctor.map(
                                        (item) => {
                                            return (
                                                <Select.Option
                                                    key={item.id}
                                                    value={item.id}
                                                    title={item.descripts}
                                                    role={item.descripts + '^' + item.descriptsSPCode}
                                                >
                                                    {item.descripts}
                                                </Select.Option>
                                            )
                                        }
                                    )
                                }
                            </Select>
                        </Col>
                        <Col className="DivideBed row">
                            <span className="db-form-title formRequired">主治医师:</span>
                            <Select
                                onChange={this.onChange.bind(this, 'attendDoctorID')}
                                style={{ width: '25%' }}
                                value={this.state.attendDoctorID}
                                optionFilterProp="role"
                                allowClear
                                showSearch
                            >
                                {
                                    this.state.attendDocList.map(
                                        (item, index) => {
                                            return (
                                                <Select.Option
                                                    value={item.id}
                                                    key={index}
                                                    title={item.descripts}
                                                    role={item.descripts + '^' + item.descriptsSPCode}
                                                >
                                                    {item.descripts}
                                                </Select.Option>
                                            )
                                        }
                                    )
                                }
                            </Select>
                        </Col>
                        <Col className="DivideBed row">
                            <span className="db-form-title formRequired">主任医师:</span>
                            <Select
                                onChange={this.onChange.bind(this, 'supDoctorID')}
                                style={{ width: '25%' }}
                                value={this.state.supDoctorID}
                                optionFilterProp="role"
                                allowClear
                                showSearch
                            >
                                {
                                    this.state.supDoctor.map(
                                        (item, index) => {
                                            return (
                                                <Select.Option
                                                    value={item.id}
                                                    key={index}
                                                    title={item.descripts}
                                                    role={item.descripts + '^' + item.descriptsSPCode}
                                                >
                                                    {item.descripts}
                                                </Select.Option>
                                            )
                                        }
                                    )
                                }
                            </Select>
                        </Col>
                        <Col className="DivideBed row">
                            <span className="db-form-title formRequired">科室主任:</span>
                            <Select
                                onChange={this.onChange.bind(this, 'departmentHeadID')}
                                style={{ width: '25%' }}
                                value={this.state.departmentHeadID}
                                optionFilterProp="seachprop"
                                allowClear
                                showSearch
                            >

                                {this.state.departmentHead ? React.$SelectOptions(this.state.departmentHead) : ''}
                            </Select>
                        </Col>
                        <Col className="DivideBed row">
                            <span className='db-form-title'>医生分组:</span>
                            <Select
                                onChange={this.onChange.bind(this, 'docGroupID')}
                                style={{ width: '25%' }}
                                value={this.state.docGroupID}
                                optionFilterProp="role"
                                allowClear
                                showSearch
                            >
                                {
                                    this.state.docGroupList.map(
                                        (item, index) => {
                                            return (
                                                <Select.Option
                                                    value={item.medGroupLogID}
                                                    key={index}
                                                    title={item.medGroupDesc}
                                                    role={item.medGroupDesc}
                                                >
                                                    {item.medGroupDesc}
                                                </Select.Option>
                                            )
                                        }
                                    )
                                }
                            </Select>
                        </Col>
                        <Col className="DivideBed row">
                            <span className='db-form-title formRequired'>主管护士:</span>
                            <Select
                                value={this.state.nurID}
                                onChange={this.onChange.bind(this, 'nurID')}
                                style={{ width: '25%' }}
                                optionFilterProp="role"
                                allowClear
                                showSearch
                            >
                                {
                                    this.state.nurse.map(
                                        (item) => {
                                            return (
                                                <Select.Option
                                                    key={item.id}
                                                    value={item.id}
                                                    title={item.descripts}
                                                    role={item.descripts + '^' + item.descriptsSPCode}
                                                >
                                                    {item.descripts}
                                                </Select.Option>
                                            )
                                        }
                                    )
                                }
                            </Select>
                        </Col>
                    </Row>
                </div>
            </Modal>
        )
    }
};

export default DivideBed;