import { Checkbox, message, Radio } from "antd/lib/index";
import { Button, Descriptions, Badge, Popconfirm, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, extendIgnoreDataRule, setDataFieldValue, guid, initQueryFilter, showFiles, showApprove, showUserSign, showUsersSign } from "../../../utils/common";
import { ExportToExcel } from '@woowalker/feui'
import styles from '../HI/StepForm.css';

import ReactToPrint from "react-to-print";
import moment from 'moment';
import config from "../../../config.js";
import XLSX from 'xlsx';
import { connect } from 'dva';
import FormPage from '../../FormPage'

class OH024ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            btndisplay: 'none',
            isCheck: false, //是否待办 审批
        }
    }
    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')

    }

    onClickApprove = (appVisible) => {
        const newtmpData = {
            data: { id: this.props.data.id, isShow: true, key: guid(), param: this.props.data.param, appVisible: appVisible, BtnAgreeDisplay: 'none' },
            formCode: 'PF123',
        }
        this.setState({
            tmpData: newtmpData,
        });
    }
    loadData = (id) => {
        var orgId = this.props.login ? this.props.login.OrgId : '';
        let json = initFilter(orgId);
        extendRule(json, 'ID', 1, id);
        extendInclude(json, 'Nav_User');
        extendInclude(json, 'Nav_Type');
        extendInclude(json, 'Nav_Detail.Nav_Rule.Nav_Type');
        extendInclude(json, 'Nav_Detail.Nav_Rule.Nav_DepartmentWorkshop');
        extendIgnoreDataRule(json)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'OH/HazardMonitorJob/Get',
            onComplete: (ret) => {
                if (ret) {
                    this.setState({
                        data: ret,
                    })
                }
            }
        });
    }
    render() {
        const { data } = this.state;
        const enums = this.props.data.enums ? this.props.data.enums : this.props.app.enums;
        return <div>
            <div style={{ padding: '10px' }}>
                <table>
                    <tr>
                        <td><Button onClick={() => this.onClickApprove(true)} style={{ marginRight: '8px' }} icon='export'>审批详情</Button></td>
                        <td><ReactToPrint trigger={() => <Button type={'default'} icon={'printer'} >打印</Button>} content={() => this.componentRef} pageStyle={"#tableId" + this.props.data.id + " { padding:0 40px;} img{width :120px}"} /></td>
                        <td><ExportToExcel fileName='职业危害监测' tableId={'tableId' + this.props.data.id} /></td>
                    </tr>
                </table>
            </div>
            <FormPage {...this.state.tmpData} />
            <div ref={el => (this.componentRef = el)} style={{ padding: '20px', paddingTop: '20px' }} id={'tableId' + this.props.data.id}>
                <h1 className={styles.showPrintHead}>职业危害监测</h1>
                {
                    data ? <div style={{ width: '95%' }}>
                        <Descriptions title='' size='middle' bordered>
                            <Descriptions.Item label="监测类型">{data?.Nav_Type?.NAME}</Descriptions.Item>
                            <Descriptions.Item label="监测时间">{data.MONITOR_TIME}</Descriptions.Item>
                            <Descriptions.Item label="监测人">{data.Nav_User?.NAME}</Descriptions.Item>
                            <Descriptions.Item label="仪器名称">{data.DEVICE_NAME}</Descriptions.Item>
                            <Descriptions.Item label="仪器规格/型号">{data.DEVICE_SPEC}</Descriptions.Item>
                            <Descriptions.Item label="监测方法">{data.MONITOR_METHOD}</Descriptions.Item>
                            {data.MONITOR_TYPE == 40 ? <Descriptions.Item label="取样人">{data.SAMPLE_PEOPLE}</Descriptions.Item> : null}

                        </Descriptions>
                    </div> : null
                }
                {
                    (data?.Nav_Detail && data?.Nav_Detail.length > 0) ?
                        <table className={styles.PrintFormLight}>
                            <tbody>
                                <tr>
                                    <td colSpan={17} className={styles.showPrintHead2}>职业危害监测明细</td>
                                </tr>
                                <tr className={styles.PrintFormLightTh}>
                                    <td>监测类别</td>
                                    <td>监测车间</td>
                                    <td>监测地点</td>

                                    {data.MONITOR_TYPE == 10 ? <td>扬尘来源</td> : null}
                                    {data.MONITOR_TYPE == 20 ? <td>噪声来源</td> : null}


                                    {data.MONITOR_TYPE == 10 || data.MONITOR_TYPE == 20 ? <td>作业状态</td> : null}
                                    {data.MONITOR_TYPE == 10 || data.MONITOR_TYPE == 20 ? <td>个体防护情况</td> : null}
                                    {data.MONITOR_TYPE == 10 || data.MONITOR_TYPE == 20 ? <td>最低测定结果</td> : null}
                                    {data.MONITOR_TYPE == 10 || data.MONITOR_TYPE == 20 ? <td>最高测定结果</td> : null}

                                    {data.MONITOR_TYPE == 30 ? <td>0.5m处监测值</td> : null}
                                    {data.MONITOR_TYPE == 30 ? <td>1m处监测值</td> : null}
                                    {data.MONITOR_TYPE == 30 ? <td>周围环境值</td> : null}

                                    {data.MONITOR_TYPE == 40 ? <td>取样时间</td> : null}
                                    {data.MONITOR_TYPE == 40 ? <td>化验时间</td> : null}
                                    {data.MONITOR_TYPE == 40 ? <td>监测数据</td> : null}
                                    <td>是否超限</td>
                                    {data.MONITOR_TYPE == 40 ? <td>化验部门</td> : null}
                                    {data.MONITOR_TYPE == 40 ? <td>化验人</td> : null}
                                </tr>
                                {data?.Nav_Detail?.map((item, i) => {
                                    return <tr>
                                        <td>{item.Nav_Rule?.Nav_Type.SMALL_TYPE} </td>
                                        <td>{item.Nav_Rule?.Nav_DepartmentWorkshop.NAME} </td>
                                        <td>{item.Nav_Rule?.LOCATION} </td>


                                        {data.MONITOR_TYPE == 10 || data.MONITOR_TYPE == 20 ? <td>{item.SAMPLE_SOURCE} </td> : null}
                                        {data.MONITOR_TYPE == 10 || data.MONITOR_TYPE == 20 ? <td>{enums.OHHazardMonitorJobStageEnum.enums[item.JOB_STATUS]} </td> : null}
                                        {data.MONITOR_TYPE == 10 || data.MONITOR_TYPE == 20 ? <td>{enums.OHHazardMonitorJobProtectEnum.enums[item.PROTECT_STATUS]} </td> : null}
                                        {data.MONITOR_TYPE == 10 || data.MONITOR_TYPE == 20 ? <td>{item.MONITOR_RESULT_LOW} </td> : null}
                                        {data.MONITOR_TYPE == 10 || data.MONITOR_TYPE == 20 ? <td>{item.MONITOR_RESULT_HIGH} </td> : null}

                                        {data.MONITOR_TYPE == 30 ? <td>{item.MONITOR_RESULT_05} </td> : null}
                                        {data.MONITOR_TYPE == 30 ? <td>{item.MONITOR_RESULT_10} </td> : null}
                                        {data.MONITOR_TYPE == 30 ? <td>{item.MONITOR_RESULT_AROUND} </td> : null}


                                        {data.MONITOR_TYPE == 40 ? <td>{item.SAMPLE_TIME} </td> : null}
                                        {data.MONITOR_TYPE == 40 ? <td>{item.TEST_TIME} </td> : null}
                                        {data.MONITOR_TYPE == 40 ? <td>{item.MONITOR_RESULT} </td> : null}
                                        <td>{enums.YesNoEnum.enums[item.OVER_LIMIT]} </td>
                                        {data.MONITOR_TYPE == 40 ? <td>{item.TEST_DEPARTMENT} </td> : null}
                                        {data.MONITOR_TYPE == 40 ? <td>{item.TESTER} </td> : null}

                                    </tr>
                                })}
                            </tbody>
                        </table>
                        : null
                }
            </div>
        </div>
    }
}
export default connect(({ login, app }) => ({ login, app }))(OH024ShowPrint) 
