/*
 * Create:      柿子
 * CreateDate:  2024/01/15
 * Describe：   医嘱共享查看
 * */
import React, { Component } from 'react';
import { Modal, Card, Row, Col } from 'antd';
import PublicTablePagination from 'pages/common/PubilcTablePagination';

class SharingMedicalOrders extends Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
      rowData: {},
      loading: false,
      diagTotalWidth: 0,
      diagColumns: [],
      diagTableData: [],
      diagRowID: '',
      drugColumns: [],
      drugTotalWidth: 0,
      drugTableData: [],
      drugRowID: '',
      patImg: '', // 患者头像
    }
  };

  componentDidMount() {
    this.props && 'onRef' in this.props && this.props.onRef(this);
  };

  modifyVisible = (visible, rowData = {}) => {
    const { diagColumns, drugColumns } = this.state;
    if (visible) {
      !(diagColumns && diagColumns.length > 0) && this.getDiagColumns();
      !(drugColumns && drugColumns.length > 0) && this.getDrugColumns();
    }
    this.setState({ visible, rowData }, () => {
      visible && this.getTableData();
    });
  };

  // 获取诊断表头数据
  getDiagColumns = async () => {
    try {
      let data = {
        params: [{
          type: 'C',
          compontName: 'SharingMedicalOrdersDiagInfo',
        }]
      }
      let res = await React.$asyncPost(this, '01040073', data);
      let columns = res.result?.C || [];
      if (columns && columns.length > 0) {
        this.setState({
          diagColumns: columns,
          diagTotalWidth: res.totalWidth
        })
      }
    } catch (error) {
      console.log(error)
    }
  };

  // 获取药品表头数据
  getDrugColumns = async () => {
    try {
      let data = {
        params: [{
          type: 'C',
          compontName: 'SharingMedicalOrdersDrugInfo',
        }]
      }
      let res = await React.$asyncPost(this, '01040073', data);
      let columns = res.result?.C || [];
      if (columns && columns.length > 0) {
        this.setState({
          drugColumns: columns,
          drugTotalWidth: res.totalWidth
        })
      }
    } catch (error) {
      console.log(error)
    }
  };

  // 获取列表数据
  getTableData = async () => {
    try {
      this.setState({ loading: true });
      const { rowData } = this.state;
      let data = {
        params: [{
          ...rowData
        }]
      }
      let res = await React.$asyncPost(this, '05110029', data);
      let result = Array.isArray(res.result) && res.result.length > 0 ? res.result[0] : (res?.result || {});
      let tradeinfo = result?.ExTend?.tradearray?.tradeinfo || [];
      let diagTableData = tradeinfo && Array.isArray(tradeinfo) && tradeinfo.length > 0 ? tradeinfo : (Object.prototype.toString.call(tradeinfo) === '[object Object]' ? [{ ...tradeinfo }] : []);
      for (let i = 0; i < diagTableData.length; i++) {
        let diagStr = '';
        let diagArr = diagTableData[i]?.recipearray?.recipe || [];
        for (let j = 0; j < diagArr.length; j++) {
          diagStr += ((j + 1) + '. ' + diagArr[j]?.diagnosename + '； ');
        }
        diagTableData[i].key = i + 1;
        diagTableData[i].diagStr = diagStr;
      }
      this.setState({ diagTableData, loading: false, patImg: result?.PatientPhoto || '' }, () => {
        this.handleClear();
      });
    } catch (error) {
      console.log(error);
      this.setState({ loading: false });
    }
  };

  // 操作行
  onClickRowPublic = (record) => {
    return {
      // 单击行选中
      onClick: () => {
        if (this.state.diagRowID === '' || (this.state.diagRowID && (this.state.diagRowID !== record.key))) {
          let feeitem = record?.feeitemarray?.feeitem || [];
          let drugTableData = feeitem && Array.isArray(feeitem) && feeitem.length > 0 ? feeitem : (Object.prototype.toString.call(feeitem) === '[object Object]' ? [{ ...feeitem }] : []);
          for (let i = 0; i < drugTableData.length; i++) {
            drugTableData[i].key = i + 1;
            drugTableData[i].tradeoldno = drugTableData[i]?.tradeoldno || record?.tradeoldno || '';
          }
          this.setState({
            diagRowID: record.key,
            drugTableData,
          })
        } else {
          this.handleClear();
        }
      }
    }
  };

  handleClear = () => {
    this.setState({ diagRowID: '', drugTableData: [], drugRowID: '', });
  };

  // 选中行操作
  setRowClassNamePublic = (record) => {
    return record.key === this.state.diagRowID ? 'clickRowStyle' : '';
  };

  handleDrugClickRow = (record) => {
    return {
      // 单击行选中
      onClick: () => {
        if (this.state.rowID === '' || (this.state.rowID && (this.state.rowID !== record.key))) {
          this.setState({
            drugRowID: record.key,
          })
        } else {
          this.setState({
            drugRowID: '',
          })
        }
      }
    }
  };

  // 选中行操作
  handleDrugSetRowClassName = (record) => {
    return record.key === this.state.drugRowID ? 'clickRowStyle' : '';
  };

  render() {
    const { visible, diagTableData, diagColumns, diagTotalWidth, loading, drugColumns, drugTotalWidth, drugTableData, patImg } = this.state;
    return (
      <Modal
        title="最近就医50天数据下载展示"
        width="60vw"
        footer={null}
        bodyStyle={{ padding: '12px' }}
        visible={visible}
        onCancel={() => this.modifyVisible(false)}
      >
        <div className="table-body-height">
          <Card
            size="small"
            title={<div className="card-title-left-icon"><div></div>诊断信息</div>}
          >
            <Row>
              <Col span={4} style={{ paddingRight: '12px' }} className="flex-center">
                <img style={{ width: '100%' }} src={'data:image/jpeg;base64,' + patImg} alt="患者头像" />
              </Col>
              <Col span={20}>
                <PublicTablePagination
                  param={{
                    y: 150,
                    height: '200px',
                    data: diagTableData, // 表格数据
                    x: diagTotalWidth, // 表格的宽度
                    columns: diagColumns, // 表头配置
                    loading: loading,
                    componentName: 'SharingMedicalOrdersDiagInfo',
                  }}
                  getColumns={this.getDiagColumns}
                  onClickRowPublic={this.onClickRowPublic}
                  setRowClassNamePublic={this.setRowClassNamePublic}
                />
              </Col>
            </Row>
          </Card>
          <Card
            size="small"
            style={{ marginTop: '12px' }}
            title={<div className="card-title-left-icon"><div></div>药品信息</div>}
          >
            <PublicTablePagination
              param={{
                y: 200,
                height: '250px',
                data: drugTableData, // 表格数据
                x: drugTotalWidth, // 表格的宽度
                columns: drugColumns, // 表头配置
                componentName: 'SharingMedicalOrdersDrugInfo',
              }}
              getColumns={this.getDrugColumns}
              onClickRowPublic={this.handleDrugClickRow}
              setRowClassNamePublic={this.handleDrugSetRowClassName}
            />
          </Card>

        </div>
      </Modal>
    )
  }
};

export default SharingMedicalOrders;