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 OG010ShowPrint 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.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_AppointType');
        extendInclude(json, 'Nav_Files.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_Department');
        extendInclude(json, 'Nav_AppointUsers.Nav_User');
        extendInclude(json, 'Nav_AppointAgreeUser');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'OG/PolicyDocumentDetail/Get',
            onComplete: (ret) => {
                if (ret) {
                    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] || '';
    }
    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.data?.id} style={{ padding: '20px' }}>
                <h1 style={{ textAlign: 'center' }}>人员任命情况变化记录表</h1>
                <table style={{ width: '100%', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                    <tr>
                        <td colSpan={3} rowSpan={1} className={styles.fontBold}>创建时间</td>
                        <td colSpan={4} rowSpan={1} >{data && data.CREATE_TIME}</td>
                        <td colSpan={3} rowSpan={1} className={styles.fontBold}>任命文件编号</td>
                        <td colSpan={4} rowSpan={1} >{data && data.CODE}</td>
                        <td colSpan={3} rowSpan={1} className={styles.fontBold}>版本号</td>
                        <td colSpan={4} rowSpan={1} >{data && data.VERSION}</td>
                    </tr>
                    <tr>
                        <td colSpan={3} rowSpan={1} className={styles.fontBold}>被任命人员类别</td>
                        <td colSpan={4} rowSpan={1} >{data && data.Nav_AppointType && data.Nav_AppointType.NAME}</td>
                        <td colSpan={3} rowSpan={1} className={styles.fontBold}>任命文件名称</td>
                        <td colSpan={4} rowSpan={1} >{data && data.NAME}</td>
                        <td colSpan={3} rowSpan={1} className={styles.fontBold}>人员任命增加/删除原因</td>
                        <td colSpan={4} rowSpan={1}>{this.fmtEnum('OGAppointmentAddDelReason', data && data.APPOINT_REASON)}</td>

                    </tr>
                    <tr>
                        <td colSpan={3} rowSpan={1} className={styles.fontBold}>编制单位</td>
                        <td colSpan={4} rowSpan={1}>{data && data.Nav_Department.NAME}</td>
                        <td colSpan={3} rowSpan={1} className={styles.fontBold}>批准人员</td>
                        <td colSpan={4} rowSpan={1}>{data && data.Nav_AppointAgreeUser.NAME}</td>
                        <td colSpan={3} rowSpan={1} className={styles.fontBold}>颁布时间</td>
                        <td colSpan={4} rowSpan={1}>{data && data.PUBLISH_TIME}</td>
                    </tr>
                    <tr>
                        <td colSpan={3} rowSpan={1} className={styles.fontBold}>生效时间</td>
                        <td colSpan={4} rowSpan={1}>{data && data.APPOINT_EFFECT_TIME}</td>
                        <td colSpan={3} rowSpan={1} className={styles.fontBold}>状态</td>
                        <td colSpan={4} rowSpan={1}>{this.fmtEnum('HMOperationStepEnum', data && data.STATUS)}</td>
                        <td colSpan={3} rowSpan={1} className={styles.fontBold}>被任命人员</td>
                        <td colSpan={4} rowSpan={1}>{data && data.Nav_AppointUsers.map(t => t.Nav_User.NAME).join('、')}</td>
                    </tr>
                    <tr>
                        <td colSpan={3} rowSpan={1} className={styles.fontBold}>附件</td>
                        <td colSpan={18} rowSpan={1}>
                            {
                                data && data.Nav_Files && data.Nav_Files.map((item, i) => {
                                    return <a width={'20%'} title={item.Nav_ImgFile.FILE_NAME} target='_blank' href={config.picServerHost + item.Nav_ImgFile.Nav_File.FILE_PATH} >{item.Nav_ImgFile.FILE_NAME} </a>
                                })
                            }
                        </td>
                    </tr>

                </table>
            </div>
        </div>
    }
}
export default connect(({ login, app }) => ({ login, app }))(OG010ShowPrint) 