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

    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.data.onCancel != "undefined" && typeof this.props.data.onCancel == 'function')
            this.props.data.onCancel();
    }

    loadData = (dataId) => {
        let json = initFilter(this.props.login.OrgId);
        extendRule(json, 'ID', 1, dataId);
        extendInclude(json, 'Nav_Department');
        extendInclude(json, 'Nav_DemandReportDetail');

        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'LR/DemandDistinguishReport/Get',
            onComplete: (ret) => {
                if (ret) {
                    ret.Nav_DemandReportDetail.sort((a, b) => { return a.IS_OTHER - b.IS_OTHER });
                    this.setState({ data: ret })
                    if (this.props.data.tableKey == "2" || this.props.data.tableKey == undefined) {
                        this.setState({ BtnAgreeDisplay: 'none' })
                    } else {
                        this.setState({ BtnAgreeDisplay: 'inline' })
                    }
                }
            }
        });
    }
    onTableBtnAgree() {
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'HM/HMNoticeRelease/PersonalAgree',
            payload: {
                ID: this.props.data.id,
                TaskID: this.props.data.TaskID,
            },
            onComplete: (ret) => {
                if (ret) {
                    message.success('确认成功！');
                    this.setState({ BtnAgreeDisplay: 'none' })
                    this.BtnClose();
                }
            }
        })
    }

    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>
                <Button type="primary" style={{ marginLeft: '8px', display: this.state.BtnAgreeDisplay }} onClick={() => this.onTableBtnAgree()} icon="check" >确认</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}>
                            <tbody>
                                <tr>
                                    <td colSpan={4} rowSpan={1} className={styles.fontBold}>统计年份</td>
                                    <td colSpan={4} rowSpan={1}>{data.YEARS}</td>
                                    <td colSpan={4} rowSpan={1} className={styles.fontBold}>统计时间</td>
                                    <td colSpan={4} rowSpan={1}>{moment(data.REPORT_TIME).format('YYYY-MM-DD')}</td>
                                    <td colSpan={4} rowSpan={1} className={styles.fontBold}>调查份数</td>
                                    <td colSpan={4} rowSpan={1}>{data.INVESTIGATE_TIMES}</td>
                                </tr>
                                <tr>
                                    <td colSpan={4} rowSpan={1} className={styles.fontBold}>反馈份数</td>
                                    <td colSpan={4} rowSpan={1}>{data.FEEDBACK_TIMES}</td>
                                    <td colSpan={4} rowSpan={1} className={styles.fontBold}>反馈比例</td>
                                    <td colSpan={4} rowSpan={1}>{data.FEEDBACK_RATE + "%"}</td>
                                    <td colSpan={4} rowSpan={1} className={styles.fontBold}>统计部门</td>
                                    <td colSpan={4} rowSpan={1}>{data.Nav_Department ? data.Nav_Department.NAME : ''}</td>
                                </tr>
                            </tbody>
                            <tbody>
                                <tr>
                                    <td colSpan={1} rowSpan={1} className={styles.fontBold}>序号</td>
                                    <td colSpan={2} rowSpan={1} className={styles.fontBold}>类别</td>
                                    <td colSpan={4} rowSpan={1} className={styles.fontBold}>需求法律法规</td>
                                    <td colSpan={2} rowSpan={1} className={styles.fontBold}>需求人数</td>
                                    <td colSpan={6} rowSpan={1} className={styles.fontBold}>提出人</td>
                                    <td colSpan={9} rowSpan={1} className={styles.fontBold}>需求部门</td>
                                </tr>
                            </tbody>
                            {data.Nav_DemandReportDetail && data.Nav_DemandReportDetail.map((item, index) => {
                                return (
                                    <tbody>
                                        <tr>
                                            <td colSpan={1} rowSpan={1} >{index + 1}</td>
                                            <td colSpan={2} rowSpan={1} >{item.IS_OTHER == 0 ? "法律法规" : "其他"}</td>
                                            <td colSpan={4} rowSpan={1} >{item.DEMAND_NAME}</td>
                                            <td colSpan={2} rowSpan={1} >{item.DEMAND_TIMES}</td>
                                            <td colSpan={6} rowSpan={1} >{item.DEMAND_PERSON}</td>
                                            <td colSpan={9} rowSpan={1} >{item.DEMAND_DEPARTMENT}</td>
                                        </tr>
                                    </tbody>
                                )
                            })
                            }
                        </table> : null
                }
            </div>
        </div>
    }
}

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