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 LR001ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
        };
    };

    componentDidMount() {
        this.loadData();
    }

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

    loadData = () => {
        let json = initFilter(this.props.login.OrgId);
        extendRule(json, 'ID', 1, this.props.data.id);
        extendInclude(json, 'Nav_Department');
        extendInclude(json, 'Nav_User');
        extendInclude(json, 'Nav_DemandTypes.Nav_DemandType');
        extendInclude(json, 'Nav_DemandLaw.Nav_Law');
        extendInclude(json, 'Nav_DemandOther.Nav_OtherDistinguish');
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'LR/DemandDistinguish/Get',
            onComplete: (ret) => {
                if (ret) {
                    ret && ret.Nav_DemandLaw && ret.Nav_DemandLaw.unshift({ Nav_Law: { NAME: '文件名称', REMARK: '备注' } })
                    ret && ret.Nav_DemandOther && ret.Nav_DemandOther.unshift({ Nav_OtherDistinguish: { NAME: '文件名称' }, REMARK: '备注' })
                    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}>
                <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={6} rowSpan={1} className={styles.fontBold}>识别时间</td>
                                <td colSpan={6} rowSpan={1}>{moment(data.YEAR).format('YYYY-MM-DD')}</td>
                                <td colSpan={6} rowSpan={1} className={styles.fontBold}>识别部门</td>
                                <td colSpan={6} rowSpan={1}>{data.Nav_Department ? data.Nav_Department.NAME : ''}</td>
                            </tr>
                            <tr>
                                <td colSpan={6} rowSpan={1} className={styles.fontBold}>识别人员</td>
                                <td colSpan={6} rowSpan={1}>{data.Nav_User ? data.Nav_User.NAME : ''}</td>
                                <td colSpan={6} rowSpan={1} className={styles.fontBold}>需求类别</td>
                                <td colSpan={6} rowSpan={1}>{data.demandTypeStr }</td>
                            </tr>
                            {
                                data.Nav_DemandLaw != null && data.Nav_DemandLaw.length > 0 ? data.Nav_DemandLaw.map((item, index) => {
                                    return <tr>
                                        {index < 1 ? <><td colSpan={6} rowSpan={data.Nav_DemandLaw.length}>法律法规</td></> : null}
                                        <td colSpan={2} rowSpan={1}>{index == 0 ? '序号' : index}</td>
                                        <td colSpan={8} rowSpan={1}>{item.Nav_Law ? item.Nav_Law.NAME : ''}</td>
                                        <td colSpan={8} rowSpan={1}>{item.Nav_Law ? item.Nav_Law.REMARK : ''}</td>
                                    </tr>
                                }) : <> <tr>
                                    <td colSpan={6} rowSpan={1}>法律法规</td>
                                    <td colSpan={2} rowSpan={1}>''</td>
                                    <td colSpan={8} rowSpan={1}>''</td>
                                    <td colSpan={8} rowSpan={1}>''</td>
                                </tr></>
                            }
                            {
                                data.Nav_DemandOther != null && data.Nav_DemandOther.length > 0 ? data.Nav_DemandOther.map((item, index) => {
                                    return <tr>
                                        {index < 1 ? <><td colSpan={6} rowSpan={data.Nav_DemandOther.length}>其他</td></> : null}
                                        <td colSpan={2} rowSpan={1}>{index == 0 ? '序号' : index}</td>
                                        <td colSpan={8} rowSpan={1}>{item.Nav_OtherDistinguish ? item.Nav_OtherDistinguish.NAME : ''}</td>
                                        <td colSpan={8} rowSpan={1}>{item.REMARK}</td>
                                    </tr>
                                }) : <> <tr>
                                    <td colSpan={6} rowSpan={1}>其他</td>
                                    <td colSpan={2} rowSpan={1}>''</td>
                                    <td colSpan={8} rowSpan={1}>''</td>
                                    <td colSpan={8} rowSpan={1}>''</td>
                                </tr></>
                            }
                        </table> : null
                }
            </div>
        </div>
    }
}

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