import { message } from "antd/lib/index";
import { Button, 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 styles from  '../../CustomPages/HI/StepForm.css';
import XLSX from 'xlsx';
import { connect } from 'dva';
import moment from 'moment';
import config from "../../../config";

class LR007ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null
        };
    };

    componentDidMount() {
        if (window.navigator.userAgent.indexOf("Windows") < 1) {
            this.setState({ isMobile: true })
        }
        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);
        }
    }

    BtnClose = () => {
        if (typeof this.props.closeFn != "undefined")
            this.props.closeFn();
        else if (typeof this.props.data.closeTab == 'function') {
            this.props.data.closeTab(this.props.data.id)
        }  else if (typeof this.props.onCancel === "function"){
            this.props.onCancel();
        }
    }

    loadData = (dataId) => {
        const json = initFilter(this.props.login.OrgId);
        extendRule(json, 'ID', 1, dataId);
        extendInclude(json, 'Nav_IntegrateDetail.Nav_Law')
        extendInclude(json, 'Nav_IntegrateDetail.Nav_SCSystem')
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'LR/LawIntegrate/Get',
            payload: json,
            onComplete: (ret) => {
                if (ret) {
                    this.setState({ data: ret })
                }
            }
        });
    }
    onTableBtnExport() {
        let TableWrap = document.getElementById('tableId' + this.props.login.OrgId);
        let Table = TableWrap.getElementsByTagName('table')[0];
        const wb = XLSX.utils.table_to_book(Table);
        XLSX.writeFile(wb, "法律法规融入表.xlsx")
    }
    render() {
        const { data } = this.state;
        return <div>
            <div style={{ padding: '10px' }}>
                <ReactToPrint
                    trigger={() => <Button type={'primary'} icon={'printer'} >打印</Button>}
                    content={() => this.componentRef}
                />
                <Button style={{ marginLeft: '8px' }} onClick={() => this.onTableBtnExport()} icon="export" >导出</Button>
            </div>


            {/* 事故事件上报表 */}
            <div ref={el => (this.componentRef = el)} id={'tableId' + this.props.login.OrgId} style={{ padding: '20px' }}>
                <h1 style={{ textAlign: 'center'}}>安全生产法律法规融入</h1>
                {
                    data ? <table style={{ width: '100%', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                        
                        <tr style={{ fontWeight: 'bold' }}>
                            <td colSpan={2} rowSpan={1}>序号</td>
                            <td colSpan={6} rowSpan={1}>法律法规名称</td>
                            <td colSpan={4} rowSpan={1}>版本号</td>
                            <td colSpan={12} rowSpan={1}>企业规章制度</td>
                        </tr>
                        {
                            data.Nav_IntegrateDetail && data.Nav_IntegrateDetail.map((it,idx)=>{
                                return (
                                    <tr key={it.ID}>
                                        <td colSpan={2} rowSpan={1}>{idx+1}</td>
                                        <td colSpan={6} rowSpan={1}>{it.Nav_Law ? it.Nav_Law.NAME : ''}</td>
                                        <td colSpan={4} rowSpan={1}>{it.Nav_Law ? it.Nav_Law.VERSION_NUM : ''}</td>
                                        <td colSpan={12} rowSpan={1}>{it.Nav_SCSystem ? it.Nav_SCSystem.NAME : ''}</td>

                                    </tr>
                                )
                            })
                        }
                        <tr>
                            <td colSpan={24} rowSpan={1} className={styles.fontBold}>融入比例: {data.RATE}%</td>
                        </tr>
                    </table> : null
                }
            </div>
        </div>
    }
}

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