import { Button, Descriptions,Popconfirm, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, extendIgnoreDataRule,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 { connect } from 'dva';
import moment from 'moment';
import { message } from "antd/lib/index";

class HM123ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            BtnAgreeDisplay: 'none',
            details:[]
        };
    };

    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);
        }
    }
    onPersonalAgree() {
        let json = initFilter(this.props.login.OrgId,this.props.data.id);
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'HM/HMFilePublish/ChangeStatus',
            payload: json,
            onComplete: (ret) => {
                if (ret) {
                    message.success('成功！');
                    this.setState({ BtnSignDisplay: 'none' })
                    this.BtnClose();
                }
            }
        })
    }
    BtnClose = () => {
        if (typeof this.props.data.onCancel != "undefined"&&typeof this.props.data.onCancel == 'function')
            this.props.data.onCancel();
    }

    loadData = (dataId) => {
        let that=this;
        let json = initFilter(that.props.login.OrgId);
        extendRule(json, 'ID', 1, dataId);
        extendIgnoreDataRule(json)
        that.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'HM/HMFilePublish/GetEdit',
            onComplete: (ret) => {
                if (ret) {
                    let detailTemp=[];
                    if(ret.Nav_License_Details && ret.Nav_License_Details.length!==0)
                    {
                        ret.Nav_License_Details.forEach(item => {
                            let objIdentifying = {
                                Nav_Area:item.Nav_Area,
                                Nav_OperationStep:item.Nav_OperationStep,
                                Nav_DetailRisk: item.Nav_DetailRisk,
                                SafeConfirmsStr:item.SafeConfirmsStr,
                                SafeMeasuresStr:item.SafeMeasuresStr,
                                DealMeasuresStr:item.DealMeasuresStr,
                                AUDIT_LEVEL:item.AUDIT_LEVEL,
                                Nav_WorkPermitType:item.Nav_WorkPermitType,
                                Nav_Files:item.Nav_Files,
                                CYCLE_TYPE:item.CYCLE_TYPE
                            }
                            detailTemp.push(objIdentifying);
                          })
                    }
                    if(ret.Nav_Crucial_Details && ret.Nav_Crucial_Details.length!==0)
                    {
                        ret.Nav_Crucial_Details.forEach(item => {
                            let objIdentifying = {
                                Nav_Area:item.Nav_Area,
                                Nav_OperationStep:item.Nav_OperationStep,
                                Nav_DetailRisk: item.Nav_DetailRisk,
                                SafeConfirmsStr:item.SafeConfirmsStr,
                                SafeMeasuresStr:item.SafeMeasuresStr,
                                DealMeasuresStr:item.DealMeasuresStr,
                                Nav_WorkPermitType:item.Nav_WorkPermitType,
                                AUDIT_LEVEL:item.AUDIT_LEVEL,
                                Nav_Files:item.Nav_Files,
                                CYCLE_TYPE:item.CYCLE_TYPE
                            }
                            detailTemp.push(objIdentifying);
                          })
                    }
                    if(ret.Nav_Ordinary_Details && ret.Nav_Ordinary_Details.length!==0)
                    {
                        ret.Nav_Ordinary_Details.forEach(item => {
                            let objIdentifying = {
                                Nav_Area:item.Nav_Area,
                                Nav_OperationStep:item.Nav_OperationStep,
                                Nav_DetailRisk: item.Nav_DetailRisk,
                                SafeConfirmsStr:item.SafeConfirmsStr,
                                SafeMeasuresStr:item.SafeMeasuresStr,
                                DealMeasuresStr:item.DealMeasuresStr,
                                Nav_WorkPermitType:item.Nav_WorkPermitType,
                                AUDIT_LEVEL:item.AUDIT_LEVEL,
                                Nav_Files:item.Nav_Files,
                                CYCLE_TYPE:item.CYCLE_TYPE
                            }
                            detailTemp.push(objIdentifying);
                          })
                    }
                    that.setState({ data: ret,details:detailTemp});
                    if (that.props.data.tableKey==="2" ||that.props.data.tableKey===undefined) {
                        that.setState({ BtnAgreeDisplay: 'none' })
                    }else{
                        that.setState({ BtnAgreeDisplay: 'inline' })
                    }
                }
                else
                { message.error('请先尝试刷新，若仍然加载失败，请联系管理员排查!');}
            }
        });
    }

    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, "作业任务分析文件发布.xlsx")
    }

    render() {
        const { data, details} = this.state;
        const enums=this.props.app.enums;
        return <div>
            <div style={{ padding: '10px' }}>
                <ReactToPrint
                    trigger={() => <Button type={'default'} icon={'printer'} style={{ marginLeft: '8px', display: data && data.IS_PUBLISH === 1?"inline":"none" }}>打印</Button>}
                    content={() => this.componentRef}
                    pageStyle=".printDIV { padding:0 40px;} img{width :120px}"
                />
                <Button onClick={() => this.onTableBtnExport()} icon="export" style={{ marginLeft: '8px',display: data && data.IS_PUBLISH === 1?"inline":"none"  }}>导出</Button>
                <Button type="primary" style={{marginLeft: '8px', display: this.state.BtnAgreeDisplay }} onClick={() => this.onPersonalAgree()} icon="check" >已阅</Button>
            </div>
            <div ref={el => (this.componentRef = el)} style={{ padding: '20px' }} id={'tableId' + this.props.data.id} className="printDIV">
                <h1 style={{ textAlign: 'center', margin: '15px' }}>作业任务分析文件发布</h1>
                {
                     data ?
                     <table style={{ width: '100%', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                        <tbody>
                            <tr>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>文件名称</td>
                                <td colSpan={3} rowSpan={1}>{data.NAME}</td>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>发布人员</td>
                                <td colSpan={3} rowSpan={1}>{data.Nav_User ? data.Nav_User.NAME : ''}</td>
                            </tr>
                            <tr>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>发布时间</td>
                                <td colSpan={3} rowSpan={1}>{data.PUBLISH_DATE?moment(data.PUBLISH_DATE).format('YYYY-MM-DD'):null}</td>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>发布部门</td>
                                <td colSpan={3} rowSpan={1}>{data.Nav_User.Nav_Department ? data.Nav_User.Nav_Department.NAME : ''}</td>
                            </tr>
                            <tr>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>文件编号</td>
                                <td colSpan={3} rowSpan={1}>{data.CODE}</td>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>文件版次</td>
                                <td colSpan={3} rowSpan={1}>{data.EDITION}</td>
                            </tr>
                            <tr>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>文件密级</td>
                                <td colSpan={3} rowSpan={1}>{enums.SecretLevelEnum.enums[data.SecretLevel]}</td>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>发布范围</td>
                                <td colSpan={3} rowSpan={1}>{
                                data.Nav_Area && data.Nav_Area.map((item, i) => {
                                    if (i == (data.Nav_Area.length - 1)) {
                                        return item.Nav_Department.NAME
                                    } else {
                                        return item.Nav_Department.NAME + '  '
                                    }
                                })
                            }</td>
                            </tr>
                            <tr>
                                        <td  colSpan={1} className={styles.fontBold}>序号</td>
                                        {/* <td width='150px' className={styles.fontBold}>辨识区域</td> */}
                                            <td  colSpan={1} className={styles.fontBold}>任务名称</td>
                                            <td colSpan={1}  className={styles.fontBold}>作业周期</td>
                                            {/* <td  className={styles.fontBold}>涉及风险</td> */}
                                            <td colSpan={1}  className={styles.fontBold}>作业前安全确认</td>
                                            <td colSpan={1}  className={styles.fontBold}>作业流程及安全措施</td>
                                            <td colSpan={1}  className={styles.fontBold}>作业后处理措施</td>
                                            <td colSpan={1} className={styles.fontBold}>作业许可类别</td>
                                            <td  colSpan={1} className={styles.fontBold}>审批层级</td>
                                            <td  colSpan={2}  className={styles.fontBold}>附件</td>
                                        </tr>
                                        {
                                            details.map((item,i)=>{
                                                return <tr>
                                                     <td colSpan={1} >
                                                        {i+1}
                                                    </td>
                                                    {/* <td>
                                                        {item.Nav_Area?.NAME}
                                                    </td> */}
                                                    <td colSpan={1} >
                                                        {item.Nav_OperationStep?.NAME}
                                                    </td>
                                                    <td colSpan={1} >
                                             {enums.HMCycleTypeEnum.enums[item.CYCLE_TYPE]}
                                            </td>
                                                    {/* <td>
                                                    {
                                item.Nav_DetailRisk && item.Nav_DetailRisk.map((item, i) => {
                                    return <tr><label> {(i+1)+"."+item.Nav_EvaluateRisk?.NAME}</label></tr>
                                })
                            }
                                                    </td> */}
                                                    <td colSpan={1} >
                                                        {item.SafeConfirmsStr}
                                                    </td>
                                                    <td colSpan={1} >
                                                        {item.SafeMeasuresStr}
                                                    </td>
                                                    <td colSpan={1} >
                                                        {item.DealMeasuresStr}
                                                    </td>
                                                    <td colSpan={1} >
                                                        {item.Nav_WorkPermitType?.NAME}
                                                    </td>
                                                    <td colSpan={1} >
                                                        {enums.HMOperationTaskAuditEnum.enums[item.AUDIT_LEVEL]}
                                                    </td>
                                                    <td colSpan={2} > 
                                                    {
                                item.Nav_Files && item.Nav_Files.map((t, i) => {
                                    return <a width={'20%'} title={t.Nav_ImgFile.FILE_NAME} target='_blank' href={config.picServerHost + t.Nav_ImgFile.Nav_File.FILE_PATH} >{t.Nav_ImgFile.FILE_NAME} <br></br></a>
                                })
                            }
                                                    </td>
                                                </tr>
                                            })
                                        }
                        </tbody>
                     </table>:null
                }
                
            </div>
        </div>
    }

}

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