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 '../HI/StepForm.css';
import config from "../../../config";
import XLSX from 'xlsx';
import { connect } from 'dva';
import moment from 'moment';
let title = '';
class LR009ShowPrint 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);
        }
    }
    fmtEnum(name, value) {
        const enums = this.props.app.enums;
        if (!enums || !enums[name]) return '';
        return enums[name].enums[value] || '';
    }
    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)
        }
    }

    loadData = (dataId) => {
        let json = initFilter(this.props.login.OrgId);
        extendRule(json, 'ID', 1, dataId);
        extendInclude(json, 'Nav_IntegrateUpdateDetail.Nav_Law');
        extendInclude(json, 'Nav_IntegrateUpdateDetail.Nav_LRLawInstitution.Nav_SCSystem');

        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'LR/LawIntegrateUpdate/Get',
            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;
        const enums = this.props.app.enums;
        return <div>
            <div style={{ padding: '10px' }}>
                <ReactToPrint
                    trigger={() => <Button type={'primary'} 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.login.OrgId} style={{ padding: '20px' }}>
                <h1 style={{ textAlign: 'center' }}>{title}更新与融入表</h1>
                {
                    data ?
                        <table style={{ width: '100%', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                            <tr>
                                <td colSpan={5} rowSpan={1} className={styles.fontBold}>年份</td>
                                <td colSpan={5} rowSpan={1}>{moment(data.YEAR).format('YYYY')}</td>
                                <td colSpan={5} rowSpan={1} className={styles.fontBold}>融入比例</td>
                                <td colSpan={5} rowSpan={1}>{data.RATE}</td>
                            </tr>
                            <tr>
                                <td colSpan={1} rowSpan={1} className={styles.fontBold}>序号</td>
                                <td colSpan={4} rowSpan={1} className={styles.fontBold}>法律名称</td>
                                <td colSpan={5} rowSpan={1} className={styles.fontBold}>编制/废除时间</td>
                                <td colSpan={7} rowSpan={1} className={styles.fontBold}>文件名称</td>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>更新状态</td>
                            </tr>
                            {
                                data.Nav_IntegrateUpdateDetail && data.Nav_IntegrateUpdateDetail.map((item, index) => {
                                    return <tr>
                                        <td colSpan={1} rowSpan={1}>{index + 1}</td>
                                        <td colSpan={4} rowSpan={1}>{item.Nav_Law ? item.Nav_Law.NAME : ''}</td>
                                        <td colSpan={5} rowSpan={1}>{item.Nav_Law ? moment(item.Nav_Law.UPDATE_TIME).format('YYYY-MM-DD') : ''}</td>
                                        <td colSpan={7} rowSpan={1}>{
                                            item.Nav_LRLawInstitution && item.Nav_LRLawInstitution.map((ite, i) => {
                                                return ite.Nav_SCSystem.NAME + '  '
                                            })
                                        }</td>
                                        <td colSpan={3} rowSpan={1}>{
                                            this.fmtEnum('LRLawIntegrateUpdateStatusEnum', item.UPDATE_STATUS)}</td>
                                    </tr>
                                })
                            }
                        </table> : null
                }
            </div>
        </div>
    }
}

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