
/*
 * Creator:     XYH 、ML
 * CreatDate:   2020-06-17
 * descripts：  互联网电子病历组件
 * */
import React from 'react'
import { Form, Row, Col, Modal, List, Divider } from 'antd';
import { $http } from '../../../containers/config/https';
import store from 'store';
import './style.less';

let user = {};
let patientData = {};

class RecordCompoent extends React.Component {
    constructor(props) {
        super(props);
        patientData = sessionStorage.patientData ? JSON.parse(sessionStorage.patientData) : {};
        user = sessionStorage.userData ? JSON.parse(sessionStorage.userData) : {};
        patientData = JSON.parse(sessionStorage.getItem('patientData'));
        this.state = {
            recordTemplate: [],
            recordArr: [],
            selectedKeys: [],
            basicData: [],
            baseInfo: {},
            diagnoseObj: [],
            orderPresc: [],
            showButton: true,
            detailMessage: {},
            imageSource: [],
            visible: false,
            firstIndex: "",
        }
    }

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

    showModal = (row) => {
        this.setState({
            visible: true,
            admID: row.admID
        }, () => {
            this.recordTemplate()
        })
    }

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

    recordTemplate = () => {
        let { expandedKeys, admID } = this.state
        let data = {
            params: [{
                admID: admID
            }]
        }
        $http.post('urlDeault', this, {
            code: "10020001",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    let recordArr = []
                    if (res.result.TemplateList) {
                        recordArr = res.result.TemplateList
                        recordArr.forEach(element => {
                            element.title = element.name
                            element.key = element.id
                            element.isLeaf = true
                        });
                    }
                    var treeData = []
                    var treeObj = {
                        title: '病历',
                        key: '0',
                        children: recordArr
                    }
                    treeData.push(treeObj)
                    this.state.selectedKeys.push(recordArr[0].id)
                    // expandedKeys.push(recordArr[0].id)
                    this.setState({
                        TemplateList: res.result.TemplateList,
                        recordTemplate: treeData,
                        recordArr: recordArr,
                        // expandedKeys,
                    }, () => {
                        this.templateBasic()
                        //this.selectHistoryTemplate()
                    })

                }
            }
        })
    }

    templateBasic = () => {
        let { expandedKeys, admID } = this.state
        let data = {
            params: [{
                IEMRTemplateID: this.state.selectedKeys[0],
                admID: admID,

            }]
        }
        $http.post('urlDeault', this, {
            code: "10020002",
            data: data,
            success: (res) => {
                // console.log(res);
                if (+res.errorCode === 0) {
                    this.setState({
                        basicData: res.result,
                    }, () => {
                        this.patRecordData()
                    })

                }
            }
        })
    }

    patRecordData = () => {
        let { admID } = this.state
        let data = {
            params: [{
                admID: admID
            }]
        }
        $http.post('urlDeault', this, {
            code: "10020004",
            data: data,
            success: (res) => {
                // console.log(res);
                if (+res.errorCode === 0) {
                    this.setState({
                        baseInfo: res.result.baseInfo,
                        diagnoseObj: res.result.diagnoseObj,
                        orderPresc: res.result.orderPresc,
                    })
                }
            }
        })

    }

    onSelect = (data, event) => {
        var selectedKeys = this.state.selectedKeys
        if (data.id != "") {
            selectedKeys[0] = data.id
            this.setState({
                showButton: true
            }, () => {
                this.templateBasic()
            })
        }
        this.setState({
            selectedKeys: selectedKeys,
        })

    };

    render() {
        // console.log("this.state.diagnoseObj",this.state.diagnoseObj)
        return (
            <div className="RecordCompoent">
                <Modal
                    visible={this.state.visible}
                    title='电子病历'
                    width={800}
                    onCancel={() => this.setState({
                        visible: false
                    })}
                    footer={null}
                >
                    <div className="medicalRecord" >
                        {/* if(item.elementTypeCode == "Labelarea" || item.TempElementTypeCode== "Labelarea") {
                                                   
                                             } */}
                        <div style={{ width: '100%', height: store.getState().tableHeight.y - 75 + 'px', overflow: 'auto', fontSize: '14px', paddingLeft: '7px', borderTop: '2px solid #eeeeee' }}>
                            {this.state.basicData && this.state.basicData.map((item, index) => {
                                return (
                                    <Row style={{ margin: "12px 0", paddingRight: "28px" }}
                                    >
                                        <Col span={3} style={{ textAlign: "right", color: "#333", fontWeight: "600", marginRight: "4px" }}>{item.IEMRTempElementDesc && item.IEMRTempElementDesc ? item.IEMRTempElementDesc : "--"}</Col>
                                        <Col span={20}>
                                            {item.elementTypeCode == "Labelarea" && !item.IEMRTempElementData ? "" : item.IEMRTempElementData && item.IEMRTempElementData ? item.IEMRTempElementData : "--"}
                                            {item.elementTypeCode == "Labelarea" ? (
                                                <List
                                                    className={item.IEMRTempElementCode && item.IEMRTempElementCode == "DE05.01.025.00" && this.state.diagnoseObj && this.state.diagnoseObj.length != 0 ?
                                                        "data-list" : item.IEMRTempElementCode && item.IEMRTempElementCode == "DE06.00.289.00" && this.state.orderPresc && this.state.orderPresc.length != 0 ?
                                                            "data-list" : "no-data-list"}
                                                    size="small"
                                                    style={{
                                                        // DE06.00.289.00
                                                        maxHeight: item.IEMRTempElementCode && item.IEMRTempElementCode == "DE05.01.025.00" && this.state.diagnoseObj && this.state.diagnoseObj.length != 0 ? "154px" : "254px",
                                                        height: item.IEMRTempElementCode && item.IEMRTempElementCode == "DE05.01.025.00" && this.state.diagnoseObj && this.state.diagnoseObj.length != 0 ?
                                                            "auto" : item.IEMRTempElementCode && item.IEMRTempElementCode == "DE06.00.289.00" && this.state.orderPresc && this.state.orderPresc.length != 0 ?
                                                                "auto" : "95px !important",
                                                        overflow: "auto",
                                                    }}
                                                    bordered
                                                    dataSource={item.IEMRTempElementCode == "DE05.01.025.00" ? this.state.diagnoseObj : this.state.orderPresc}
                                                    renderItem={(item, data) => item.IEMRTempElementCode == "DE05.01.025.00" ? (
                                                        <List.Item key={data.diagnoseID}>{data.ICDDesc + ";"}</List.Item>
                                                    ) : (data.prescTypeCode != "CM" ? (
                                                        <List.Item key={data.ordID}>
                                                            <Row style={{ width: '100%' }}>
                                                                <Col span={16}>
                                                                    {1}、{data.ordDesc} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                                    {data.spec}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{data.qtyUomStr}
                                                                </Col>
                                                                <Col span={1}> <Divider type="vertical" /></Col>
                                                                <Col span={7} style={{ paddingLeft: "2px" }}>
                                                                    {data.durStr}
                                                                </Col>
                                                                <Col span={18}>
                                                                    用法：{data.usageDesc} &nbsp;&nbsp; {data.frequenceDesc}
                                                                </Col>
                                                            </Row>
                                                            {data && data.childOrd && data.childOrd.map((item, index) => {
                                                                <Row>
                                                                    <Col span={18}>
                                                                        <div>{index + 2}、{item.ordDesc}</div>
                                                                        <div>{item.spec}&nbsp;{item.qtyUomStr}</div>
                                                                    </Col>
                                                                    <Col span={6} style={{ borderLeft: '1px solid #999999', }}>
                                                                        <div>{item.durStr}</div>
                                                                    </Col>
                                                                    <Col span={18}>
                                                                        用法：{item.usageDesc} &nbsp;&nbsp; {item.frequenceDesc}
                                                                    </Col>
                                                                </Row>
                                                            })}
                                                        </List.Item>) : (
                                                        <List.Item key={data[0] ? data[0].ordID : ""}>
                                                            {data.comPresc && data.comPresc.length > 0 && data ? (
                                                                <Row>
                                                                    {data.comPresc.map((item, index) => {
                                                                        return (

                                                                            <Col span={24}>
                                                                                {index + 1}、{item.comPrinciple} &nbsp;&nbsp; {item.ordDesc} &nbsp;&nbsp; {item.doseUnitStr}
                                                                            </Col>


                                                                        )
                                                                    })}
                                                                    <Col span={24}>
                                                                        用法 ：{data.comPresc[0].durStr} &nbsp;&nbsp; {data.comPresc[0].frequenceDesc}   &nbsp;&nbsp; {data.prescDoseQty}   &nbsp;&nbsp; {data.prescUsageExplainDesc}  &nbsp;&nbsp; {data.comPresc[0].usageDesc}
                                                                    </Col>
                                                                </Row>
                                                            ) : ""}

                                                        </List.Item>
                                                    ))}
                                                />
                                            ) : ""}
                                        </Col>
                                    </Row>
                                )
                            })}
                        </div>
                        <Row style={{ height: '40px', paddingLeft: '10px' }}>
                            {this.state.TemplateList && this.state.TemplateList.map((data, index) => {
                                return (
                                    <Col span={4} style={{ marginLeft: '5px', cursor: "pointer", padding: "0 8px", borderRadius: "4px", height: '40px', lineHeight: '40px', textAlign: 'center', background: this.state.selectedKeys[0] == data.id ? 'rgba(223, 238, 255, 1)' : '#999999', width: 'auto' }} onClick={this.onSelect.bind(this, data)}>{data.name}</Col>
                                )
                            })}
                        </Row>
                    </div>
                </Modal>
            </div>
        )
    }



} export default Form.create()(RecordCompoent)

