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 OG059UserView extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            opinions: [],
            opinion_user_map: {},
        }
    };

    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);
        extendInclude(json, 'Nav_Collection');
        extendInclude(json, 'Nav_Opinions');
        extendInclude(json, 'Nav_User');
        extendInclude(json, 'Nav_Files.Nav_ImgFile.Nav_File');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'OG/EmployeeOpinionCollectionUser/Get',
            onComplete: (ret) => {
                if (!ret)
                    return;
                let map = {};
                let opinions = [];
                let opinion_user_map = {};
                ret.Nav_Opinions.forEach((op, idx) => {
                    opinions.push(op);
                    opinion_user_map[op.ID] = op;
                })
                this.setState({
                    data: ret,
                    opinions: opinions,
                    opinion_user_map: opinion_user_map,
                    map,
                });
            }
        });
    }
    onTableBtnExport() {
        let TableWrap = document.getElementById('tableId' + this.props.data.id);
        let Table = TableWrap.getElementsByTagName('table')[0];
        const wb = XLSX.utils.table_to_book(Table);
        let name = '员工意见征集';
        name += '.xlsx';
        XLSX.writeFile(wb, name)
    }
    fmtEnum(name, value) {
        const enums = this.props.app.enums;
        if (!enums || !enums[name]) return '';
        return enums[name].enums[value] || '';
    }
    renderUserFeed(it, idx) {
        return <tr key={it.ID}>
            <td colSpan={2} rowSpan={1} >{idx + 1}</td>
            <td colSpan={12} rowSpan={1} >{ }</td>
            <td colSpan={2} rowSpan={1} >{it.Nav_User.NAME}</td>
            <td colSpan={2} rowSpan={1} >受理意见</td>
            <td colSpan={6} rowSpan={1} >不认可解释</td>
        </tr>
    }
    render() {
        const curuid = this.props.login.user.ID;
        const { data, opinions, opinion_user_map } = this.state;
        return <div>
            <div ref={el => (this.componentRef = el)} id={'tableId' + this.props.data.id}>
                <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.Nav_Collection.NAME}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>征集起止时间</td>
                            <td colSpan={6} rowSpan={1}>{data && `${data.Nav_Collection.START_TIME}-${data.Nav_Collection.END_TIME}`}</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>序号</td>
                            <td colSpan={12} rowSpan={1} className={styles.fontBold}>意见反馈</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>反馈人员</td>
                        </tr>
                        {
                            opinions.map((it, idx) => {
                                return <tr key={it.ID}>
                                    <td colSpan={2} rowSpan={1} >{idx + 1}</td>
                                    <td colSpan={12} rowSpan={1} >{it.OPINION_CONTENT}</td>
                                    <td colSpan={2} rowSpan={1} >{data.Nav_User.NAME}</td>
                                </tr>
                            })
                        }
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>附件</td>
                            <td colSpan={14} 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 }))(OG059UserView) 