import { message } from "antd/lib/index";
import { Button, Popconfirm, Row, Col, Form, Input, Select, Table } 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";

const { Option } = Select;

class SE049ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            subData: null,
            trainData: {
                value: 1,
                reason: ''
            }
        };
    };

    componentDidMount() {
        this.loadData();
    }

    BtnClose = () => {
        if (typeof this.props.data.onCancel != "undefined" && typeof this.props.data.onCancel == 'function')
            this.props.data.onCancel();
    }
    loadData = () => {
        let json = initFilter(this.props.login.OrgId);
        json.Parameter1 = this.props.data.id;
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'SE/SETrainNotify/Get',
            onComplete: (ret) => {
                if (ret) {
                    console.log(ret)
                    this.setState({
                        data: ret.mainNotify,
                        subData: ret.subRecords
                    })

                }
            }
        });
    }
    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")
    }
    openTest(level) {
        alert(1)
    }
    returnModel(level) {
        let str = '';
        if (level == undefined) {
            return str;
        }
        if (level.indexOf('1') >= 0) {
            str += '讲授法 ';
        }
        if (level.indexOf('2') >= 0) {
            str += '视听法 ';
        }
        if (level.indexOf('3') >= 0) {
            str += '研讨法 ';
        }
        if (level.indexOf('4') >= 0) {
            str += '演示法 ';
        }
        return str;
    }
    render() {
        const { data, subData } = 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}>
                {
                    data ? <table style={{ width: '100%', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                        <tbody>
                            <tr>
                                <td colSpan={3} rowSpan={1}>培训名称</td>
                                <td colSpan={21} rowSpan={1} ><h1>{data.NAME}</h1></td>
                            </tr>
                            <tr>
                                <td colSpan={3} rowSpan={1}>培训内容</td>
                                <td colSpan={21} rowSpan={1}>{
                                    data.Nav_TrainContentList ? data.Nav_TrainContentList.map(t => {
                                        return t.Nav_Point.NAME + '  '
                                    }) : ''
                                }
                                </td>
                            </tr>
                            <tr>
                                <td colSpan={3} rowSpan={1} >培训类型</td>
                                <td colSpan={5} rowSpan={1} >{data.Nav_TrainType ? data.Nav_TrainType.NAME : ''}</td>
                                <td colSpan={3} rowSpan={1} >培训形式</td>
                                <td colSpan={5} rowSpan={1} >{this.returnModel(data.TRAIN_MODEL)}</td>
                                <td colSpan={3} rowSpan={1} >考核方式</td>
                                <td colSpan={5} rowSpan={1} >{data.Nav_TrainCheckType ? data.Nav_TrainCheckType.NAME : ''}</td>
                            </tr>
                        </tbody>
                    </table> : null
                }
            </div>
            <div ref={el => (this.componentRef = el)} id={'tableId' + this.props.data.id}>
                {
                    data ? <table style={{ width: '100%', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                        <tbody>
                            <tr>
                                <td colSpan={4} rowSpan={1} >序号</td>
                                <td colSpan={5} rowSpan={1} >组织部门</td>
                                <td colSpan={5} rowSpan={1} >培训时间</td>
                                <td colSpan={5} rowSpan={1} >培训地点</td>
                                <td colSpan={5} rowSpan={1} >培训老师</td>
                                <td colSpan={5} rowSpan={1} >姓名</td>
                                <td colSpan={5} rowSpan={1} >岗位</td>
                                <td colSpan={5} rowSpan={1} >培训时长</td>
                                <td colSpan={5} rowSpan={1} >考核成绩</td>
                                <td colSpan={5} rowSpan={1} >考核试卷</td>
                                <td colSpan={5} rowSpan={1} >电子签名</td>
                            </tr>
                            {
                                subData.map((item, index) => {
                                    let htmls=[];
                                    item.Nav_Users.map((item1, index) => {
                                        let score = item.Nav_Papers.find(t=>t.USER_ID==item1.USER_ID).SCORE;
                                        let html = <tr >
                                            <td colSpan={4} rowSpan={1}>{index + 1}</td>
                                            <td colSpan={5} rowSpan={1}>{item.Nav_Notify.Nav_LaunchDepartment ? item.Nav_Notify.Nav_LaunchDepartment.NAME : ''}</td>
                                            <td colSpan={5} rowSpan={1}>{item1.Nav_User.NAME}</td>
                                            <td colSpan={5} rowSpan={1}>{item.Nav_Notify.TRAIN_ADDR}</td>
                                            <td colSpan={5} rowSpan={1}>{item.Nav_Notify.TRAIN_TEACHER}</td>
                                            <td colSpan={5} rowSpan={1}>{item1.Nav_User.NAME}</td>
                                            <td colSpan={5} rowSpan={1}>{item.Nav_Notify.TRAIN_ADDR}</td>
                                            <td colSpan={5} rowSpan={1}>{item.Nav_Notify.HOURS}</td>
                                            <td colSpan={5} rowSpan={1}>{score}</td>
                                            <td colSpan={5} rowSpan={1}>{score}</td>
                                            {/* <td colSpan={5} rowSpan={1}>{<Button type="primary" onClick={this.openTest}>查看</Button>}</td> */}
                                            <td colSpan={5} rowSpan={1}>{<img width={'150px'} alt={item1.Nav_User.NAME} src={config.picServerHost + item1.Nav_User.Nav_UserSignFiles[0].Nav_ImgFile.Nav_File.FILE_PATH} />}</td>
                                        </tr>
                                        htmls.push(html);
                                    })
                                    return htmls;
                                })
                            }
                        </tbody>
                    </table> : null
                }
            </div>
        </div>
    }

}

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