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 HM047ShowPrint 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_Area');
    extendInclude(json, 'Nav_OperationStep');
    extendInclude(json, 'Nav_Identifying');
    extendInclude(json, 'Nav_Type');
    extendInclude(json, 'Nav_EvaluateRiskWorkduty.Nav_Post');
    extendInclude(json, 'Nav_EvaluateRiskEquipment.Nav_Equipment');
    extendInclude(json, 'Nav_EvaluateRiskFacilities.Nav_Facilities');
    extendInclude(json, 'Nav_EvaluateRiskIndividualMeasures.Nav_IndividualMeasures');
    extendInclude(json, 'Nav_EvaluateRiskEngineeringMeasures.Nav_EngineeringMeasures');
    extendInclude(json, 'Nav_EvaluateRiskManagementMeasures.Nav_ManagementMeasures');
    extendInclude(json, 'Nav_EvaluateRiskEducationMeasures.Nav_EducationMeasures');
    extendInclude(json, 'Nav_EvaluateRiskEmergencyMeasures.Nav_EmergencyMeasures');
    extendInclude(json, 'Nav_DepartmentLiable');
    extendInclude(json, 'Nav_PostLiable');
    this.props.dispatch({
      type: 'app/getDataByPost',
      payload: json,
      url: 'HM/EVALUATERISK/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: '20px' }}>
        <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.CODE}</Descriptions.Item> */}
              <Descriptions.Item label="风险名称">{data.NAME}</Descriptions.Item>
              <Descriptions.Item label="辨识区域">{data.Nav_Area ? data.Nav_Area.NAME : ''}</Descriptions.Item>
              <Descriptions.Item label="岗位">
                {
                  data.Nav_EvaluateRiskWorkduty && data.Nav_EvaluateRiskWorkduty.map((item, i) => {
                    if (i == (data.Nav_EvaluateRiskWorkduty.length - 1)) {
                      return item.Nav_Post.NAME
                    } else {
                      return item.Nav_Post.NAME + '  '
                    }
                  })
                }
              </Descriptions.Item>
              <Descriptions.Item label="作业环节">{data.Nav_OperationStep ? data.Nav_OperationStep.NAME : ''}</Descriptions.Item>
              <Descriptions.Item label="辨识对象">{data.Nav_Identifying ? data.Nav_Identifying.NAME : ''}</Descriptions.Item>
              <Descriptions.Item label="风险类别">{data.Nav_Type ? data.Nav_Type.NAME : ''}</Descriptions.Item>
              <Descriptions.Item label="风险地点">{data.RISK_PLACE}</Descriptions.Item>
              <Descriptions.Item label="设备设施">
                {
                  data.Nav_EvaluateRiskEquipment && data.Nav_EvaluateRiskEquipment.map((item, i) => {
                    if (i == (data.Nav_EvaluateRiskEquipment.length - 1)) {
                      return item.Nav_Equipment.NAME
                    } else {
                      return item.Nav_Equipment.NAME + ' '
                    }
                  })
                }
              </Descriptions.Item>
              {/* <Descriptions.Item label="相关设施">
                {
                  data.Nav_EvaluateRiskFacilities && data.Nav_EvaluateRiskFacilities.map((item, i) => {
                    if (i == (data.Nav_EvaluateRiskFacilities.length - 1)) {
                      return item.Nav_Facilities.NAME
                    } else {
                      return item.Nav_Facilities.NAME + ' '
                    }
                  })
                }
              </Descriptions.Item> */}
              <Descriptions.Item label="风险评价L">{data.EVALUATE_L}</Descriptions.Item>
              <Descriptions.Item label="风险评价E">{data.EVALUATE_E}</Descriptions.Item>
              <Descriptions.Item label="风险评价C">{data.EVALUATE_C}</Descriptions.Item>
              <Descriptions.Item label="分值D">{data.EVALUATE_SCORE}</Descriptions.Item>
              <Descriptions.Item label="风险等级">{enums.EvaluateLevelEnum.enums[data.EVALUATE_LEVEL]}</Descriptions.Item>
              <Descriptions.Item label="个体防护措施">
              {
                  data.Nav_EvaluateRiskIndividualMeasures && data.Nav_EvaluateRiskIndividualMeasures.map((item, i) => {
                    if (i == (data.Nav_EvaluateRiskIndividualMeasures.length - 1)) {
                      return item.Nav_IndividualMeasures.NAME
                    } else {
                      return item.Nav_IndividualMeasures.NAME + '  '
                    }
                  })
                }
              </Descriptions.Item>
              <Descriptions.Item label="工程控制措施">
              {
                  data.Nav_EvaluateRiskEngineeringMeasures && data.Nav_EvaluateRiskEngineeringMeasures.map((item, i) => {
                    if (i == (data.Nav_EvaluateRiskEngineeringMeasures.length - 1)) {
                      return item.Nav_EngineeringMeasures.NAME
                    } else {
                      return item.Nav_EngineeringMeasures.NAME + '  '
                    }
                  })
                }
                </Descriptions.Item>
              <Descriptions.Item label="管理控制措施">
              {
                  data.Nav_EvaluateRiskManagementMeasures && data.Nav_EvaluateRiskManagementMeasures.map((item, i) => {
                    if (i == (data.Nav_EvaluateRiskManagementMeasures.length - 1)) {
                      return item.Nav_ManagementMeasures.NAME
                    } else {
                      return item.Nav_ManagementMeasures.NAME + '  '
                    }
                  })
                }
                </Descriptions.Item>
              <Descriptions.Item label="教育培训措施">
              {
                  data.Nav_EvaluateRiskEducationMeasures && data.Nav_EvaluateRiskEducationMeasures.map((item, i) => {
                    if (i == (data.Nav_EvaluateRiskEducationMeasures.length - 1)) {
                      return item.Nav_EducationMeasures.NAME
                    } else {
                      return item.Nav_EducationMeasures.NAME + '  '
                    }
                  })
                }
                </Descriptions.Item>
              <Descriptions.Item label="应急处置措施">
              {
                  data.Nav_EvaluateRiskEmergencyMeasures && data.Nav_EvaluateRiskEmergencyMeasures.map((item, i) => {
                    if (i == (data.Nav_EvaluateRiskEmergencyMeasures.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 }))(HM047ShowPrint) 
