import { message } from "antd/lib/index";
import { Button, Popconfirm, Row, Col, 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';
class SE021ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
        };
    };

    componentDidMount() {
        this.loadData();
    }
    BtnClose = () => {
        if (typeof this.props.data.onCancel != "undefined" && typeof this.props.data.onCancel == 'function')
            this.props.data.onCancel();
    }
    fmtEnum(name,value){
        const enums = this.props.app.enums;
        if (!enums || !enums[name]) return '';
        return enums[name].enums[value] || ''; 
    }
    loadData = () => {
        if (this.props.data.id == "")
            return;
        let json = initFilter(this.props.login.OrgId, '', null, null, null);
        extendRule(json, 'ID', 1, this.props.data.id);
        json.OrgType = 2;
        extendInclude(json, 'Nav_User');
        extendInclude(json, 'Nav_ResultList');
        extendInclude(json, 'Nav_ResultList.Nav_Subject');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'SE/TrainingEffectEvaluationSurvey/Get',
            onComplete: (ret) => {
                if (!ret)
                    return;
                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);
        let name = '培训效果评估调查表';
        name += '.xlsx';
        XLSX.writeFile(wb, name)
    }
    render() {
        const { data } = this.state;
        const enums = this.props.app.enums;
        const reportCount = data && data.Nav_Demands ? data.Nav_Demands.filter(i => i.OK).length : 0;
        const total = data && data.Nav_Demands ? data.Nav_Demands.length : 0;
        const rate = total === 0 ? 0 : Math.round(reportCount * 100 / total);
        return <div>
            <div style={{ padding: '10px' }}>
                <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={3} rowSpan={1} className={styles.fontBold}>培训名称</td>
                            <td colSpan={5} rowSpan={1}>{data ? data.NAME : null}</td>
                            <td colSpan={3} rowSpan={1} className={styles.fontBold}>调查截止时间</td>
                            <td colSpan={5} rowSpan={1}>{data ? data.END_TIME : null}</td>
                            <td colSpan={3} rowSpan={1} className={styles.fontBold}>参与人员</td>
                            <td colSpan={5} rowSpan={1}>{data && data.Nav_User ? data.Nav_User.NAME : ''}</td>
                        </tr>
                        <tr>
                            <td colSpan={3} rowSpan={1} className={styles.fontBold}>序号</td>
                            <td colSpan={10} rowSpan={1} className={styles.fontBold}>题目</td>
                            <td colSpan={11} rowSpan={1} className={styles.fontBold}>选项</td>
                        </tr>
                        {
                            this.state.data && this.state.data.Nav_ResultList.map((r, idx) => {
                                return <tr key={r.ID}>
                                    <td colSpan={3} rowSpan={1} >{idx + 1}</td>
                                    <td colSpan={10} rowSpan={1} >{r.Nav_Subject.NAME}</td>                                    
                                    <td colSpan={11} rowSpan={1} >{this.fmtEnum('SEEvaluationResultEnum',r.EVALUATION_RESULT)}</td>
                                </tr>
                            })

                        }
                        <tr>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>培训改进意见</td>
                            <td colSpan={20} rowSpan={1}>{data ? data.OPINION : null}</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    }
}

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