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 SE013ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            canSign: false,
            overTimeSign: false,
            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);
        extendRule(json, 'ID', 1, this.props.data.id);
        extendInclude(json, 'Nav_LaunchDepartment');
        extendInclude(json, 'Nav_LaunchUser');
        extendInclude(json, 'Nav_TrainType');
        extendInclude(json, 'Nav_RecordUser');
        extendInclude(json, 'Nav_TrainCheckType');
        extendInclude(json, 'Nav_TrainContentList');
        extendInclude(json, 'Nav_TrainContentList.Nav_Point');
        extendInclude(json, 'Nav_TrainUserList');
        extendInclude(json, 'Nav_TrainUserList.Nav_User');
        extendInclude(json, 'Nav_Files.Nav_ImgFile.Nav_File');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'SE/TrainNotify/Get',
            onComplete: (ret) => {
                if (ret) {
                    let canSign = this.props.data.tableKey == "1" && ret.Nav_TrainUserList.find(it => it.USER_ID == this.props.login.user.ID) ? true : false
                    let overTimeSign = ret.STATUS == 3 && ret.Nav_TrainUserList.find(it => it.USER_ID == this.props.login.user.ID && it.IS_LEAVE === 0) && this.props.data.tableKey == "1" ? true : false
                    this.setState({
                        data: ret,
                        canSign: canSign,
                        overTimeSign: overTimeSign
                    })

                }
            }
        });
    }
    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")
    }
    onSelectChange = (v) => {
        this.state.trainData.value = v
    }
    onTableBtnTrain() {
        if (this.state.trainData.value == 2 && !this.state.trainData.reason) {
            message.error('选择请假时必须录入请假理由');
            return;
        }
        let json = {
            USER_ID: this.props.login.userId,
            ORG_ID: this.props.login.OrgId,
            NOTIFY_ID: this.props.data.id,
            LEAVE_REASON: this.state.trainData.reason,
            IS_LEAVE: this.state.trainData.value,
            TaskID: this.props.data.TaskID,
        };
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'SE/SETrainNotify/TrainPersonStatus',
            payload: json,
            onComplete: (ret) => {
                if (ret) {
                    message.success('提交成功！');
                    this.BtnClose();
                }
            }
        })
    }
    onTableBtnAgree() {
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'SE/SETrainNotify/overTimeSign',
            payload: {
                ID: this.props.data.id,
                TaskID: this.props.data.TaskID,
                USER_ID: this.props.login.userId,
                NOTIFY_ID: this.props.data.id,
            },
            onComplete: (ret) => {
                if (ret) {
                    message.success('确认成功！');
                    this.setState({ BtnAgreeDisplay: 'none' })
                    this.BtnClose();
                }
            }
        })
    }
    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;
        const onChangeReason = (e) => {
            this.state.trainData.reason = e.target.value;
        }
        const formItemLayout = {
            labelCol: { span: 2 },
            wrapperCol: { span: 22 },
        };
        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>
                {this.state.canSign && (
                    <>
                        <Select defaultValue={1} style={{ width: 100, marginLeft: '8px' }} onChange={(e) => this.onSelectChange(e)}>
                            <Option value={1}>参加</Option>
                            <Option value={2}>请假</Option>
                        </Select>
                        <Input onChange={onChangeReason} style={{ width: 500, marginLeft: '8px' }} />
                        <Button type={"primary"} style={{ marginLeft: '8px' }} onClick={() => this.onTableBtnTrain()} icon="check" >保存</Button>
                    </>
                )}
                {this.state.overTimeSign && (
                    <>
                        <Button type={"primary"} style={{ marginLeft: '8px' }} 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>
                {
                    data ? <table style={{ width: '100%', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                        <tr>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>培训名称</td>
                            <td colSpan={20} rowSpan={1} >{data.NAME}</td>
                        </tr>
                        <tr>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>通知编号</td>
                            <td colSpan={4} rowSpan={1} >{data.CODE}</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>发起时间</td>
                            <td colSpan={4} rowSpan={1} >{data.LAUNCH_TIME}</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>发起部门</td>
                            <td colSpan={4} rowSpan={1} >{data.Nav_LaunchDepartment ? data.Nav_LaunchDepartment.NAME : ''}</td>
                        </tr>
                        <tr>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>发起人员</td>
                            <td colSpan={4} rowSpan={1} >{data.Nav_LaunchUser ? data.Nav_LaunchUser.NAME : ''}</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>培训类型</td>
                            <td colSpan={4} rowSpan={1} >{data.Nav_TrainType ? data.Nav_TrainType.NAME : ''}</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>培训级别</td>
                            <td colSpan={4} rowSpan={1} >{this.props.app.enums.FMDepartmentType.enums[data.LEVEL]}</td>
                        </tr>
                        <tr>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>培训时间</td>
                            <td colSpan={4} rowSpan={1} >{data.TRAIN_START_TIME}~{data.TRAIN_END_TIME}</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>培训地点</td>
                            <td colSpan={4} rowSpan={1} >{data.TRAIN_ADDR}</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>培训学时</td>
                            <td colSpan={4} rowSpan={1} >{data.HOURS}</td>
                        </tr>
                        <tr>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>培训老师</td>
                            <td colSpan={4} rowSpan={1} >{data.TRAIN_TEACHER}</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>培训形式</td>
                            <td colSpan={4} rowSpan={1} >{this.returnModel(data.TRAIN_MODEL)}</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>考核方式</td>
                            <td colSpan={4} rowSpan={1} >{data.Nav_TrainCheckType ? data.Nav_TrainCheckType.NAME : ''}</td>
                        </tr>
                        <tr>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>培训内容</td>
                            <td colSpan={20} rowSpan={1}>{
                                data.Nav_TrainContentList ? data.Nav_TrainContentList.map(t => {
                                    return <li>{t.Nav_Point.NAME}</li>
                                }) : ''
                            }
                            </td>
                        </tr>
                        <tr>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>培训安排及要求</td>
                            <td colSpan={20} rowSpan={1} >{data.TRAIN_PLAN}</td>
                        </tr>
                        <tr>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>培训记录人</td>
                            <td colSpan={20} rowSpan={1} >{data.Nav_RecordUser ? data.Nav_RecordUser.NAME : ""}</td>
                        </tr>
                        <tr>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>附件</td>
                            <td colSpan={20} rowSpan={1}>{
                                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>
                        <tr>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>部门/车间</td>
                            <td colSpan={5} rowSpan={1} className={styles.fontBold}>工号</td>
                            <td colSpan={5} rowSpan={1} className={styles.fontBold}>姓名</td>
                            <td colSpan={5} rowSpan={1} className={styles.fontBold}>岗位</td>
                            <td colSpan={5} rowSpan={1} className={styles.fontBold}>参加/请假</td>
                        </tr>
                        {
                            data.Nav_TrainUserList.map((item, index) => {
                                return <tr >
                                    <td colSpan={4} rowSpan={1}>{item.DEPARTMENT_NAME}</td>
                                    <td colSpan={5} rowSpan={1}>{item.Nav_User.CODE}</td>
                                    <td colSpan={5} rowSpan={1}>{item.Nav_User.NAME}</td>
                                    <td colSpan={5} rowSpan={1}>{item.POST_NAME}</td>
                                    <td colSpan={5} rowSpan={1}>{item.IS_LEAVE == 1 ? '参加' : (item.IS_LEAVE == 2 ? '请假'+ "-" + item.LEAVE_REASON : '未完成') }</td>
                                </tr>
                            })
                        }
                    </table> : null
                }
            </div>
        </div>
    }

}

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