/*
 * Create:      柿子
 * CreateDate:  2023/12/27
 * Describe：   转科
 * */
import React, { Component } from 'react';
import { Modal, Row, Col, Card, Timeline, Icon, Select, Divider, message } from 'antd';
import PatientInfo from './PatientInfo'; // 患者信息展示
import './style/index.less';

const TimeItem = Timeline.Item;

class TransferDepartment extends Component {
  constructor(props) {
    super(props);
    this.state = {
      userData: React.$getSessionData(),
      visible: false,
      rowData: {},
      transRecordData: [], // 转科记录
      selectLocInfo: {}, // 科室信息
      selectWardInfo: {}, // 病区信息
      selectDocInfo: {}, // 医生信息
      locList: [], // 科室下拉
      wardList: [],
      docList: [],
      docGroupList: [],
      docGroupID: undefined, // 医生分组
    }
  };

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

  modifyVisible = (visible, rowData = {}) => {
    const { locList } = this.state;
    if (visible) {
      if (!(locList && locList.length > 0)) {
        this.getLoList();
      }
      this.getTransRecordData(rowData?.admID || ''); // 获取转科数据
    }
    this.setState({ visible, rowData });
  };

  // 获取科室病区数据
  getLoList = async () => {
    try {
      const { userData } = this.state;
      let data = {
        params: [{
          type: '^I^W^OI^',
          hospID: userData?.hospID || '',
          detailFlag: '1',
          linkFlag: '1',
          docFlag: '1',
          linkType: 'W',
        }]
      }
      let res = await React.$asyncPost(this, '04020008', data);
      let locData = res.result?.Data || [];
      let locList = [];
      for (let i = 0; i < locData.length; i++) {
        let obj = locData[i];
        if (obj.locTypeCode.indexOf('I') > -1) {
          locList.push(obj);
        }
      }
      this.setState({
        locList,
      });
    } catch (error) {
      console.log(error);
    }
  }

  // 确认
  handleOk = () => {
    const { rowData } = this.state;

  };

  // 科室
  handleLocChange = e => {
    const { locList, rowData } = this.state;
    let selectLocInfo = {};
    if (e) {
      let currentLocList = locList && locList.filter(item => item.locID === e);
      console.log('currentLocList', currentLocList, locList)
      selectLocInfo = currentLocList && currentLocList.length > 0 ? currentLocList[0] : {};
      let admAge = rowData?.admAge?.split('岁')[0];
      if ((selectLocInfo?.locAgeFrom !== '') && (selectLocInfo?.locAgeTo !== '')) {
        if ((+(admAge) < selectLocInfo?.locAgeFrom) || (+(admAge) > selectLocInfo?.locAgeTo)) {
          message.warning('患者年龄不符合该病区收治年龄区间！');
          return;
        }
      }
      this.state.wardList = selectLocInfo?.linkLoc || [];
      this.state.docList = selectLocInfo?.locDoctor || [];
    }
    this.setState({ selectLocInfo, selectWardInfo: {}, selectDocInfo: {}, docGroupList: [], docGroupID: undefined });
  };

  // 病区
  handleWardChange = (e) => {
    let selectWardInfo = {};
    if (e) {
      const { wardList } = this.state;
      let currentWardList = wardList && wardList.filter(item => item.locID === e);
      selectWardInfo = currentWardList && currentWardList.length > 0 ? currentWardList[0] : {};
    }
    this.setState({
      selectWardInfo
    });
  };

  // 医生
  handleDoctorChange = e => {
    const { docList, selectLocInfo } = this.state;
    let selectDocInfo = {};
    if (e) {
      let currentDocList = docList && docList.filter(item => item.id === e);
      selectDocInfo = currentDocList && currentDocList.length > 0 ? currentDocList[0] : {};
    }
    this.setState({
      selectDocInfo,
      docGroupID: undefined,
    }, () => {
      this.getDocGroupInfo(selectDocInfo?.id || '', selectLocInfo?.locID || '');
    });
  };

  // 医生分组
  handleDocGroupChange = e => {
    this.setState({ docGroupID: e });
  };

  // 获取医生分组数据
  getDocGroupInfo = async (userID, locID) => {
    try {
      const { docGroupID } = this.state;
      let data = {
        params: [{
          userID,
          locID
        }]
      }
      let res = await React.$asyncPost(this, '01040093', data);
      let docGroupList = res?.result || [];
      if (!docGroupID && docGroupList && Array.isArray(docGroupList) && docGroupList.length === 1) {
        this.state.docGroupID = docGroupList[0].medGroupLogID;
      }
      this.setState({
        docGroupList,
      });
    } catch (error) {
      console.log(error);
    }
  };

  // 获取专科记录数据
  getTransRecordData = async (admID) => {
    try {
      let data = {
        params: [{
          admID
        }]
      }
      let res = await React.$asyncPost(this, '04020009', data);
      this.setState({ transRecordData: res?.result || [] });
    } catch (error) {
      console.log(error);
    }
  }

