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 EduCardStatus = {
    '草稿': 0,
    '公司培训': 1,
    '部门培训': 2,
    '班组培训': 3,
    '员工签到': 4,
    '审阅中': 5,
    '归档': 6,
}
class SE015ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            Note: '',
            EduCardID: null,
            impl: null,
            impls: null,
            readOnly: true,
            depName: "",
        };
    };
    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);
        extendRule(json, 'ID', 1, dataId);
        extendInclude(json, 'Nav_LaunchDepartment');
        extendInclude(json, 'Nav_LaunchUser');
        extendInclude(json, 'Nav_Department');
        extendInclude(json, 'Nav_Post');
        extendInclude(json, 'Nav_User.Nav_Department.Nav_Parent.Nav_Parent');
        extendInclude(json, 'Nav_TrainRecordList.Nav_User');
        extendInclude(json, 'Nav_TrainRecordList.Nav_Department');
        extendInclude(json, 'Nav_TrainRecordList.Nav_Content');
        extendInclude(json, 'Nav_TrainRecordList.Nav_TrainRecordFile.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_TrainRecordList.Nav_TrainSignatureFile.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_User.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'SE/SEThreeLevelSafeEduCard/Get',
            onComplete: (ret) => {
                if (ret) {
                    if (ret?.Nav_TrainRecordList) {
                        ret.Nav_TrainRecordList = ret.Nav_TrainRecordList.sort((a, b) => { return a.THREE_LEVEL_SAFE_TRAIN_TYPE - b.THREE_LEVEL_SAFE_TRAIN_TYPE });
                        ret.Nav_TrainRecordList = ret.Nav_TrainRecordList.filter(t=>t.SCORE!=undefined);
                    }
                    this.setState({ data: ret })
                    let top = ret.Nav_Department?.Nav_Parent?.Nav_Parent?.NAME;
                    if (top != undefined) {
                        this.state.depName = top + "/" + ret.Nav_Department?.Nav_Parent?.NAME + "/" + ret.Nav_Department?.NAME
                    } else {
                        this.state.depName = ret.Nav_Department?.Nav_Parent?.NAME + "/" + ret.Nav_Department?.NAME
                    }
                }
            }
        });
    }
    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 = '';
        if (this.state.data && this.state.data.CODE)
            name += this.state.data.CODE
        name += '三级安全教育卡.xlsx'
        XLSX.writeFile(wb, name)
    }

    onTableBtnApprove = () => {
        if (!this.state.data || this.state.data.STATUS !== EduCardStatus['审阅中'])
            return
        let json = initFilter(this.props.login.OrgId);
        json.Parameter1 = this.props.data.id;
        json.Parameter2 = this.props.data.TaskID;
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'SE/SEThreeLevelSafeEduCard/Approve',
            payload: json,
            onComplete: (ret) => {
                if (ret) {
                    message.success('审阅成功');
                    this.setState({
                        EduCardID: null,
                    });
                    this.BtnClose();
                }
            }
        })
    }
    personalApprove = () => {
        let json = initFilter(this.props.login.OrgId);
        json.Parameter1 = this.props.data.id;
        json.Parameter2 = this.props.data.TaskID;
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'SE/SEThreeLevelSafeTrainRecord/PersonalAgree',
            payload: json,
            onComplete: (ret) => {
                if (ret) {
                    message.success('审阅成功');
                    this.setState({
                        EduCardID: null,
                    });
                    this.BtnClose();
                }
            }
        })
    }
    fmtEnum(name, value) {
        const enums = this.props.app.enums;
        if (!enums || !enums[name]) return '';
        return enums[name].enums[value] || '';
    }
    fmtTopics = () => {
        let ret = [];
        if (this.state.data && this.state.data.Nav_Topics) {
            this.state.data.Nav_Topics.map(it => {
                if (it.Nav_Topic) {
                    ret.push(it.Nav_Topic.NAME);
                }
            })
        }
        return ret.join('、');
    }
    fmtJoinUsers = () => {
        let ret = [];
        if (this.state.data && this.state.data.Nav_JoinUsers) {
            this.state.data.Nav_JoinUsers.map(it => {
                if (it.Nav_User) {
                    ret.push(it.Nav_User.NAME);
                }
            })
        }
        return ret.join('、')
    }
    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 } = 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>
                {
                    (data && data.STATUS === EduCardStatus['审阅中']) && this.props.data.tableKey != undefined && this.props.data.tableKey == "1" ?
                        <Button onClick={() => this.onTableBtnApprove()} type={'primary'} icon="check" >已阅</Button>
                        : null
                }
                {
                    (data && (data.STATUS <= EduCardStatus['员工签到'])) && this.props.data.tableKey != undefined && this.props.data.tableKey == "1" ?
                        <Button onClick={() => this.personalApprove()} type={'primary'} icon="check" >签到</Button>
                        : null
                }
                <br />
            </div>
            <div ref={el => (this.componentRef = el)} id={'tableId' + this.props.data.id} style={{ padding: '20px' }}>
                <h1 style={{ textAlign: 'center' }}>三级安全教育卡</h1>
                {
                    data ? <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.LAUNCH_TIME}</td>

                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>发起部门</td>
                            <td colSpan={4} rowSpan={1}>{data.Nav_LaunchDepartment ? data.Nav_LaunchDepartment.NAME : null}</td>

                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>发起人员</td>
                            <td colSpan={4} rowSpan={1}>{data.Nav_LaunchUser ? data.Nav_LaunchUser.NAME : null}</td>

                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>编号</td>
                            <td colSpan={4} rowSpan={1}>{data.Nav_User.CODE}</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>姓名</td>
                            <td colSpan={4} rowSpan={1}>{data.Nav_User.NAME}</td>

                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>身份证号</td>
                            <td colSpan={10} rowSpan={1}>{data.ID_CARD_NUMBER}</td>

                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>入职时间</td>
                            <td colSpan={4} rowSpan={1}>{moment(data.IN_TIME).format('YYYY-MM-DD')}</td>

                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>部门</td>
                            <td colSpan={10} rowSpan={1}>{this.state.depName}</td>

                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>岗位</td>
                            <td colSpan={10} rowSpan={1}>{data.Nav_Post.NAME}</td>

                        </tr>
                        {
                            data.Nav_TrainRecordList && data.Nav_TrainRecordList.map(it => {
                                return <>
                                    <tr>
                                        <td colSpan={24} rowSpan={1} className={styles.fontBold}>
                                            {
                                                this.fmtEnum('SEThreeLevelSafeTrainType', it.THREE_LEVEL_SAFE_TRAIN_TYPE)
                                            }
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>开始时间</td>
                                        <td colSpan={3} rowSpan={1}>{moment(it.START_TIME).format('YYYY-MM-DD')}</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>结束时间</td>
                                        <td colSpan={3} rowSpan={1}>{moment(it.END_TIME).format('YYYY-MM-DD')}</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>培训地点</td>
                                        <td colSpan={2} rowSpan={1}>{it.TAINNING_ADDR}</td>

                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>组织部门</td>
                                        <td colSpan={3} rowSpan={1}>{it.Nav_Department ? it.Nav_Department.NAME : null}</td>

                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>记录/考核人</td>
                                        <td colSpan={3} rowSpan={1}>{it.Nav_User ? it.Nav_User.NAME : null}</td>
                                    </tr>
                                    <tr>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>培训学时</td>
                                        <td colSpan={3} rowSpan={1}>{it.TRAINNING_TIME}</td>

                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>培训老师</td>
                                        <td colSpan={3} rowSpan={1}>{it.TEACHER}</td>

                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>培训形式</td>
                                        <td colSpan={2} rowSpan={1}>{this.returnModel(it.TRAIN_MODEL)}</td>

                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>考核结果</td>
                                        <td colSpan={3} rowSpan={1}>{this.fmtEnum('SEExaminationResultsEnum', it.EXAMINATION_RESULTS)}</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>考核分数</td>
                                        <td colSpan={3} rowSpan={1}>{it.SCORE}</td>
                                    </tr>
                                    <tr>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>培训内容</td>
                                        <td colSpan={22} rowSpan={1} className={styles.fontBold}>
                                            {it.Nav_Content.NAME}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>签名</td>
                                        <td colSpan={22} rowSpan={1} >
                                            {
                                                data.Nav_User.Nav_UserSignFiles.length > 0 && it.STATUS == 1 ? <img width={'150px'} alt={data.Nav_User.NAME} src={config.picServerHost + data.Nav_User.Nav_UserSignFiles[0].Nav_ImgFile.Nav_File.FILE_PATH} /> : null
                                            }
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>附件</td>
                                        <td colSpan={22} rowSpan={1}>
                                            {
                                                it.Nav_TrainRecordFile && it.Nav_TrainRecordFile.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> : null
                }
            </div>
        </div>
    }

}

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