/** 
 * Creator:     JYW
 * CreatDate:   2019-04-04
 * descripts：  转移组件
 * 
*/
import React, { Component } from "react";
import { Modal, message, Row, Col, Divider, Select } from 'antd';
import { $http } from '../../../containers/config/https';
import TransRecord from './TransRecord';
import PatientInfo from '../Patient/PatientInfo';

import '../BedChart/CSS/bedchart.less'


let user;
const Option = Select.Option;
class TransLoc extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            admID: "",
            patientInfo: "",
            locList: [],
            selectLocInfo: "",
            allWardList: [],
            wardList: [],
            selectWardInfo: "",
            docList: [],
            selectDocInfo: "",
            transRecord: [],
            patStatus: "",
            docGroupID:"",
            docGroupList:[]
        }
        user = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
    }
    onCancel = () => {
        this.setState({
            visible: false
        })
    }
    handleOk = () => {

        if ((this.state.selectWardInfo === "")) {
            message.warning("请选择科室、病区！");
            return;
        }
        let transType = "1";
        if (this.state.selectLocInfo === "") {
            transType = "2";
        }
        let data = {
            params: [{
                admID: this.state.admID,
                locID: this.state.selectLocInfo.locID,
                wardID: this.state.selectWardInfo.locID,
                docID: this.state.selectDocInfo.id,
                hospID: user.hospID,
                userID: user.userID,
                transType: transType,
                docGroupID:this.state.docGroupID
            }]
        }
        $http.post('urlDeault', this, {
            code: "04050001",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    message.success("转科成功！");
                    this.setState({ visible: false });
                    this.props.updateData({
                        admID: "",
                        patID: "",
                        selectBedId: "",
                        otherSelectId: "",
                        mouseInID: "",
                        roomLoading: true,
                    })
                    this.props.getPatients();
                    if (this.props.child) {
                        this.props.child.getRooms();
                    }
                } else {
                    // message.error('转科失败!:' + res.errorMessage);
                }
            }
        })

    }
    handleCancel = () => {
        message.info("转移取消！");
        this.setState({
            visible: false
        });
    }
    componentDidMount() {

    }
    getTransInfo = () => {
        let data = {
            params: [{
                type: "^I^W^OI^",
                hospID: user.hospID,
                detailFlag: "1",
                linkFlag: "1",
                docFlag: "1",
                linkType: "W",
            }]
        }
        $http.post('urlDeault', this, {
            code: "04020008",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    let wardList = [], locList = [];
                    for (let i = 0; i < res.result.Data.length; i++) {
                        let obj = res.result.Data[i];
                        if (obj.locTypeCode === "W") {
                            wardList.push(obj);
                        }
                        if (obj.locTypeCode.indexOf("I") > -1) {
                            locList.push(obj);
                        }
                    }
                    this.setState({
                        locList: locList,
                        // wardList: wardList,
                        allWardList: wardList,
                    });
                } else {
                    message.error('获取病人基本信息失败!:' + res.errorMessage);
                }
            }
        })
    }
    onLocChange = (value, Option) => {
        if (value) {
            let admAge = this.refs.PatientInfo.state.patientInfo.admInfo.admAge.split("岁")[0];
            if ((this.state.locList[Option.key].locAgeFrom !== "") && (this.state.locList[Option.key].locAgeTo !== "")) {
                if ((+(admAge) < this.state.locList[Option.key].locAgeFrom) || (+(admAge) > this.state.locList[Option.key].locAgeTo)) {
                    message.warning("患者年龄不符合该病区收治年龄区间！");
                    return;
                }
            }

            this.setState({
                selectLocInfo: this.state.locList[Option.key],
                wardList: this.state.locList[Option.key].linkLoc,
                docList: this.state.locList[Option.key].locDoctor,
                selectWardInfo: "",
            });
        } else {
            this.setState({
                // wardList: this.state.allWardList,
                wardList: [],
                selectLocInfo: "",
                selectWardInfo: "",
                docList: [],
                selectDocInfo: "",
            });
        }
        this.setState({
            docGroupID:"",
        },()=>{
            this.getDocGroupInfo(this.state.selectDocInfo === "" ? "" : this.state.selectDocInfo.id,this.state.selectLocInfo === "" ? "" : this.state.selectLocInfo.locID);
        });
    }
    onWardChange = (value, Option) => {
        if (value) {
            if (this.state.wardList[Option.key]) {
                this.setState({
                    selectWardInfo: this.state.wardList[Option.key],
                });
            }
        } else {
            this.setState({
                selectWardInfo: "",
            });
        }
    }
    onDoctorChange = (value, Option) => {
        if (value) {
            this.setState({
                selectDocInfo: this.state.docList[Option.key],
            });
        } else {
            this.setState({
                selectDocInfo: "",
            });
        }
        this.setState({
            docGroupID:"",
        },()=>{
            this.getDocGroupInfo(this.state.selectDocInfo === "" ? "" : this.state.selectDocInfo.id,this.state.selectLocInfo === "" ? "" : this.state.selectLocInfo.locID);
        });
    }
    onDocGroupChange=(value)=>{
        this.setState({docGroupID:value});
    }

    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);
                }
            }
        })
    }
    /** 快捷筛选 */
    filterOption = (input, option) => {
        return ((option.props.title.toLowerCase().indexOf(input.toLowerCase()) >= 0) || (option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0))
    }
    render() {
        return (
            <Modal
                title="转科"
                className='transLoc'
                visible={this.state.visible}
                onOk={this.handleOk}
                onCancel={this.handleCancel}
                width="940px"
                style={{
                    height: '60vh',
                    background: '#FFFFFF',
                    borderRadius: '6px',
                    top: '20vh'
                }}
                okText="转出"
                cancelText="取消"
                destroyOnClose
                maskClosable={false}
            >
                <div>
                    <Row style={{ height: '378px' }}>
                        <Row style={{ height: '40px', width: '100%' }}>
                            <PatientInfo admID={this.state.admID} ref="PatientInfo" />
                        </Row>
                        <Col span={24} style={{ padding: '14px 0 20px 32px', paddingTop: "17px", height: '100%' }}>
                            <Row style={{ height: '100%' }}>
                                <Col span={8} className="transLoc left">
                                    <TransRecord admID={this.state.admID} />
                                </Col>
                                <Col span={16} className="transLoc right">
                                    <Row>
                                        <Row>
                                            <Col span={5} className="transLoc right-title">
                                                转入科室:
                                            </Col>
                                            <Col span={12}>
                                                <Select style={{ width: "100%" }}
                                                    optionFilterProp="children"
                                                    filterOption={this.filterOption}
                                                    onChange={this.onLocChange}
                                                    allowClear={true}
                                                    value={this.state.selectLocInfo === "" ? "" : this.state.selectLocInfo.locID}
                                                    showSearch={true}>
                                                    {
                                                        this.state.locList.map(
                                                            (item, index) => {
                                                                return (
                                                                    <Option value={item.locID} title={item.locDescSPCode} key={index}>{item.locDesc}</Option>
                                                                )
                                                            }
                                                        )
                                                    }
                                                </Select>
                                            </Col>
                                        </Row>
                                        <Row>
                                            <Col style={{ maxHeight: "52px", overflow: 'hidden', textOverflow: 'ellipsis' }}>
                                                科室简介:{this.state.selectLocInfo === "" ? "" : this.state.selectLocInfo.locIntroduce}
                                            </Col>

                                        </Row>
                                        <Row>
                                            <Col span={12}>
                                                科主任:{this.state.selectLocInfo === "" ? "" : this.state.selectLocInfo.locDirectorName}
                                            </Col>
                                            <Col span={12}>
                                                科室电话:{this.state.selectLocInfo === "" ? "" : this.state.selectLocInfo.locTel}
                                            </Col>
                                        </Row>
                                        <Row>
                                            <Col span={12}>
                                                楼层信息:{this.state.selectLocInfo === "" ? "" : this.state.selectLocInfo.locBuildDesc + " " + this.state.selectLocInfo.locFloorDesc}
                                            </Col>
                                            <Col span={12}>
                                                收治患者年龄区间：{
                                                    this.state.selectLocInfo === "" ? "" :
                                                        this.state.selectLocInfo.locAgeFrom === "" ? "" : this.state.selectLocInfo.locAgeFrom + "-" + this.state.selectLocInfo.locAgeTo
                                                }
                                            </Col>
                                        </Row>


                                    </Row>
                                    <Divider type='horizontal' />
                                    <Row >
                                        <Col span={5} className="transLoc right-title" >
                                            转入病区:
                                        </Col>
                                        <Col span={12}>
                                            <Select style={{ width: "100%" }}
                                                showSearch={true} optionFilterProp="children"
                                                allowClear={true}
                                                filterOption={this.filterOption}
                                                onChange={this.onWardChange}
                                                value={this.state.selectWardInfo === "" ? "" : this.state.selectWardInfo.locDesc}>
                                                {
                                                    this.state.wardList.map(
                                                        (item, index) => {
                                                            return (
                                                                <Option value={item.locID} key={index} title={item.locDescSPCode}>{item.locDesc}</Option>
                                                            )
                                                        }
                                                    )
                                                }
                                            </Select>
                                        </Col>
                                    </Row>
                                    <Row>
                                        <Row>
                                            <Col style={{ maxHeight: "52px", overflow: 'hidden' }}>
                                                病区简介:{this.state.selectWardInfo === "" ? "" : this.state.selectWardInfo.locIntroduce}
                                            </Col>

                                        </Row>
                                        <Row>
                                            <Col span={12}>
                                                护士长:{this.state.selectWardInfo === "" ? "" : this.state.selectWardInfo.locHeadNurName}
                                            </Col>
                                            <Col span={12}>
                                                病区电话:{this.state.selectWardInfo === "" ? "" : this.state.selectWardInfo.locTel}
                                            </Col>
                                        </Row>
                                        <Row>
                                            <Col>
                                                楼层信息:{this.state.selectWardInfo === "" ? "" : this.state.selectWardInfo.locBuildDesc + " " + this.state.selectWardInfo.locFloorDesc}
                                            </Col>
                                        </Row>
                                    </Row>
                                    <Divider type='horizontal' />
                                    <Row>
                                        <Col span={5} className="transLoc right-title">
                                            主管医师:
                                        </Col>
                                        <Col span={12}>
                                            <Select style={{ width: "100%" }}
                                                showSearch={true}
                                                optionFilterProp="children"
                                                filterOption={this.filterOption}
                                                disabled={this.state.selectLocInfo === ""}
                                                allowClear={true}
                                                value={this.state.selectDocInfo === "" ? "" : this.state.selectDocInfo.id}
                                                onChange={this.onDoctorChange}>

                                                {
                                                    this.state.docList.map(
                                                        (item, index) => {
                                                            return (
                                                                <Option value={item.id} key={index} title={item.descspell}>{item.descripts}</Option>
                                                            )
                                                        }
                                                    )
                                                }
                                            </Select>
                                        </Col>
                                    </Row>
                                    <Row >
                                        <Col span={24} style={{ height: "80px" }}>
                                            医师简介:{this.state.selectDocInfo === "" ? "" : this.state.selectDocInfo.introduce}
                                        </Col>
                                    </Row>
                                    <Row>
                                        <Col span={5} className="transLoc right-title">
                                            医生分组:
                                        </Col>
                                        <Col span={12}>
                                            <Select style={{ width: "100%" }}
                                                showSearch={true}
                                                optionFilterProp="children"
                                                filterOption={this.filterOption}
                                                disabled={this.state.selectLocInfo === ""}
                                                allowClear={true}
                                                value={this.state.docGroupID}
                                                onChange={this.onDocGroupChange}>
                                                {
                                                    this.state.docGroupList.map(
                                                        (item, index) => {
                                                            return (
                                                                <Select.Option value={item.medGroupLogID} key={index} title={item.medGroupDesc}>{item.medGroupDesc}</Select.Option>
                                                            )
                                                        }
                                                    )
                                                }
                                            </Select>
                                        </Col>
                                    </Row>
                                </Col>
                            </Row>
                        </Col>
                    </Row>
                </div >
            </Modal >
        );
    }
}
export default TransLoc;