/**
 * 病人基本信息详情。
 */
import React from 'react';
import { Modal, Col, Select, message, Input } from 'antd';
import { $http } from '../../../containers/config/https';
import './style/style.less';

// 组件const
const { Option } = Select;
class PatientInfoDetail extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            patInfo: {},
            column: [],
            doctor: [],
            nurse: [],
            docID: "",
            nurID: "",
            docGroupID: "",
            docGroupList: [],
            supDoctor: [],
            supDoctorID: '',
            attendDocList: [],//当前科室主治医生
            attendDoctorID: '',
            departmentHead: [],
            departmentHeadID: '', // 科室主任
        }
    }

    componentDidMount() {
        this.props.onRef(this);
    }

    componentWillReceiveProps(nextprops) {
        if ((nextprops.admID !== this.props.admID) && (nextprops.admID !== undefined)) {
            this.handleQuery(nextprops.admID);
        }
    }

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

    // 新版床位图使用
    modifyVisible = (visible, admID) => {
        this.setState({
            visible,
        }, () => {
            visible && this.handleQuery(admID);
        });
    }

    //编辑用户基本信息详情
    handleOk = () => {
        if (this.state.admID != "") {
            if (!this.state.attendDoctorID) {
                message.error("请选择科主治医师!");
                return
            }
            if (!this.state.supDoctorID) {
                message.error("请选择主任医师!");
                return
            }
            if (!this.state.departmentHeadID) {
                message.error("请选择科室主任!");
                return
            }
            let data = {
                params: [{
                    admID: this.state.admID,
                    nurID: this.state.nurID,
                    docID: this.state.docID,
                    docGroupID: this.state.docGroupID,
                    attendDoctorID: this.state.attendDoctorID,
                    supDoctorID: this.state.supDoctorID,
                    departmentHeadID: this.state.departmentHeadID,
                }]
            }
            $http.post('urlDeault', this, {
                code: "04020012",
                data: data,
                success: (res) => {
                    if (+res.errorCode === 0) {
                        message.success("更新成功!")
                    }
                }
            })
        }
        else {
            message.warning("患者就诊ID为空！")
        }
        this.setState({
            visible: false,
        });
        this.props.getPatients();
    }

    //取消编辑
    handleCancel = () => {
        this.setState({
            visible: false,
        });
    }

    //查询要显示的用户基本信息详情
    handleQuery = (admID) => {
        if (admID === "") { return; }
        let data = {
            params: [{
                admID: admID,
            }]
        }
        $http.post('urlDeault', this, {
            code: "04020011",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    this.setState({
                        admID,
                        patInfo: res.result.patInfo,
                        column: res.result.column,
                        doctor: res.result.doctor,
                        nurse: res.result.nurse,
                        docGroupID: res.result.patInfo.admInfo.MedGroupDr,
                        supDoctorID: res.result.patInfo.admInfo.supDoctorID,
                        attendDoctorID: res.result.patInfo.admInfo.attendDoctorID,
                        departmentHeadID: res.result.patInfo.admInfo.departmentHeadID,
                        departmentHead: res.result.departmentHead, // 科室主任
                    }, () => {
                        this.getDocGroupInfo(this.state.supDoctorID, this.state.patInfo.admInfo.admLocID);
                        this.getAttendAndSupDocList(this.state.patInfo.admInfo.admWardID);
                    })
                }
            }
        })
    }

    //暴漏给外部关闭
    showModal = () => {
        this.setState({
            visible: true,
        });
        this.handleQuery(this.props.admID);
    }

    //更改护士触发的事件
    onNurseChange = (value) => {
        this.setState({
            nurID: value,
        })
    }

    //更改医生触发的事件
    onDoctorChange = (value) => {
        this.setState({
            docID: value,
        })
    }

    handleDepartmentHeadChange = (value) => {
        this.setState({
            departmentHeadID: value,
        })
    }

    //更改上级医师触发事件
    handleSupDocOnChange = (value) => {
        this.setState({
            supDoctorID: value,
            docGroupID: "",
        }, () => {
            this.getDocGroupInfo(this.state.supDoctorID, this.state.patInfo.admInfo.admLocID);
        })
    }

    //更改主治医师触发事件
    handleAttendDocOnChange = (value) => {
        this.setState({
            attendDoctorID: value,
        })
    }

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

    // 获取主治医生和主任医师列表
    getAttendAndSupDocList = (locID) => {
        let data = {
            params: [{
                wardID: locID
            }]
        }
        $http.post('urlDeault', this, {
            code: "01040241",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    this.setState({
                        supDoctor: res.result.supDoctor,
                        attendDocList: res.result.attendDocList,//当前科室主治医生
                    });
                } else {
                    message.error('获取主治医生和主任医师列表!:' + res.errorMessage);
                }
            }
        })
    }

    /**
     * @ByShizi: 哈尔滨普瑞-护士站分床选择科主任等检索功能优化
     * 需求 #950
     * Create 2021-09-27
     * */
    render() {
        let patInfo = this.state.column.map(
            (item) => {
                return (
                    <Col
                        key={item.dataIndex}
                        span={12}
                        style={{ textAlign: 'right', padding: '3px 16px 3px 0' }}
                    >
                        <span className={(item.dataIndex.indexOf("attendDoc") > -1 || item.dataIndex.indexOf("supDoc") > -1 || item.dataIndex.indexOf("admDoc") > -1 || item.dataIndex.indexOf("departmentHead") > -1 || item.dataIndex.indexOf("Nur") > -1 ? 'formRequired' : '') + ' pd-form-title'}>{item.title}</span>
                        {item.dataIndex.indexOf("attendDoc") > -1 ? (
                            <Select
                                style={{ width: '80%', marginLeft: '6px' }}
                                placeholder="主治医师"
                                optionFilterProp="role"
                                value={this.state.attendDoctorID}
                                disabled={item.display === "Y" ? false : true}
                                onChange={this.handleAttendDocOnChange}
                                allowClear
                                showSearch
                            >
                                {
                                    this.state.attendDocList.map(
                                        (doc, index) => {
                                            return (
                                                <Option
                                                    value={doc.id}
                                                    key={index}
                                                    title={doc.descripts}
                                                    role={doc.descripts + '^' + doc.descriptsSPCode}
                                                >
                                                    {doc.descripts}
                                                </Option>
                                            )
                                        }
                                    )
                                }
                            </Select>
                        ) : (item.dataIndex.indexOf("supDoc") > -1 ? (
                            <Select
                                style={{ width: '80%', marginLeft: '6px' }}
                                placeholder="主任医师"
                                optionFilterProp="role"
                                value={this.state.supDoctorID}
                                disabled={item.display === "Y" ? false : true}
                                onChange={this.handleSupDocOnChange}
                                allowClear
                                showSearch
                            >
                                {
                                    this.state.supDoctor.map(
                                        (doc, index) => {
                                            return (
                                                <Option
                                                    value={doc.id}
                                                    key={index}
                                                    title={doc.descripts}
                                                    role={doc.descripts + '^' + doc.descriptsSPCode}
                                                >
                                                    {doc.descripts}
                                                </Option>
                                            )
                                        }
                                    )
                                }
                            </Select>
                        ) : (item.dataIndex.indexOf("admDoc") > -1 ? (
                            <Select
                                style={{ width: '80%', marginLeft: '6px' }}
                                placeholder="主管医师"
                                value={this.state.docID === "" ? this.state.patInfo.admInfo.admDocID : this.state.docID}
                                disabled={item.display === "Y" ? false : true}
                                onChange={this.onDoctorChange}
                                optionFilterProp="role"
                                allowClear
                                showSearch
                            >
                                {
                                    this.state.doctor.map(
                                        (doctor) => {
                                            return (
                                                <Option
                                                    key={doctor.id}
                                                    value={doctor.id}
                                                    title={doctor.descripts}
                                                    role={doctor.descripts + '^' + doctor.descriptsSPCode}
                                                >
                                                    {doctor.descripts}
                                                </Option>
                                            )
                                        }
                                    )
                                }
                            </Select>
                        ) : (item.dataIndex.indexOf("departmentHead") > -1 ? (
                            <Select
                                style={{ width: '80%', marginLeft: '6px' }}
                                placeholder="科室主任"
                                value={this.state.departmentHeadID}
                                disabled={item.display === "Y" ? false : true}
                                onChange={this.handleDepartmentHeadChange}
                                optionFilterProp="seachprop"
                                allowClear
                                showSearch
                            >
                                {this.state.departmentHead ? React.$SelectOptions(this.state.departmentHead) : ''}
                            </Select>
                        ) : (item.dataIndex.indexOf("Nur") > -1 ? (
                            <Select
                                style={{ width: '80%', marginLeft: '6px' }}
                                placeholder="主管护士"
                                value={this.state.nurID === "" ? this.state.patInfo.admInfo.admNurID : this.state.nurID}
                                disabled={item.display === "Y" ? false : true}
                                onChange={this.onNurseChange}
                                optionFilterProp="role"
                                allowClear
                                showSearch
                            >
                                {
                                    this.state.nurse.map(
                                        (nurse) => {
                                            return (
                                                <Option
                                                    key={nurse.id}
                                                    value={nurse.id}
                                                    title={nurse.descripts}
                                                    role={nurse.descripts + '^' + nurse.descriptsSPCode}
                                                >
                                                    {nurse.descripts}
                                                </Option>
                                            )
                                        }
                                    )
                                }
                            </Select>
                        ) : (item.dataIndex.indexOf("group") > -1 ? (
                            <Select
                                style={{ width: '80%', marginLeft: '6px' }}
                                placeholder="医生分组"
                                value={this.state.docGroupID}
                                optionFilterProp="role"
                                disabled={item.display === "Y" ? false : true}
                                onChange={this.onDocGroupChange}
                                allowClear
                                showSearch
                            >
                                {
                                    this.state.docGroupList.map(
                                        (group, index) => {
                                            return (
                                                <Option
                                                    value={group.medGroupLogID}
                                                    key={index}
                                                    title={group.medGroupDesc}
                                                    role={group.medGroupDesc}
                                                >
                                                    {group.medGroupDesc}
                                                </Option>
                                            )
                                        }
                                    )
                                }
                            </Select>
                        ) : (
                            <Input
                                style={{ width: '80%', marginLeft: '6px' }}
                                value={this.state.patInfo.basicInfo[item.dataIndex] === undefined ? this.state.patInfo.admInfo[item.dataIndex] : this.state.patInfo.basicInfo[item.dataIndex]} disabled={item.display === "Y" ? false : true}
                            />
                        ))))))
                        }


                    </Col>
                )
            }
        )

        return (
            <Modal
                title="病人基本信息详情"
                className="patientDetails"
                visible={this.state.visible}
                onOk={this.handleOk}
                onCancel={this.handleCancel}
                okText="更新"
                cancelText="取消"
                destroyOnClose
                maskClosable={false}
                width={1200}
            >

                {
                    patInfo
                }

            </Modal>
        )
    }

}
export default PatientInfoDetail;
