/*
 * Creator:     ML
 * CreatDate:   2019-11-21
 * descripts：检验结果 详细
 * */
import React from 'react';
import { Row, Col, Modal, Table, Divider, Icon, Button } from 'antd';
import { $http } from '../../../containers/config/https';
import CommonPagination from 'components/pagination/index';
import ColumnAuthoritysModel from 'pages/columnAuthority/ColumnAuthoritysModel';
import { $loadTemplate } from '../../lodop/Common/Print.js';
class TestResultModal extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      info: {},
      tableData: [],
      show: false,
      columns: [],
      localeProvider: [],
      page: 1,
      pageSize: 10,
      ID: "",
      size: "small",
      printData: []
    }
  }
  componentDidMount() {
    this.props.onRef(this);
  }
  handleOk = (e) => {
  }
  showModal = (ID) => {
    let userData = JSON.parse(sessionStorage.getItem('userData'))
    this.setState({
      visible: true,
      userData,
      ID,
      page: 1,
      pageSize: 10,
      show: false,
      rowId: ""
    }, () => {
      this.getTableData("search")
      this.getMainTableColumns()
    });
  }
  //获取table表格
  getTableData = (e) => {
    const { pageSize, page, ID } = this.state
    let data = {}
    data = {
      params: [{
        reportID: ID || "",
      }],
      pagination: [{
        pageSize: pageSize,
        currentPage: page,
      }]
    }
    $http.post('urlS', this, {
      code: '03050135',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0 && res.result.data) {
          this.setState({
            tableData: res.result.data,
            total: res.result.totalCount,
            search: e
          })
        }
      }
    })
  }
  //获取病人信息
  getInfo = (data) => {
    this.setState({
      info: data
    })
  }
  handleCancel = () => {
    this.setState({
      visible: false,
      StartDate: undefined,
      EndDate: undefined,
      patInfo: {},
      search: "",
      show: false
    })
  }
  // 修改page和pageSize 跳转至---
  onChange = (e, pageSize) => {
    this.setState({ page: e, pageSize: this.state.pageSize }, () => {
      // 重新调用查询方法
      this.getTableData()
    })
  }
  //下拉选择页数
  onShowSizeChange = (page, pageSize) => {
    this.setState({ page: 1, pageSize: pageSize }, () => {
      // 重新调用查询方法
      this.getTableData("search")
    })
  }
  // 显示分页总条数
  showTotal(total) {
    return `共 ${total} 条`;
  }
  //点击Row
  onClickRow = (record) => {
    return {
      onClick: () => {
        this.setState({
          parms: record,
          checkStaus: true
        })
        if (this.state.rowId === "") {
          this.setState({
            rowId: record.ID,
          });
        } else {
          if (this.state.rowId !== record.ID) {
            this.setState({
              rowId: record.ID,
            });
          } else {
            this.setState({
              rowId: "",
            });
          }
        }

      },
      onDoubleClick: () => {
        this.setState({
          rowId: record.ID,
        })
      }
    };
  }
  //设置点击row颜色
  setRowClassName = (record) => {
    return record.ID === this.state.rowId ? 'clickRowStyle' : '';
  }
  onColumnRef = (ref) => {
    this.columnRef = ref;
  }
  // 获取主列表columns
  getMainTableColumns = () => {
    let data = {
      params: [{
        compontName: "LISResult", // 检验结果表头
        reactCode: ['LISResult'],
        type: 'C',
      }]
    }
    $http.post('urlS', this, {
      code: '01040073',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          let arr = res.result && res.result.C.length > 0 ? JSON.parse(JSON.stringify(res.result.C)).map(item => {
            return {
              align: item.align,
              widthNum: parseInt(item.width),
              width: item.width,
              dataIndex: item.dataIndex,
              title: <div onDoubleClick={() => this.columnRef.onOpenModel()}>{this.state.userData.language == 'EN' ? item.enDescripts : item.title}</div>,
              display: item.display,
              key: item.seqNo,
              render: (text, record) => {
                if (item.code == "abnormalFlag") {
                  return (
                    <span style={{ color: "#F6A724", }}>{record.abnormalFlag}</span>
                  )
                } if (item.code == "result") {
                  if (record.abnormalFlag && record.abnormalFlag.length > 0) {
                    return (
                      <span style={{ color: "#F6A724", }}>{record.result}</span>
                    )
                  }
                }
                return text
              }
            }
          }) : '';
          let filterArr = arr.filter(function (item) { return item.display === 'Y' });
          this.setState({
            localeProvider: this.state.userData.language == 'EN' ? res.result.language.EN : res.result.language.CN,
            columns: filterArr, widthTotal: res.totalWidth, localeProvider: this.state.userData.language == 'EN' ? res.result.language.EN : res.result.language.CN
          })
        }
      }
    })
  }
  //国际化
  setMessage(name, message) {
    return this.state.localeProvider ? this.state.localeProvider[name][message] : ''
  }
  handleShow = () => {
    const { show } = this.state
    this.setState({
      show: !show
    })
  }
  //打印
  handlePrint = () => {
    const {ID} = this.state
    let data = {}
    data = {
      params: [{
        reportID: ID ,
      }],
    }
    $http.post('urlS', this, {
      code: '03050223',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0 ) {
          if (res.result && res.result.length>0) {
            res.result.map((item,i)=>{
              $loadTemplate(item.template, item.printStr);
            })
          }
        }
      }
    })
  }
  render() {
    const { info, show, columns } = this.state
    return (
      <div
        tabIndex={1}
        onKeyDown={this.handleOnKeydown}
        style={{ outline: 'none' }}
      >
        <Modal
          className='testDom'
          width='1254px'
          visible={this.state.visible}
          title={this.state.localeProvider && this.state.localeProvider.LISResultInspectionReport ? this.setMessage('LISResultInspectionReport', 'descripts') : "检验报告"}
          onOk={this.handleOk}
          onCancel={this.handleCancel}
          footer={null}
        >
          <div style={{ minHeight: '252px' }}>
            <Row>
              <Col>
                <span className="title">{info.patName}</span>
                <span>{this.state.localeProvider && this.state.localeProvider.LISResultRegistrationNumber ? this.setMessage('LISResultRegistrationNumber', 'descripts') : "登记号："}<span className="patNo">{info.patNo}</span></span>
                <span>{this.state.localeProvider && this.state.localeProvider.LISResultSpecimenNumber ? this.setMessage('LISResultSpecimenNumber', 'descripts') : "标本号："}<span className="patNo">{info.specNo}</span></span>
                <span className="spanDom">
                  <span>{this.state.localeProvider && this.state.localeProvider.LISResultReviewTime ? this.setMessage('LISResultReviewTime', 'descripts') : "审核时间："}{info.reportDate} {info.reportTime}</span>
                  <span>{this.state.localeProvider && this.state.localeProvider.LISResultReviewer ? this.setMessage('LISResultReviewer', 'descripts') : "审核人："}{info.reportUserName}</span>
                </span>
                <Icon type="container" onClick={this.handleShow} style={{ fontSize: "18px", marginLeft: "12px" }} />
              </Col>
            </Row>
            <div className="info" style={{ display: (show ? "block" : "none") }}>
              <Row>
                <Col span={5}>
                  {this.state.localeProvider && this.state.localeProvider.LISResultGenderAge ? this.setMessage('LISResultGenderAge', 'descripts') : "性别/年龄："}{info.patSex}/{info.patAge}
                </Col>
                <Col span={5}>
                  {this.state.localeProvider && this.state.localeProvider.LISResultApplicationDepartment ? this.setMessage('LISResultApplicationDepartment', 'descripts') : "申请科室："}
                  {info.requestLocDesc}
                </Col>
                <Col span={5}>
                  {this.state.localeProvider && this.state.localeProvider.LISResultApplyForADoctor ? this.setMessage('LISResultApplyForADoctor', 'descripts') : "申请医生："}
                  {info.requestUserName}
                </Col>
                <Col span={3}>
                  {this.state.localeProvider && this.state.localeProvider.LISResultSerialNumber ? this.setMessage('LISResultSerialNumber', 'descripts') : "流水号："}
                  {info.serialNumber}
                </Col>
                <Col span={3}>
                  {this.state.localeProvider && this.state.localeProvider.LISResultMedicalRecordNumber ? this.setMessage('LISResultMedicalRecordNumber', 'descripts') : "病案号："}
                  {info.patMedicalNo}
                </Col>
              </Row>
              <Row>
                <Col span={5}>
                  {this.state.localeProvider && this.state.localeProvider.LISResultSpecimenCost ? this.setMessage('LISResultSpecimenCost', 'descripts') : "标本/费用："}
                  {info.specDesc}/{info.ordAmt ? "￥" + info.ordAmt : "--"}
                </Col>
                <Col span={5}>
                  {this.state.localeProvider && this.state.localeProvider.LISResultApplyForAWard ? this.setMessage('LISResultApplyForAWard', 'descripts') : "申请病区："}
                  {info.requestWardDesc}
                </Col>
                <Col span={5}>
                  {this.state.localeProvider && this.state.localeProvider.LISResultClinicalDiagnosis ? this.setMessage('LISResultClinicalDiagnosis', 'descripts') : "临床诊断："}
                  {info.diag}
                </Col>
                <Col span={9}>
                  {this.state.localeProvider && this.state.localeProvider.LISResultSampleNotes ? this.setMessage('LISResultSampleNotes', 'descripts') : "样本备注："}
                  {info.specNote}
                </Col>
              </Row>
              <Row>
                <Col span={10}>
                  {this.state.localeProvider && this.state.localeProvider.LISResultCollectingInformation ? this.setMessage('LISResultCollectingInformation', 'descripts') : "采集信息："}
                  {info.collectionDate}<span style={{ display: "inline-block", width: "4px" }}></span>{info.collectionTime}<span style={{ display: "inline-block", width: "4px" }}></span>{info.collectionUserName}
                </Col>
                <Col span={14}>
                  {this.state.localeProvider && this.state.localeProvider.LISResultReceiveInformation ? this.setMessage('LISResultReceiveInformation', 'descripts') : "接收信息："}
                  {info.accDate}<span style={{ display: "inline-block", width: "4px" }}></span>{info.accTime}<span style={{ display: "inline-block", width: "4px" }}></span>{info.accUserName}
                </Col>
              </Row>
              <Row>
                <Col span={10}>
                  {this.state.localeProvider && this.state.localeProvider.LISResultInitialReviewInformation ? this.setMessage('LISResultInitialReviewInformation', 'descripts') : "初审信息："}
                  {info.firstCheckDate}<span style={{ display: "inline-block", width: "4px" }}></span>{info.firstCheckTime}<span style={{ display: "inline-block", width: "4px" }}></span>{info.firstCheckUserName}
                </Col>
                <Col span={14}>
                  {this.state.localeProvider && this.state.localeProvider.LISResultInitialReviewInformation ? this.setMessage('LISResultInitialReviewInformation', 'descripts') : "审核信息："}
                  {info.reportDate}<span style={{ display: "inline-block", width: "4px" }}></span>{info.reportTime}<span style={{ display: "inline-block", width: "4px" }}></span>{info.reportUserName}
                </Col>
              </Row>
            </div>
            <Divider style={{ margin: "14px 0 0 0" }} />
            <div>
              <Row>
                <Col span={24} style={{ textAlign: "center", fontSize: "24px", position: "relative" }}>
                  {info.ordDesc || "--"}
                  <Button type="primary" onClick={this.handlePrint} disabled={this.state.tableData && this.state.tableData.length == 0} style={{ position: "absolute", right: "0", bottom: "0", top: "8px" }}>
                    打印
                    </Button>
                </Col>
              </Row>
              <Table
                pagination={false}
                columns={columns}
                dataSource={this.state.tableData}
                rowClassName={this.setRowClassName}
                onRow={this.onClickRow}
                rowKey={record => record.key}
                // locale={{emptyText:this.state.findInfo}}
                style={{
                  marginTop: '12px', height: '496px',
                  overflow: 'auto'
                }}
              />
              <CommonPagination size={this.state.size} current={this.state.page} total={this.state.total} search={this.state.search}
                onShowSizeChange={this.onShowSizeChange.bind(this)} complete={true} dataSource={this.state.tableData}
                onChange={this.onChange.bind(this)} />
              <ColumnAuthoritysModel
                clientWidth='800px'
                compontName='LISResult'
                configType="C"
                onRef={this.onColumnRef}
                reloadData={this.getMainTableColumns}
              />
            </div>
          </div>
        </Modal>
      </div>
    )
  }
}
export default TestResultModal    