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 SEstyles from './SE.css';
import XLSX from 'xlsx';
import { connect } from 'dva';
import moment from 'moment';
import config from "../../../config";

const Option = Select.Option;

// public enum SETrainSurveyStatus
// {
//     草稿 = 0,
//     调查中 = 1,
//     汇总中 = 2,
//     审阅中 = 3,
//     完成 = 4,
// }


class SE019ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            confirmBtn: 'none',
        };
    };

    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] || '';
    }
    fmtJoinUsers = () => {
        let ret = [];
        if (this.state.data && this.state.data.Nav_OpinionList) {
            this.state.data.Nav_OpinionList.map(it => {
                if (it.Nav_User) {
                    ret.push(it.Nav_User.NAME);
                }
            })
        }
        return ret.join('、')
    }

    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_OpinionList');
        extendInclude(json, 'Nav_ResultList');
        extendInclude(json, 'Nav_OpinionList.Nav_User');
        extendInclude(json, 'Nav_ResultList.Nav_Subject');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'SE/TrainingEffectEvaluation/Get',
            onComplete: (ret) => {
                if (!ret)
                    return;
                if ( this.props.data.tableKey == "1") {
                    this.setState({ confirmBtn: 'inline' })
                }
                this.setState({
                    data: ret
                })
            }
        });
    }
    onTableBtnAgree() {
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'HM/HMNoticeRelease/PersonalAgree',
            payload: {
                ID: this.props.data.id,
                TaskID: this.props.data.TaskID,
            },
            onComplete: (ret) => {
                if (ret) {
                    message.success('确认成功！');
                    this.setState({ BtnAgreeDisplay: 'none' })
                    this.BtnClose();
                }
            }
        })
    }
    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' }}>
                <ReactToPrint
                    trigger={() => <Button type={'primary'} icon={'printer'} >打印</Button>}
                    content={() => this.componentRef}
                />
                <Button style={{ marginLeft: '8px' }} onClick={() => this.onTableBtnExport()} icon="export" >导出</Button>
                <Button type="primary" style={{ marginLeft: '8px', display: this.state.confirmBtn }} onClick={() => this.onTableBtnAgree()} 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={4} rowSpan={1} className={styles.fontBold}>培训名称</td>
                            <td colSpan={20} rowSpan={1}>{data ? data.NAME : null}</td>
                        </tr>
                        <tr>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>参与人员</td>
                            <td colSpan={20} rowSpan={1}>{data ? this.fmtJoinUsers() : ''}</td>
                        </tr>
                        <tr>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>调查时间</td>
                            <td colSpan={5} rowSpan={1}>{data ? data.START_TIME : null}</td>
                            <td colSpan={3} rowSpan={1} className={styles.fontBold}>调查份数</td>
                            <td colSpan={2} rowSpan={1}>{data ? data.SURVEY_NUMS : null}</td>
                            <td colSpan={3} rowSpan={1} className={styles.fontBold}>反馈份数</td>
                            <td colSpan={2} rowSpan={1}>{data ? data.FEEDBACK_NUMS : null}</td>
                            <td colSpan={3} rowSpan={1} className={styles.fontBold}>反馈比例</td>
                            <td colSpan={2} rowSpan={1}>{data ? data.FEEDBACK_RATIO : null}</td>
                        </tr>
                        <tr>
                            <td colSpan={24} rowSpan={1} className={styles.fontBold}>评估结果</td>
                        </tr>
                        <tr>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>序号</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>题目</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>好</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>较好</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>一般</td>
                            <td colSpan={4} 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={4} rowSpan={1} >{idx + 1}</td>
                                    <td colSpan={4} rowSpan={1} >{r.Nav_Subject.NAME}</td>
                                    <td colSpan={4} rowSpan={1} >{r.GOOD_RATION}</td>
                                    <td colSpan={4} rowSpan={1} >{r.BETTER_RATION}</td>
                                    <td colSpan={4} rowSpan={1} >{r.NORMAL_RATION}</td>
                                    <td colSpan={4} rowSpan={1} >{r.BAD_RATION}</td>
                                </tr>
                            })

                        }
                        <tr>
                            <td colSpan={24} rowSpan={1} className={styles.fontBold}>培训改进意见</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} >序号</td>
                            <td colSpan={4} rowSpan={1} >姓名</td>
                            <td colSpan={18} rowSpan={1} >意见</td>
                        </tr>

                        {
                            data && data.Nav_OpinionList && data.Nav_OpinionList.map((it, idx) => {
                                return <tr>
                                    <td colSpan={2} rowSpan={1} >{idx + 1}</td>
                                    <td colSpan={4} rowSpan={1}>{it.Nav_User && it.Nav_User.NAME}</td>
                                    <td colSpan={18} rowSpan={1} align="left">{it.OPINION_CONTENT}</td>
                                </tr>
                            })
                        }
                    </tbody>
                </table>
            </div>
        </div>
    }
}

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