/** 
 * Creator:     JYW
 * CreatDate:   2019-03-20
 * descripts：  床位组件
*/
import React, { Component } from 'react';
import { Card, Icon, Avatar, message, Button, Row, Col, Popover, Tooltip } from 'antd';
import { $http } from '../../../containers/config/https';
import DivideBed from './DivideBed'
import PatientInfoDetail from '../Patient/PatientInfoDetail.jsx'
import PatientCourseInfo from '../Patient/PatientCourseInfo.jsx';
import './CSS/bedchart.less';
import { $loadTemplate } from '../../lodop/Common/Print.js';

const { Meta } = Card;
let user;
class BedNew extends Component {
    constructor(props) {
        super(props);
        user = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        this.state = {
            isenter: "0",
            isclick: "",
            mouseInID: "",
            buttonTitle: "打印床头卡",
            buttonTitle1: "打印腕带",
            visible: false,
            groupDesc: user.groupDesc,
        }

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

    MouseEnter(item) {
        let parentState = this.props.getData();
        if ((((parentState.selectBedID !== "") && (parentState.selectBedID !== item.id)) || ((parentState.admID != "") && (item.patInfo.admInfo === undefined))) && (this.state.groupDesc.indexOf("护士") > -1)) {
            this.props.updateData({
                mouseInID: item.id,
                buttondisplay: "inline-block"
            })

        }
        if (((item.unavailReason !== "") && (parentState.admID === "")) && (this.state.groupDesc.indexOf("护士") > -1)) {
            this.props.updateData({
                mouseInID: item.id,
                buttondisplay: "inline-block",
            })
        }
    }

    MouseLeave(item) {
        this.props.updateData({
            mouseInID: "",
            buttondisplay: "none",
        })
    }

    onCardClick(item) {
        let parentState = this.props.getData();
        if (item.unavailReason !== "") {
            return;
        }
        if ((item.patInfo.admInfo === undefined) || (this.props.briefInformation)) {
            return;
        }
        else {
            if (item.id === parentState.selectBedID) {

                setTimeout(() => {
                    this.props.updateData({
                        selectBedID: "",
                        mouseInID: "",
                        admID: "",
                        patID: "",
                        otherSelectID: "",
                    })
                }, 100)
            } else {
                setTimeout(() => {
                    this.props.updateData({
                        admID: item.patInfo.admInfo.admID,
                        patID: item.patInfo.basicInfo.patID,
                        selectBedID: item.id,
                        patStatus: "A",
                        otherSelectID: "",
                        mouseInID: "",
                    })
                }, 100)
            }
        }
    }
    /** 
     * 换床 
     * 或
     * 结束包床操作
    */
    allotBed(bedID, admID, unavailReason, e) {
        e.stopPropagation()
        if (unavailReason === "") {
            //换床
            let data = {
                params: [
                    {
                        patInfo:
                        {
                            bedID: this.props.selectBedID,
                            userID: user.userID,
                            admID: this.props.admID,
                            hospID: user.hospID,
                            transType: "4",
                            transStatus: "1",
                        },
                        exchangePatInfo:
                        {
                            bedID: bedID,
                            userID: user.userID,
                            admID: admID,
                            hospID: user.hospID,
                            transType: "4",
                            transStatus: "1",
                        }
                    }

                ]
            }
            $http.post('urlDeault', this, {
                code: "04020005",
                data: data,
                success: (res) => {
                    if (+res.errorCode === 0) {
                        message.success("换床成功！!");
                        setTimeout(() => {
                            this.props.updateData({
                                admID: "",
                                patID: "",
                                selectBedID: "",
                                otherSelectID: "",
                                mouseInID: "",
                                roomLoading: true,
                            })
                        }, 100)
                        this.props.getPatients();
                    } else {
                        message.error('分床失败!:' + res.errorMessage);
                        this.props.getPatients();
                    }
                }
            })
        }
        else {
            this.charterBed(bedID, admID, "4", e);
        }

    }
    /**包床 */
    charterBed(bedID, admID, flag, e) {
        e.stopPropagation()
        if ((this.props.admID === "") && (admID === "")) {
            message.error("请先选择患者!");
            return
        }
        if (admID === "") { admID = this.props.admID }
        let data = {
            params: [{
                bedID: bedID,
                userID: user.userID,
                admID: admID,
                hospID: user.hospID,
                status: flag,
            }]
        }
        $http.post('urlDeault', this, {
            code: "04020006",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    if (flag !== "4") { message.success("包床成功!"); }
                    else { message.success("结束包床完成!"); }
                    setTimeout(() => {
                        this.props.updateData({
                            admID: "",
                            patID: "",
                            selectBedID: "",
                            otherSelectID: "",
                            mouseInID: "",
                            roomLoading: true,
                        })
                    }, 100)
                    this.props.getPatients();
                } else {
                    message.error('包床失败!:' + res.errorMessage);
                    this.props.getPatients();
                }
            }
        })
    }
    /** 分床
     * 医生，护士，暂时为空
     */
    divideBed(bedID, e) {
        e.stopPropagation();
        if (this.props.admID === "") {
            message.warning("请先选中患者！");
            return;
        }

        this.refs.DivideBed.setState({
            visible: true,
            admID: this.props.admID,
            bedID: bedID,
            patStatus: this.props.patStatus,
        }, () => {
            this.refs.DivideBed.getDivideInfo();
        }
        )
        return;
    }
    //床头点击
    onBedTitleClick = (admID, e) => {
        e.stopPropagation();
        this.PatInfoDetail.showModal();
    }
    //患者姓名点击
    patNameClick = (patID, admID, e) => {
        e.stopPropagation();
        this.childConsume.showModal(admID, patID);
    }
    // 绑定子组件
    onRef = (ref) => {
        this.PatInfoDetail = ref
    }
    onConsumeRef = (ref) => {
        this.childConsume = ref;
    };
    handlePrintBedCard = (admID, type, bedID, e) => {
        e.stopPropagation();
        let data = {};
        data = {
            params: [{
                admID: admID,
                bedID: bedID,
            }]
        }
        let code = (type === 'BedCard') ? "04140012" : "04140013"
        let templateCode = (type === 'BedCard') ? "BedCard" : "Wristband"
        $http.post('urlS', this, {
            code: code,
            data: data,
            success: function (res) {
                if (res.errorCode === '0') {
                    //加载模板
                    $loadTemplate(templateCode, res.result);
                }
            }
        })

    }
    render() {
        const isenter = this.state.isenter
        const propsState = this.props.getData();
        let patInfo = this.props.BedInfo.patInfo;
        let patSexDesc = ""
        if (patInfo.basicInfo !== undefined) {
            patSexDesc = patInfo.basicInfo.patSexDesc;
            // if (patSexDesc === "男") { patSexDesc = "man" }
            // else if (patSexDesc === "女") { patSexDesc = "woman" }
            // else { patSexDesc = "other" }
        }
        let bedInfo = this.props.BedInfo;
        let background = "#F7F7F7";
        if (patInfo.basicInfo) {
            switch (patInfo.basicInfo.patSexCode) {
                case "1": background = "#40A9FF"; break
                case "2": background = "#F759AB"; break
                case "9": background = "#A0D911"; break
                case "0": background = "#A0D911"; break
            }
        }
        if (this.props.BedInfo.unavailReason !== "") background = "#722ED1";
        return (
            <div
                className="bed item-bed icon"
            >
                {
                    (patInfo.admInfo !== undefined) ?
                        (

                            <Card
                                className={
                                    "bed ant-card-bordered-sex " +
                                    (propsState.selectBedID === this.props.BedInfo.id ? "ant-card-bordered-" + patInfo.basicInfo.patSexCode + "c" : "")
                                }
                                style={{ height: '100%' }}
                                headStyle={{
                                    color: '#ffffff',
                                    background: background,
                                }}
                                title={
                                    <Popover
                                        overlayClassName="antpopoverinner" placement="bottomLeft" content={
                                            (
                                                <div>
                                                    <p>患者ID:{patInfo.basicInfo.patNo}</p>
                                                    <p>性别:{patInfo.basicInfo.patSexDesc}</p>
                                                    <p>年龄:{patInfo.admInfo.admAge}</p>
                                                    <p>责医:{patInfo.admInfo.admDocDesc}</p>
                                                    <p>责护:{patInfo.admInfo.admNurDesc} </p>
                                                    <p>就诊日期:{patInfo.admInfo.admDate}</p>
                                                    <p>就诊时间:{patInfo.admInfo.admTime}</p>
                                                    <p>诊断:{patInfo.admInfo.admDiag}</p>
                                                </div>
                                            )
                                        } >
                                        <div

                                            onClick={this.onBedTitleClick.bind(this, patInfo.admInfo.admID)}>
                                            <div className="bedcode">
                                                {this.props.BedInfo.roomDescripts + "-" + this.props.BedInfo.bedcode}
                                            </div>
                                            <div
                                                className="patButton"
                                            ><Tooltip title='打印床头卡'><Icon type='printer' onClick={this.handlePrintBedCard.bind(this, patInfo.admInfo.admID, 'BedCard', this.props.BedInfo.id)} />
                                                </Tooltip>
                                                <Tooltip title='打印腕带'>
                                                    <Icon style={{paddingLeft:'3px'}} type='tag' onClick={this.handlePrintBedCard.bind(this, patInfo.admInfo.admID, 'Wristband', this.props.BedInfo.id)} />
                                                </Tooltip>
                                            </div>
                                            <div
                                                className="patName"
                                                style={{ paddingRight: "3px" }}
                                                onClick={this.patNameClick.bind(this, patInfo.basicInfo.patID, patInfo.admInfo.admID)}
                                            >
                                                {patInfo.basicInfo.patName}
                                            </div>
                                            <div className="nurseLevel">
                                                {
                                                    this.props.BedInfo.unavailReason !== "" ?
                                                        this.props.BedInfo.unavailDay :
                                                        patInfo.admInfo.admNurLevelID === "" ? "" :
                                                            <div
                                                                title={patInfo.admInfo.admNurLevelDesc}
                                                                className="bedicon"
                                                                style={{
                                                                    width: patInfo.admInfo.nurLevelIcon.size + "px",
                                                                    height: patInfo.admInfo.nurLevelIcon.size + "px",
                                                                    backgroundPosition: (-((patInfo.admInfo.nurLevelIcon.position % 10 - 1) * 18)) + "px" + " " + (-(parseInt(+patInfo.admInfo.nurLevelIcon.position / 10) * 18)) + "px"
                                                                }}
                                                            > </div>
                                                }
                                            </div>
                                        </div>
                                    </Popover>

                                }
                                actions={
                                    bedInfo.unavailReason === "" ?
                                        patInfo.admInfo.admIcon.length === 0 ? [<span></span>] :
                                            patInfo.admInfo.admIcon.map(
                                                (item) => {
                                                    return (
                                                        <div
                                                            title={item.title}
                                                            className="bedicon"
                                                            style={{
                                                                width: "18px",
                                                                height: "18px",
                                                                backgroundPosition: (-((item.position % 10 - 1) * 18)) + "px" + " " + (-(parseInt(+item.position / 10) * 18)) + "px"
                                                            }}
                                                        > </div>
                                                    );
                                                }
                                            ) :

                                        [
                                            <div
                                                title={bedInfo.unavailTitle}
                                                className="bedicon"
                                                style={{
                                                    width: "18px",
                                                    height: "18px",
                                                    backgroundPosition: (-((bedInfo.unavailPosition % 10 - 1) * 18)) + "px" + " " + (-(parseInt(+bedInfo.unavailPosition / 10) * 18)) + "px"
                                                }}
                                            >
                                            </div>

                                        ]

                                }
                                bodyStyle={
                                    (!this.props.briefInformation) ?
                                        (this.props.BedInfo.unavailReason === "") ? {

                                        } : {
                                                background: "#E9E9E9",
                                            } : {
                                            height: '0px',
                                            padding: '0px',
                                        }
                                }
                                onClick={this.onCardClick.bind(this, this.props.BedInfo)}
                                onMouseEnter={this.MouseEnter.bind(this, this.props.BedInfo)}     //移入
                                onMouseLeave={this.MouseLeave.bind(this)}     //移出
                            >
                                {
                                    (!this.props.briefInformation) ? (
                                        < Row className="">
                                            {
                                                <Row>
                                                    <Col span={12}>{patInfo.basicInfo.patNo}</Col><Col span={4}>{patInfo.basicInfo.patSexDesc}  </Col> <Col id="linehidden" span={4}>{patInfo.admInfo.admAge}  </Col>
                                                    <Col span={12}>责医:{patInfo.admInfo.admDocDesc}</Col> <Col span={12} style={{
                                                        maxHeight: '50px',
                                                        overflow: 'hidden',
                                                        whiteSpace: 'nowrap',
                                                        textOverflow: 'ellipsis',
                                                    }}>责护:{patInfo.admInfo.admNurDesc}  </Col>
                                                    <Col span={24}
                                                        style={{
                                                            maxHeight: '50px',
                                                            overflow: 'hidden',
                                                            whiteSpace: 'nowrap',
                                                            textOverflow: 'ellipsis',
                                                        }} >就诊日期:{patInfo.admInfo.admDate + " " + patInfo.admInfo.admTime} </Col>
                                                    <Col span={24}
                                                        id="lineEllipsis"
                                                        style={{
                                                            maxHeight: '50px',
                                                            overflow: 'hidden',
                                                            textOverflow: 'ellipsis',
                                                        }}>诊断:{patInfo.admInfo.admDiag}</Col>
                                                </Row>
                                            }
                                            <div className="buttonPosition">
                                                <Button
                                                    className="buttonStyle"
                                                    type="primary"
                                                    style={{
                                                        zIndex: "1000",
                                                        display: (propsState.buttondisplay === "inline-block") && (this.props.BedInfo.id === propsState.mouseInID) ? "inline-block" : "none"
                                                    }}
                                                    onClick={this.allotBed.bind(this, this.props.BedInfo.id, patInfo.admInfo.admID, this.props.BedInfo.unavailReason)}
                                                >
                                                    {
                                                        this.props.BedInfo.unavailReason === "" ? "换床" : "结束包床"
                                                    }

                                                </Button>
                                            </div>
                                        </Row>
                                    ) : ""
                                }
                            </Card >

                        )
                        : (
                            <Card

                                title={
                                    <div className="bedcode">
                                        {this.props.BedInfo.roomDescripts + "-" + this.props.BedInfo.bedcode}
                                    </div>
                                }
                                hoverable
                                actions={[<span></span>]}
                                bodyStyle={
                                    (!this.props.briefInformation) ? {
                                    } : {
                                            height: '0px',
                                            padding: '0px',
                                        }
                                }
                                onClick={this.onCardClick.bind(this, this.props.BedInfo)}
                                onMouseEnter={this.MouseEnter.bind(this, this.props.BedInfo)}     //移入
                                onMouseLeave={this.MouseLeave.bind(this)}     //移出
                            >
                                {
                                    (this.props.briefInformation) ? "" : (
                                        <div>
                                            <div style={{ height: '100%', width: '100%' }}>
                                                <div
                                                    title={bedInfo.iconTitle}
                                                    className="bedicon"
                                                    style={{
                                                        margin: '27px 0 0 89px',
                                                        width: bedInfo.iconSize + "px",
                                                        height: bedInfo.iconSize + "px",
                                                        backgroundPosition: (-((bedInfo.iconPosition % 10 - 1) * 18)) + "px" + " " + (-(parseInt(+bedInfo.iconPosition / 10) * 18)) + "px"
                                                    }}
                                                >
                                                </div>
                                                <span className="emptyMsg">暂无入住患者</span>
                                            </div>
                                            <div className="buttonPosition">
                                                <Button
                                                    className="buttonStyle"
                                                    type="primary"
                                                    onClick={this.divideBed.bind(this, this.props.BedInfo.id)}
                                                    style={{
                                                        display: (propsState.buttondisplay === "inline-block") && (this.props.BedInfo.id === propsState.mouseInID) &&
                                                            ((propsState.patStatus === "A") || (propsState.patStatus === "I") || (propsState.patStatus === "O")) ? "inline-block" : "none"
                                                    }}
                                                >
                                                    分床
                                                 </Button>
                                                <br />
                                                {

                                                    this.props.selectBedID !== "" ? (
                                                        <Button
                                                            className="buttonStyle"
                                                            type="primary"
                                                            onClick={this.charterBed.bind(this, this.props.BedInfo.id, "", "2")}
                                                            style={{ display: (propsState.buttondisplay === "inline-block") && (this.props.BedInfo.id === propsState.mouseInID) ? "inline-block" : "none" }} >
                                                            包床
                                                        </Button>
                                                    ) : ""

                                                }
                                            </div>
                                        </div>
                                    )

                                }
                                <DivideBed
                                    ref="DivideBed"
                                    briefInformation={this.props.briefInformation}
                                    selectBedID={this.props.selectBedID}
                                    admID={this.props.admID}
                                    updateData={this.props.updateData}
                                    getData={this.props.getData}
                                    getPatients={this.props.getPatients}
                                    locID={this.props.locID}
                                    child={this.props.child}
                                ></DivideBed>

                            </Card>
                        )
                }
                <PatientCourseInfo
                    activeKey="PatientCourse"
                    onRef={this.onConsumeRef}
                />
                <PatientInfoDetail
                    onRef={this.onRef}
                    getPatients={this.props.getPatients}
                    admID={patInfo.admInfo != undefined ? patInfo.admInfo.admID : ""}
                />
            </div >
        )
    }

}
export default BedNew;