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 FormPage from '../../../components/FormPage'

class OG027ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            tmpData: {},
            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);
        }
    }
    onClickApprove = (appVisible) => {
        const newtmpData = {
            data: { id: this.props.data.id, isShow: true, key: guid(), appVisible: appVisible, BtnAgreeDisplay: 'inline' },
            formCode: "PF123",
        }
        this.setState({
            tmpData: newtmpData,
        });
    }
    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_User');
        extendInclude(json, 'Nav_ApproveRewardPunishmentDetail');
        extendInclude(json, 'Nav_ApproveRewardPunishmentDetail.Nav_Department');
        extendInclude(json, 'Nav_ApproveRewardPunishmentDetail.Nav_User');
        extendInclude(json, 'Nav_ApproveRewardPunishmentDetail.Nav_User.Nav_Department');
        extendInclude(json, 'Nav_ApproveRewardPunishmentDetail.Nav_Reason');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'OG/ApproveRewardPunishmentReport/Get',
            onComplete: (ret) => {
                if (ret) {
                    if (this.props.data && this.props.data.loadDataDeal) {
                        this.props.data.loadDataDeal(1);
                    }
                    this.onClickApprove(false);
                    this.setState({ data: ret })
                }
            }
        });
    }
    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")
    }
    fmtEnum(name, value) {
        const enums = this.props.app.enums;
        if (!enums || !enums[name]) return '';
        return enums[name].enums[value] || '';
    }
    fmtDepartment(it) {
        if (!it) {
            return '';
        }
        if (it.Nav_Department) {
            return it.Nav_Department.NAME;
        }
        return it.Nav_User && it.Nav_User.Nav_Department && it.Nav_User.Nav_Department.NAME;
    }

    render() {
        const { data } = this.state;
        return <div>
            <div style={{ padding: '10px' }}>
                <Button onClick={() => this.onClickApprove(true)} icon="export">审批详情</Button>
                <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.data.id} style={{ padding: '20px' }}>
                <h1 style={{ textAlign: 'center' }}>认可与奖励上报表</h1>
                {
                    data ? <div>
                        <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.Nav_Department ? data.Nav_Department.NAME : ''}</td>
                                    <td colSpan={4} rowSpan={1} className={styles.fontBold}>发起人员</td>
                                    <td colSpan={4} rowSpan={1} >{data.Nav_User ? data.Nav_User.NAME : ''}</td>
                                    <td colSpan={4} rowSpan={1} className={styles.fontBold}>发起时间</td>
                                    <td colSpan={4} rowSpan={1} >{data.START_TIME}</td>
                                </tr>
                                <tr>
                                    <td colSpan={2} rowSpan={1} className={styles.fontBold}>序号</td>
                                    <td colSpan={4} rowSpan={1} className={styles.fontBold}>奖惩类型</td>
                                    <td colSpan={4} 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={5} rowSpan={1} className={styles.fontBold}>原因描述</td>
                                </tr>

                                {
                                    data.Nav_ApproveRewardPunishmentDetail ? data.Nav_ApproveRewardPunishmentDetail.map((item, index) => {
                                        return <tr >
                                            <td colSpan={2} rowSpan={1} >{index + 1}</td>
                                            <td colSpan={4} rowSpan={1} >{this.fmtEnum('OGRewardPunishmentType', item.TYPE)}</td>
                                            <td colSpan={4} rowSpan={1} >{this.fmtDepartment(item)}</td>
                                            <td colSpan={4} rowSpan={1} >{item.Nav_User ? item.Nav_User.NAME : ''}</td>
                                            <td colSpan={5} rowSpan={1} >{item.Nav_Reason ? item.Nav_Reason.NAME : ''}</td>
                                            <td colSpan={5} rowSpan={1} >{item.REASON_DESCRIBE}</td>
                                        </tr>
                                    }) : null
                                }
                            </tbody>
                        </table>
                        <FormPage {...this.state.tmpData} />
                    </div>
                        : null

                }
            </div>
        </div>
    }

}

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