  render() {
    const { visible, rowData, transRecordData, selectLocInfo, selectWardInfo, selectDocInfo, locList, wardList, docList, docGroupList, docGroupID } = this.state;
    return (
      <Modal
        title="转科"
        okText="转出"
        width="50vw"
        bodyStyle={{ padding: '12px', paddingTop: '6px' }}
        visible={visible}
        onOk={this.handleOk}
        onCancel={() => this.modifyVisible(false)}
      >
        <div className="ns-transfer-department">
          <PatientInfo patInfo={rowData} />
          <Row style={{ marginTop: '12px' }}>
            <Col span={8}>
              <Card
                size="small"
                bodyStyle={{ padding: 0 }}
                title={<div className="card-title-left-icon"><div></div>转科记录</div>}
              >
                <div style={{ padding: '6px', paddingLeft: 0 }}>
                  <div style={{ padding: '6px 0 6px 24px', height: '364px', overflow: 'auto' }} className="scroll-bar-style">
                    <Timeline mode="left">
                      {transRecordData && transRecordData.map(
                        (item, index) => {
                          return (
                            <TimeItem key={item.id}
                              dot={(
                                <span>
                                  <span style={{ fontSize: '14px', color: '#595959', paddingRight: '3px' }}>
                                    {item.transTypeDesc}
                                  </span>
                                  {index === (transRecordData.length - 1) ? (
                                    <Icon type="swap-right" style={{ color: item.transTypeID === '0' ? 'red' : (item.transTypeID === '1' ? 'blue' : 'green') }} />
                                  ) : (
                                    <Icon type="swap" style={{ color: item.transTypeID === '0' ? 'red' : (item.transTypeID === '1' ? 'blue' : 'green') }} />
                                  )}
                                </span>
                              )}
                            >
                              <div style={{ align: 'center', fontSize: '14px' }}>
                                {item.transTypeID === '0' ? (item.inLocDesc + '→') : item.transTypeID === '1' ? item.inLocDesc + '→' + item.outLocDesc : item.inWardDesc + '→' + item.outWardDesc}
                              </div>
                              <Row>
                                <Col span={15} style={{ fontSize: '12px' }}>
                                  <span style={{ marginRight: '6px' }}>{item.inDate}</span>{item.inTime}
                                </Col>
                                <Col span={9} style={{ color: '#595959', fontSize: '13px' }}>
                                  {item.inUserName}
                                </Col>
                              </Row>
                            </TimeItem>
                          );
                        }
                      )}
                    </Timeline>
                  </div>
                </div>
              </Card>
            </Col>
            <Col className="ns-transfer-department-right" span={16}>
              <Row>
                <div>
                  转入科室：
                  <Select
                    allowClear
                    showSearch
                    style={{ width: '70%' }}
                    placeholder="请选择"
                    optionFilterProp="search"
                    value={selectLocInfo?.locID || undefined}
                    onChange={this.handleLocChange}
                  >

                    {React.$SelectOptions(locList, 'locID', 'locDesc', 'locDescSPCode')}
                  </Select>
                </div>
                <div className="td-brief-introduction">
                  <div className="common-two-lines-ellipsis">
                    科室简介：{selectLocInfo?.locIntroduce || ''}
                  </div>
                  <Row>
                    <Col span={10}>
                      科主任：{selectLocInfo?.locDirectorName || ''}
                    </Col>
                    <Col span={10}>
                      科室电话：{selectLocInfo?.locTel || ''}
                    </Col>
                  </Row>
                  <Row>
                    <Col span={10}>
                      楼层信息：{selectLocInfo && 'locBuildDesc' in selectLocInfo && selectLocInfo.locBuildDesc && selectLocInfo.locBuildDesc + ' ' + selectLocInfo.locFloorDesc}
                    </Col>
                    <Col span={10}>
                      收治患者年龄区间：
                      {selectLocInfo && 'locAgeFrom' in selectLocInfo && selectLocInfo.locAgeFrom && selectLocInfo.locAgeFrom + '-' + selectLocInfo.locAgeTo}
                    </Col>
                  </Row>
                </div>
              </Row>
              <Divider type="horizontal" />
              <div>
                转入病区：
                <Select
                  allowClear
                  showSearch
                  style={{ width: '70%' }}
                  placeholder="请选择"
                  optionFilterProp="search"
                  value={selectWardInfo?.locDesc || undefined}
                  onChange={this.handleWardChange}
                >
                  {React.$SelectOptions(wardList, 'locID', 'locDesc', 'locDescSPCode')}
                </Select>
              </div>
              <div className="td-brief-introduction">
                <div className="common-two-lines-ellipsis">
                  病区简介：{selectWardInfo?.locIntroduce || ''}
                </div>
                <Row>
                  <Col span={10}>
                    护士长：{selectWardInfo?.locHeadNurName || ''}
                  </Col>
                  <Col span={10}>
                    病区电话：{selectWardInfo?.locTel || ''}
                  </Col>
                </Row>
                <div>
                  楼层信息：{selectWardInfo && 'locBuildDesc' in selectWardInfo && selectWardInfo.locBuildDesc && (selectWardInfo.locBuildDesc + ' ' + selectWardInfo.locFloorDesc)}
                </div>
              </div>
              <Divider type="horizontal" />
              <div>
                主管医师：
                <Select
                  showSearch
                  style={{ width: '70%' }}
                  placeholder="请选择"
                  optionFilterProp="search"
                  disabled={!(selectLocInfo?.locID)}
                  allowClear
                  value={selectDocInfo?.id || undefined}
                  onChange={this.handleDoctorChange}
                >
                  {React.$SelectOptions(docList)}
                </Select>
              </div>
              <div style={{ paddingBottom: '6px', minHeight: '78px' }} className="td-brief-introduction common-three-lines-ellipsis">
                医师简介：{selectDocInfo?.introduce || ''}
              </div>
              <div>
                医生分组：
                <Select
                  showSearch
                  allowClear
                  style={{ width: '70%' }}
                  optionFilterProp="search"
                  placeholder="请选择"
                  disabled={!(selectLocInfo?.locID)}
                  value={docGroupID}
                  onChange={this.handleDocGroupChange}
                >
                  {React.$SelectOptions(docGroupList, 'medGroupLogID', 'medGroupDesc')}
                </Select>
              </div>
            </Col>
          </Row>
        </div>
      </Modal>
    )
  }
};

export default TransferDepartment;