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';
class OG030ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            selIndex: -1,
            details: 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 = (dataId) => {
        let json = initFilter(this.props.login.OrgId, '', null, null, null);
        extendRule(json, 'ID', 1, dataId);
        json.OrgType = 2;
        extendInclude(json, 'Nav_User');
        extendInclude(json, 'Nav_Department');
        extendInclude(json, 'Nav_Details');
        extendInclude(json, 'Nav_Details.Nav_User');
        extendInclude(json, 'Nav_Details.Nav_Items');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'OG/EventGet/Get',
            onComplete: (ret) => {
                if (!ret)
                    return;
                let items = [];
                ret.Nav_Details.forEach(it => {
                    if (it.STATUS == 2) {
                        it.Nav_Items.forEach(i => {
                            if (i.DESC) {
                                items.push({
                                    ID: i.ID,
                                    TYPE: this.fmtEnum('OGEventGetType', i.TYPE),
                                    DESC: i.DESC,
                                    NAME: it.Nav_User.NAME,
                                    REPLY: i.REPLY
                                })
                            }
                        });
                    }
                })
                this.setState({
                    data: ret,
                    items,
                });
            }
        });
    }

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

    onAgree() {
        const { data } = this.state;
        if (!data || data.STATUS != 2) {
            message.error("当前状态不可提交")
            return;
        }
        let json = initFilter(this.props.login.OrgId, data.ID, null, null, null);
        json.Parameter1 = this.props.data.TaskID;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'OG/OGEventGet/Agree',
            onComplete: (ret) => {
                if (ret) {
                    message.success('审阅成功');
                    this.BtnClose();
                }
            }
        });
    }

    render() {
        const { data, items } = this.state;
        return <div>
            {

                <div style={{ padding: '10px' }}>
                    <ReactToPrint
                        trigger={() => <Button type={'primary'} icon={'printer'} >打印</Button>}
                        content={() => this.componentRef}
                    />
                    {
                        data && data.STATUS == 2 && data.USER_ID == this.props.login.user.ID && <>
                            <Button style={{ marginLeft: '8px' }} onClick={() => this.onAgree()} icon="check" >已阅</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={4} rowSpan={1}>{data && data.START_TIME}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>截止时间</td>
                            <td colSpan={4} rowSpan={1}>{data && data.END_TIME}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>征集部门</td>
                            <td colSpan={4} rowSpan={1}>{data && data.Nav_Department && data.Nav_Department.NAME}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>征集人员</td>
                            <td colSpan={4} rowSpan={1}>{data && data.Nav_User && data.Nav_User.NAME}</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>序号</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>事件类型</td>
                            <td colSpan={8} rowSpan={1} className={styles.fontBold}>事件描述</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>有意见人员</td>
                            <td colSpan={8} rowSpan={1} className={styles.fontBold}>反馈意见</td>
                        </tr>
                        {
                            items && items.map((it, idx) => {
                                return <tr key={it.ID}>
                                    <td colSpan={2} rowSpan={1} >{idx + 1}</td>
                                    <td colSpan={4} rowSpan={1} >{it.TYPE}</td>
                                    <td colSpan={8} rowSpan={1} >{it.DESC}</td>
                                    <td colSpan={2} rowSpan={1} >{it.NAME}</td>
                                    <td colSpan={8} rowSpan={1} >{it.REPLY}</td>
                                </tr>
                            })
                        }
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>工余事件数</td>
                            <td colSpan={10} rowSpan={1}>{data && data.EVENT_COUNT}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>对比数</td>
                            <td colSpan={10} rowSpan={1}>{data && data.EVENT_COMPARE}</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    }
}

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