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 { ExportToExcel } from '@woowalker/feui'
import ReactToPrint from "react-to-print";
import moment from 'moment';
import XLSX from 'xlsx';
import { connect } from 'dva';
class HM040ShowPrint 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_OperationStep');
    // extendInclude(json, 'Nav_EvaluateRisk.Nav_EvaluateRiskWorkduty.Nav_Post');

    this.props.dispatch({
      type: 'app/getDataByPost',
      payload: json,
      url: 'HM/HMOperationLink/GetEdit',
      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='table1' /></td> */}
          </tr>
        </table> 
      </div>
      <div ref={el => (this.componentRef = el)} style={{ padding: '20px', paddingTop: '20px' }} id={'tableId' + this.props.id}>
        <h1 style={{ textAlign: 'center', margin: '15px' }}>作业任务库</h1>
        {
          data ? <div style={{ width: '95%'}}>
            {/* <table style={{ width: '100%', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }}>
                        <tr>
                            <td colSpan={5} rowSpan={1}>通知名称</td>
                            <td colSpan={8} rowSpan={1} style={{ width: '50%' }}>{data.SafeConfirmsStr.replaceAll("|","               ")}<br/><br/></td>
                        </tr>
                        </table> */}
            <Descriptions title="" size="middle" bordered>
              <Descriptions.Item label="编号">{data.CODE}</Descriptions.Item>
              {/* <Descriptions.Item label="名称">{data.NAME}</Descriptions.Item> */}
              <Descriptions.Item label="作业环节">{data.Nav_OperationStep ?data.Nav_OperationStep.NAME: ''}</Descriptions.Item>
              {/* <Descriptions.Item label="危险源/风险名称">
              {
                  data.Nav_OperationLinkEvaluateRisk && data.Nav_OperationLinkEvaluateRisk.map((item, i) => {
                      return <tr><label> {(i+1)+"."+item.Nav_EvaluateRisk?.NAME}</label></tr>
                  })
                }</Descriptions.Item> */}
              <Descriptions.Item label="相关岗位">
              {
                  data.POST
                }
                 {/* {
                  data.Nav_OperationLinkEvaluateRisk.Nav_EvaluateRisk && data.Nav_OperationLinkEvaluateRisk.Nav_EvaluateRisk.Nav_EvaluateRiskWorkduty && data.Nav_OperationLinkEvaluateRisk.Nav_EvaluateRisk.Nav_EvaluateRiskWorkduty.map((item, i) => {
                    console.log(item)
                    if (i == (data.Nav_OperationLinkEvaluateRisk.Nav_EvaluateRisk.Nav_EvaluateRiskWorkduty.length - 1)) {
                      return item.Nav_Post.NAME
                    } else {
                      return item.Nav_Post.NAME + ','
                    }
                  })
                } */}
              </Descriptions.Item>
              <Descriptions.Item label="是否为关键任务">{enums.ISImportantEnum.enums[data.IS_IMPORTANT]}</Descriptions.Item>
              <Descriptions.Item label="作业许可类别">{data.Nav_WorkPermitType?.NAME}</Descriptions.Item>
              <Descriptions.Item label="许可作业审批层级">{enums.HMOperationTaskAuditEnum.enums[data.AUDIT_LEVEL]}</Descriptions.Item>
              <Descriptions.Item label="作业前安全确认">
              {
                  data.Nav_SafeConfirms && data.Nav_SafeConfirms.map((item, i) => {
                    return <tr><label> {(i+1)+"."+item.NAME}</label></tr>
                  })
                }
              </Descriptions.Item>
              <Descriptions.Item label="作业流程及安全措施">{
                  data.Nav_SafeMeasures && data.Nav_SafeMeasures.map((item, i) => {
                    return <tr><label> {(i+1)+"."+item.NAME}</label></tr>
                  })
                }</Descriptions.Item>
              <Descriptions.Item label="作业后处理措施">{
                  data.Nav_DealMeasures && data.Nav_DealMeasures.map((item, i) => {
                    return <tr><label> {(i+1)+"."+item.NAME}</label></tr>
                  })
                }</Descriptions.Item>
              <Descriptions.Item label="添加时间">{data.CREATE_TIME}</Descriptions.Item>
              <Descriptions.Item label="状态">{enums.STATUSEnum.enums[data.STATUS]}</Descriptions.Item>
              {/* {data.SafeConfirmsStr?.replaceAll("|","\r\n")} */}
            </Descriptions>
          </div> : null
        }
      </div>
    </div>
  }
}
export default connect(({ login,app }) => ({ login,app }))(HM040ShowPrint) 
