import { Button, Descriptions, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { connect } from 'dva';
import { initFilter, extendRule, extendInclude, setDataFieldValue, guid, initQueryFilter } from "../../../utils/common";
import ReactToPrint from "react-to-print";
import styles from '../HI/StepForm.css';
import config from "../../../config";
import XLSX from 'xlsx';
import FormPage from '../../../components/FormPage'
class HM121ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            tmpData: {},
            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);
        }
    }

    loadData = (dataId) => {
        let json = initFilter(this.props.login.OrgId);
        extendRule(json, 'ID', 1, dataId);
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'HM/HMEvaluateResult/FullGet',
            onComplete: (ret) => {
                if (this.props.data && this.props.data.loadDataDeal) {
                    this.props.data.loadDataDeal(1);
                }
                this.onClickApprove(false);
                if (ret) {
                    this.setState({ data: ret })
                }
            }
        });
    }
    onClickApprove = (appVisible) => {
        const newtmpData = {
            data: { id: this.props.data.id, isShow: true, key: guid(), appVisible: appVisible, BtnAgreeDisplay: 'inline' },
            formCode: "PF123",
        }
        this.setState({
            tmpData: newtmpData,
        });
    }
    getChild = (data) => {
        const enums = this.props.app.enums;
        if (data.EVALUATE_TYPE === 2) {
            return <>
                <tr>
                    <td colSpan={1} rowSpan={2}>序号</td>
                    <td colSpan={1} rowSpan={2}>辨识区域</td>
                    <td colSpan={1} rowSpan={2}>辨识对象</td>
                    <td colSpan={1} rowSpan={2}>风险类别</td>
                    <td colSpan={3} rowSpan={2}>风险名称</td>
                    <td colSpan={1} rowSpan={2}>风险地点</td>
                    <td colSpan={2} rowSpan={2}>岗位</td>
                    <td colSpan={1} rowSpan={2}>设备设施</td>
                    {/* <td colSpan={1} rowSpan={2}>相关设施</td> */}
                    <td colSpan={1} rowSpan={2}>作业环节</td>
                    <td colSpan={4} rowSpan={1}>风险评价</td>
                    <td colSpan={1} rowSpan={2}>风险等级</td>
                    <td colSpan={10} rowSpan={1}>风险控制</td>
                    {/* <td colSpan={1} rowSpan={2}>责任单位</td>
                    <td colSpan={1} rowSpan={2}>责任岗位</td> */}
                    <td colSpan={1} rowSpan={2}>责任人</td>

                </tr>
                <tr>
                    <td colSpan={1} rowSpan={1}>L</td>
                    <td colSpan={1} rowSpan={1}>E</td>
                    <td colSpan={1} rowSpan={1}>C</td>
                    <td colSpan={1} rowSpan={1}>D</td>
                    <td colSpan={2} rowSpan={1}>个体防护措施</td>
                    <td colSpan={2} rowSpan={1}>工程控制措施</td>
                    <td colSpan={2} rowSpan={1}>管理控制措施</td>
                    <td colSpan={2} rowSpan={1}>教育培训措施</td>
                    <td colSpan={2} rowSpan={1}>应急处置措施</td>
                </tr>
                {
                    data.Nav_Risks && data.Nav_Risks.map((item, index) => {
                        return <tr>
                            <td colSpan={1} rowSpan={1}>{index + 1}</td>
                            <td colSpan={1} rowSpan={1}>{item.Nav_Area ? item.Nav_Area.NAME : ''}</td>
                            <td colSpan={1} rowSpan={1}>{item.Nav_Identifying ? item.Nav_Identifying.NAME : ''}</td>
                            <td colSpan={1} rowSpan={1}>{item.Nav_Type ? item.Nav_Type.NAME : ''}</td>
                            <td colSpan={3} rowSpan={1}>{item.NAME}</td>
                            <td colSpan={1} rowSpan={1}>{item.RISK_PLACE}</td>
                            <td colSpan={2} rowSpan={1}>{
                                item.Nav_EvaluateRiskWorkduty && item.Nav_EvaluateRiskWorkduty.map((ite, i) => {
                                    if (i == (item.Nav_EvaluateRiskWorkduty.length - 1)) {
                                        return ite.Nav_Post.NAME
                                    } else {
                                        return ite.Nav_Post.NAME + '  '
                                    }
                                })
                            }</td>
                            <td colSpan={1} rowSpan={1}>{
                                item.Nav_EvaluateRiskEquipment && item.Nav_EvaluateRiskEquipment.map((ite, i) => {
                                    if (i == (item.Nav_EvaluateRiskEquipment.length - 1)) {
                                        return ite.Nav_Equipment.NAME
                                    } else {
                                        return ite.Nav_Equipment.NAME + '  '
                                    }
                                })}</td>
                            {/* <td colSpan={1} rowSpan={1}>{
                                item.Nav_EvaluateRiskFacilities && item.Nav_EvaluateRiskFacilities.map((ite, i) => {
                                    if (i == (item.Nav_EvaluateRiskFacilities.length - 1)) {
                                        return ite.Nav_Facilities.NAME
                                    } else {
                                        return ite.Nav_Facilities.NAME + '  '
                                    }
                                })}</td> */}
                            <td colSpan={1} rowSpan={1}>{item.Nav_OperationStep ? item.Nav_OperationStep.NAME : ''}</td>
                            <td colSpan={1} rowSpan={1}>{item.EVALUATE_L}</td>
                            <td colSpan={1} rowSpan={1}>{item.EVALUATE_E}</td>
                            <td colSpan={1} rowSpan={1}>{item.EVALUATE_C}</td>
                            <td colSpan={1} rowSpan={1}>{item.EVALUATE_SCORE}</td>

                            <td colSpan={1} rowSpan={1}>{enums.EvaluateLevelEnum.enums[item.EVALUATE_LEVEL]}</td>
                            <td colSpan={2} rowSpan={1}>{
                                item.Nav_EvaluateRiskIndividualMeasures && item.Nav_EvaluateRiskIndividualMeasures.map((ite, i) => {
                                    if (i == (item.Nav_EvaluateRiskIndividualMeasures.length - 1)) {
                                        return ite.Nav_IndividualMeasures.NAME
                                    } else {
                                        return ite.Nav_IndividualMeasures.NAME + '  '
                                    }
                                })}</td>
                            <td colSpan={2} rowSpan={1}>{
                                item.Nav_EvaluateRiskEngineeringMeasures && item.Nav_EvaluateRiskEngineeringMeasures.map((ite, i) => {
                                    if (i == (item.Nav_EvaluateRiskEngineeringMeasures.length - 1)) {
                                        return ite.Nav_EngineeringMeasures.NAME
                                    } else {
                                        return ite.Nav_EngineeringMeasures.NAME + '  '
                                    }
                                })}</td>
                            <td colSpan={2} rowSpan={1}>{
                                item.Nav_EvaluateRiskManagementMeasures && item.Nav_EvaluateRiskManagementMeasures.map((ite, i) => {
                                    if (i == (item.Nav_EvaluateRiskManagementMeasures.length - 1)) {
                                        return ite.Nav_ManagementMeasures.NAME
                                    } else {
                                        return ite.Nav_ManagementMeasures.NAME + '  '
                                    }
                                })}</td>
                            <td colSpan={2} rowSpan={1}>{
                                item.Nav_EvaluateRiskEducationMeasures && item.Nav_EvaluateRiskEducationMeasures.map((ite, i) => {
                                    if (i == (item.Nav_EvaluateRiskEducationMeasures.length - 1)) {
                                        return ite.Nav_EducationMeasures.NAME
                                    } else {
                                        return ite.Nav_EducationMeasures.NAME + '  '
                                    }
                                })}</td>
                            <td colSpan={2} rowSpan={1}>{
                                item.Nav_EvaluateRiskEmergencyMeasures && item.Nav_EvaluateRiskEmergencyMeasures.map((ite, i) => {
                                    if (i == (item.Nav_EvaluateRiskEmergencyMeasures.length - 1)) {
                                        return ite.Nav_EmergencyMeasures.NAME
                                    } else {
                                        return ite.Nav_EmergencyMeasures.NAME + '  '
                                    }
                                })}</td>
                            {/* <td colSpan={1} rowSpan={1}>{item.Nav_DepartmentLiable ? item.Nav_DepartmentLiable.NAME : ''}</td> */}
                            <td colSpan={1} rowSpan={1}>{item.Nav_LiableUser ? item.Nav_LiableUser.NAME : ''}</td>
                        </tr>
                    })
                }

            </>
        } else {
            return <>
                <tr>
                    <td colSpan={1} rowSpan={1}>序号</td>
                    <td colSpan={1} rowSpan={1}>职业危害名称</td>
                    <td colSpan={1} rowSpan={1}>职业危害位置</td>
                    <td colSpan={1} rowSpan={1}>暴露时间</td>
                    <td colSpan={1} rowSpan={1}>暴露限值</td>
                    <td colSpan={1} rowSpan={1}>暴露限值单位</td>
                    <td colSpan={1} rowSpan={1}>暴露人数</td>
                    <td colSpan={1} rowSpan={1}>可能造成的职业风险</td>
                    <td colSpan={1} rowSpan={1}>危害等级</td>
                    <td colSpan={3} rowSpan={1}>监测措施</td>
                    <td colSpan={3} rowSpan={1}>个体防护措施</td>
                    <td colSpan={3} rowSpan={1}>工程控制措施</td>
                    <td colSpan={3} rowSpan={1}>管理控制措施</td>
                    <td colSpan={3} rowSpan={1}>教育培训措施</td>
                    <td colSpan={3} rowSpan={1}>应急处置措施</td>
                    <td colSpan={1} rowSpan={1}>责任单位</td>
                    <td colSpan={1} rowSpan={1}>责任岗位</td>
                </tr>
                {
                    data.Nav_Hazards && data.Nav_Hazards.map((item, index) => {
                        return <tr>
                            <td colSpan={1} rowSpan={1}>{index + 1}</td>
                            <td colSpan={1} rowSpan={1}>{item.Nav_Identifying.NAME}</td>
                            <td colSpan={1} rowSpan={1}>{item.HAZARD_POSITION}</td>
                            <td colSpan={1} rowSpan={1}>{item.EXPOSURE_TIME}</td>
                            <td colSpan={1} rowSpan={1}>{item.EXPOSURE_LIMIT_VALUE}</td>
                            <td colSpan={1} rowSpan={1}>{item.Nav_Identifying ? item.Nav_Identifying.LIMIT_UNIT : ''}</td>
                            <td colSpan={1} rowSpan={1}>{item.EXPOSURE_NUMBER}</td>
                            <td colSpan={1} rowSpan={1}>{item.Nav_OccupationalRisk ? item.Nav_OccupationalRisk.NAME : ''}</td>
                            <td colSpan={1} rowSpan={1}>{enums.HazardLevelEnum.enums[item.HAZARD_LEVEL]}</td>
                            <td colSpan={3} rowSpan={1}>{item.Nav_MonitorMeasures ? item.Nav_MonitorMeasures.NAME : ''}</td>
                            <td colSpan={3} rowSpan={1}>{
                                item.Nav_EvaluateHazardIndividualMeasures && item.Nav_EvaluateHazardIndividualMeasures.map((ite, i) => {
                                    if (i == (item.Nav_EvaluateHazardIndividualMeasures.length - 1)) {
                                        return ite.Nav_IndividualMeasures.NAME
                                    } else {
                                        return ite.Nav_IndividualMeasures.NAME + '  '
                                    }
                                })}</td>
                            <td colSpan={3} rowSpan={1}>{
                                item.Nav_EvaluateHazardEngineeringMeasures && item.Nav_EvaluateHazardEngineeringMeasures.map((ite, i) => {
                                    if (i == (item.Nav_EvaluateHazardEngineeringMeasures.length - 1)) {
                                        return ite.Nav_EngineeringMeasures.NAME
                                    } else {
                                        return ite.Nav_EngineeringMeasures.NAME + '  '
                                    }
                                })}</td>
                            <td colSpan={3} rowSpan={1}>{
                                item.Nav_EvaluateHazardManagementMeasures && item.Nav_EvaluateHazardManagementMeasures.map((ite, i) => {
                                    if (i == (item.Nav_EvaluateHazardManagementMeasures.length - 1)) {
                                        return ite.Nav_ManagementMeasures.NAME
                                    } else {
                                        return ite.Nav_ManagementMeasures.NAME + '  '
                                    }
                                })}</td>
                            <td colSpan={3} rowSpan={1}>{
                                item.Nav_EvaluateHazardEducationMeasures && item.Nav_EvaluateHazardEducationMeasures.map((ite, i) => {
                                    if (i == (item.Nav_EvaluateHazardEducationMeasures.length - 1)) {
                                        return ite.Nav_EducationMeasures.NAME
                                    } else {
                                        return ite.Nav_EducationMeasures.NAME + '  '
                                    }
                                })}</td>
                            <td colSpan={3} rowSpan={1}>{
                                item.Nav_EvaluateHazardEmergencyMeasures && item.Nav_EvaluateHazardEmergencyMeasures.map((ite, i) => {
                                    if (i == (item.Nav_EvaluateHazardEmergencyMeasures.length - 1)) {
                                        return ite.Nav_EmergencyMeasures.NAME
                                    } else {
                                        return ite.Nav_EmergencyMeasures.NAME + '  '
                                    }
                                })}</td>
                            <td colSpan={1} rowSpan={1}>{item.Nav_DepartmentLiable ? item.Nav_DepartmentLiable.NAME : ''}</td>
                            <td colSpan={1} rowSpan={1}>{item.Nav_PostLiable ? item.Nav_PostLiable.NAME : ''}</td>

                        </tr>
                    })
                }
            </>
        }
    }

    onTableBtnExport() {
        let TableWrap = document.getElementById('tableId' + this.props.data.id);
        let Table = TableWrap.getElementsByTagName('table')[0];
        const wb = XLSX.utils.table_to_book(Table);
        XLSX.writeFile(wb, this.props.record.Nav_Department.NAME + ".xlsx")
    }
    render() {
        const { data } = this.state;
        const content = ['', '职业危害辨识', '危险源与风险辨识'];
        return <div>
            <div style={{ padding: '10px' }}>
                <Button onClick={() => this.onClickApprove(true)} icon="export">审批详情</Button>
                <ReactToPrint
                    trigger={() => <Button type={'default'} icon={'printer'} >打印</Button>}
                    content={() => this.componentRef}
                />
                <Button onClick={() => this.onTableBtnExport()} icon="export" style={{ marginLeft: '8px' }}>导出</Button>
            </div>
            <div ref={el => (this.componentRef = el)} id={'tableId' + this.props.data.id} style={{ padding: '20px' }}>
                <h1 style={{ textAlign: 'center' }}>评价记录表</h1>

                {
                    data ? <div>
                        <Descriptions size="middle" bordered>
                            <Descriptions.Item label="编号">{data.CODE}</Descriptions.Item>
                            <Descriptions.Item label="评价内容">{content[data.EVALUATE_TYPE]}</Descriptions.Item>
                            <Descriptions.Item label="评价人员">{data.Nav_NoteTaker ? data.Nav_NoteTaker.NAME : ''}</Descriptions.Item>
                            <Descriptions.Item label="开始时间">{data.START_TIME}</Descriptions.Item>
                            <Descriptions.Item label="结束时间">{data.END_TIME}</Descriptions.Item>
                            <Descriptions.Item label="附件"> {
                                data.Nav_Files && data.Nav_Files.map((item, i) => {
                                    return <a width={'20%'} title={item.Nav_ImgFile.FILE_NAME} target='_blank' href={config.picServerHost + item.Nav_ImgFile.Nav_File.FILE_PATH} >{item.Nav_ImgFile.FILE_NAME} </a>
                                })
                            }</Descriptions.Item>
                            <Descriptions.Item label="评价要求">{data.Nav_Requirement ? data.Nav_Requirement.NAME : ''}</Descriptions.Item>

                        </Descriptions>
                        <table style={{ width: '100%', textAlign: 'center' }} className={styles.PrintForm1}>
                            {
                                this.getChild(data)
                            }
                        </table>
                        <FormPage {...this.state.tmpData} />
                    </div> : null
                }
            </div>
        </div>
    }

}

export default connect(({ login, app }) => ({ login, app }))(HM121ShowPrint) 