import { Button, Descriptions, Badge, Popconfirm, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, setDataFieldValue, guid, initQueryFilter } from "../../../utils/common";
import ReactToPrint from "react-to-print";
import { ExportToExcel } from '@woowalker/feui'
import XLSX from 'xlsx';
import { connect } from 'dva';
class HM129ShowPrint extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: null,
    }
  }
  componentDidMount() {
    if (this.props.data?.id)
      this.loadData(this.props.data?.id);
  }
  componentWillReceiveProps(NextProps) {
    if (NextProps.data?.id && this.props.data?.id != NextProps.data?.id) {
      this.loadData(NextProps.data?.id);
    }
  }
  onTableBtnExport() {
    let TableWrap = document.getElementById('table1');
    let Table = TableWrap.getElementsByTagName('table1')[0];
    const wb = XLSX.utils.table_to_book(Table);
    XLSX.writeFile(wb, this.props.record.Nav_Department.NAME + ".xlsx")
  }
  BtnClose = () => {
    if (typeof this.props.data.onCancel != "undefined" && typeof this.props.data.onCancel == 'function')
      this.props.data.onCancel();
  }
  //加载数据(传过来的参数都在props里面，包括登录信息)
  loadData = (id) => {
    var orgId = this.props.login ? this.props.login.OrgId : '';   //登录后有存储登录信息
    let json = initFilter(orgId);
    extendRule(json, 'ID', 1, id);    //ID=‘’，字段，等于，条件值， 1代表=，2代表不等于，3小于，4小于等于，5大于，6大于等于，7以开始的Like,8以结束的Like，9包含 
    extendInclude(json, 'Nav_Identifying');
    extendInclude(json, 'Nav_Area');
    extendInclude(json, 'Nav_OccupationalRisk');
    extendInclude(json, 'Nav_MonitorMeasures');
    extendInclude(json, 'Nav_EvaluateHazardIndividualMeasures.Nav_IndividualMeasures');
    extendInclude(json, 'Nav_EvaluateHazardEngineeringMeasures.Nav_EngineeringMeasures');
    extendInclude(json, 'Nav_EvaluateHazardManagementMeasures.Nav_ManagementMeasures');
    extendInclude(json, 'Nav_EvaluateHazardEducationMeasures.Nav_EducationMeasures');
    extendInclude(json, 'Nav_EvaluateHazardEmergencyMeasures.Nav_EmergencyMeasures');
    extendInclude(json, 'Nav_DepartmentLiable');
    extendInclude(json, 'Nav_PostLiable');
    this.props.dispatch({
      type: 'app/getDataByPost',
      payload: json,
      url: 'HM/EvaluateHazard/Get',
      onComplete: (ret) => {    //返回查询结果ret
        if (ret) {    //判断是否有记录
          this.setState({   //设置setState全局变量
            data: ret,      //将ret对象赋值给data, data供页面调用
          })
        }
      }
    });
  }
  //渲染页面
  render() {
    const { data } = this.state;
    const enums = this.props.app.enums;
    return <div>
      <div style={{ padding: '10px' }}>
        <table>
          <tr>
            <td><ReactToPrint trigger={() => <Button type={'default'} icon={'printer'} >打印</Button>} content={() => this.componentRef} /></td>
            <td><ExportToExcel fileName='职业危害库' tableId={'tableId' + this.props.data.id} /></td>
          </tr>
        </table>
      </div>
      <div ref={el => (this.componentRef = el)} id={'tableId' + this.props.data.id}>
        <h1 style={{ textAlign: 'center' }}>职业危害库</h1>
        {
          data ? <div style={{ width: '95%' }}>
            <Descriptions title="" size="middle" bordered>
              <Descriptions.Item label="辨识区域">{data.Nav_Area ? data.Nav_Area.NAME : ''}</Descriptions.Item>
              <Descriptions.Item label="职业危害位置">{data.HAZARD_POSITION}</Descriptions.Item>
              <Descriptions.Item label="职业危害名称">{data.Nav_Identifying ? data.Nav_Identifying.NAME : ''}</Descriptions.Item>
              <Descriptions.Item label="暴露时间">{data.EXPOSURE_TIME}</Descriptions.Item>
              <Descriptions.Item label="暴露限值">{data.EXPOSURE_LIMIT_VALUE}</Descriptions.Item>
              <Descriptions.Item label="暴露人数">{data.EXPOSURE_NUMBER}</Descriptions.Item>
              <Descriptions.Item label="可能造成的职业风险">{data.Nav_OccupationalRisk ? data.Nav_OccupationalRisk.NAME : ""}</Descriptions.Item>
              <Descriptions.Item label="危害等级">{data.HAZARD_LEVEL}</Descriptions.Item>
              <Descriptions.Item label="监测措施">{data.Nav_MonitorMeasures ? data.Nav_MonitorMeasures.NAME : ''}</Descriptions.Item>
              <Descriptions.Item label="个体防护措施">
                {
                  data.Nav_EvaluateHazardIndividualMeasures && data.Nav_EvaluateHazardIndividualMeasures.map((item, i) => {
                    if (i == (data.Nav_EvaluateHazardIndividualMeasures.length - 1)) {
                      return item.Nav_IndividualMeasures.NAME
                    } else {
                      return item.Nav_IndividualMeasures.NAME + '  '
                    }
                  })
                }
              </Descriptions.Item>
              <Descriptions.Item label="工程控制措施">
                {
                  data.Nav_EvaluateHazardEngineeringMeasures && data.Nav_EvaluateHazardEngineeringMeasures.map((item, i) => {
                    if (i == (data.Nav_EvaluateHazardEngineeringMeasures.length - 1)) {
                      return item.Nav_EngineeringMeasures.NAME
                    } else {
                      return item.Nav_EngineeringMeasures.NAME + '  '
                    }
                  })
                }
              </Descriptions.Item>
              <Descriptions.Item label="管理控制措施">
                {
                  data.Nav_EvaluateHazardManagementMeasures && data.Nav_EvaluateHazardManagementMeasures.map((item, i) => {
                    if (i == (data.Nav_EvaluateHazardManagementMeasures.length - 1)) {
                      return item.Nav_ManagementMeasures.NAME
                    } else {
                      return item.Nav_ManagementMeasures.NAME + '  '
                    }
                  })
                }
              </Descriptions.Item>
              <Descriptions.Item label="教育培训措施">
                {
                  data.Nav_EvaluateHazardEducationMeasures && data.Nav_EvaluateHazardEducationMeasures.map((item, i) => {
                    if (i == (data.Nav_EvaluateHazardEducationMeasures.length - 1)) {
                      return item.Nav_EducationMeasures.NAME
                    } else {
                      return item.Nav_EducationMeasures.NAME + '  '
                    }
                  })
                }
              </Descriptions.Item>
              <Descriptions.Item label="应急处置措施">
                {
                  data.Nav_EvaluateHazardEmergencyMeasures && data.Nav_EvaluateHazardEmergencyMeasures.map((item, i) => {
                    if (i == (data.Nav_EvaluateHazardEmergencyMeasures.length - 1)) {
                      return item.Nav_EmergencyMeasures.NAME
                    } else {
                      return item.Nav_EmergencyMeasures.NAME + '  '
                    }
                  })
                }
              </Descriptions.Item>

              <Descriptions.Item label="责任单位">{data.Nav_DepartmentLiable ? data.Nav_DepartmentLiable.NAME : ''}</Descriptions.Item>
              <Descriptions.Item label="责任岗位">{data.Nav_PostLiable ? data.Nav_PostLiable.NAME : ''}</Descriptions.Item>
              <Descriptions.Item label="管控层级">{enums.FMDepartmentType.enums[data.LEVEL]}</Descriptions.Item>
              <Descriptions.Item label="添加时间">{data.CREATE_TIME}</Descriptions.Item>
            </Descriptions>
          </div> : null
        }
      </div>
    </div>
  }
}
export default connect(({ login, app }) => ({ login, app }))(HM129ShowPrint) 
