import { message } from "antd/lib/index";
import { Button, Popconfirm, Row, Col, DatePicker, Checkbox, Radio, Form, Input, Select, Table, Upload, Icon, PageHeader, Modal } 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 OG031ShowPrint 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) => {
        if (this.props.data.id == "")
            return;
        let json = initFilter(this.props.login.OrgId, '', null, null, null);
        extendRule(json, 'ID', 1, this.props.data.id);
        extendInclude(json, 'Nav_User');
        extendInclude(json, 'Nav_AHUser');
        extendInclude(json, 'Nav_Department');
        extendInclude(json, 'Nav_DoUser');
        extendInclude(json, 'Nav_DoDepartment');
        extendInclude(json, 'Nav_Files.Nav_ImgFile.Nav_File');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'OG/EventReport/Get',
            onComplete: (ret) => {
                if (!ret)
                    return;
                if (this.props.data.tableKey == "2" || this.props.data.tableKey == undefined) {
                    this.setState({ BtnAgreeDisplay: 'none' })
                } else {
                    this.setState({ BtnAgreeDisplay: 'inline' })
                }
                this.setState({
                    data: ret,
                });
            }
        });
    }

    fmtEnum(name, value) {
        const enums = this.props.app.enums;
        if (!enums || !enums[name]) return '';
        return enums[name].enums[value] || '';
    }

    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")
    }

    render() {
        const { data, items } = 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}>
                    <tbody>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>上报日期</td>
                            <td colSpan={6} rowSpan={1}>{data && data.TIME}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>上报员工</td>
                            <td colSpan={6} rowSpan={1}>{data && data.Nav_User && data.Nav_User.NAME}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>所在部门</td>
                            <td colSpan={6} rowSpan={1}>{data && data.Nav_Department && data.Nav_Department.NAME}</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>事件类型</td>
                            <td colSpan={6} rowSpan={1}>{data && this.fmtEnum('OGEventGetType', data.TYPE)}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>发生日期</td>
                            <td colSpan={6} rowSpan={1}>{data && data.HAPPEN_TIME}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>发生地点</td>
                            <td colSpan={6} rowSpan={1}>{data && data.PLACE}</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>事件描述</td>
                            <td colSpan={22} rowSpan={1}>{data && data.DESC}</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>处理状况</td>
                            <td colSpan={6} rowSpan={1}>{data && this.fmtEnum('OGEventDoType', data.DO_TYPE)}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>处理部门</td>
                            <td colSpan={6} rowSpan={1}>{data && data.Nav_DoDepartment && data.Nav_DoDepartment.NAME}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>责任人</td>
                            <td colSpan={6} rowSpan={1}>{data && data.Nav_DoUser && data.Nav_DoUser.NAME}</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>情况描述</td>
                            <td colSpan={22} rowSpan={1}>{data && data.DO_DESC}</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>附件</td>
                            <td colSpan={22} 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>
                    </tbody>
                </table>
            </div>
        </div>
    }
}

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