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, extendOrder } 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";
import FormPage from '../../../components/FormPage'
class SE011ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            BtnAgreeDisplay: 'none',
            confirmBtn: 'none',
            tmpData: {},
        };
    };
    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_YearTrainPlanDepartment.Nav_Department');
        extendInclude(json, 'Nav_Files.Nav_ImgFile.Nav_File');

        extendInclude(json, 'Nav_YearTrainPlanDetail.Nav_TrainTypeEnum');
        extendInclude(json, 'Nav_YearTrainPlanDetail.Nav_YearTrainPlanContent.Nav_TestEnumPoint');
        extendInclude(json, 'Nav_YearTrainPlanDetail.Nav_TrainCheckTypeEnum');
        extendInclude(json, 'Nav_YearTrainPlanDetail.Nav_ResponsibilityDep');
        extendInclude(json, 'Nav_YearTrainPlanDetail.Nav_ResponsibilityUser');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'SE/YearTrainPlan/Get',
            onComplete: (ret) => {
                if (ret) {
                    if (this.props.data && this.props.data.loadDataDeal) {
                        this.props.data.loadDataDeal(1);
                    }
                    this.onClickApprove(false);
                    ret.Nav_YearTrainPlanDetail.sort((a, b) => { return a.MOON - b.MOON });
                    if (ret.STATUS == 30 && this.props.data.tableKey == "1") {
                        this.setState({ confirmBtn: 'inline' })
                    }
                    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);
        XLSX.writeFile(wb, "年度培训计划.xlsx")
    }
    fmtEnum(name, value) {
        const enums = this.props.app.enums;
        if (!enums || !enums[name]) return '';
        return enums[name].enums[value] || '';
    }
    fmtPlanContentList(item) {
        if (!item || !item.Nav_YearTrainPlanContent)
            return '';
        let ret = [];
        for (let it of item.Nav_YearTrainPlanContent) {
            if (it.Nav_TestEnumPoint && it.Nav_TestEnumPoint.NAME)
                ret.push(it.Nav_TestEnumPoint.NAME);
        }
        return ret.join('、');
    }
    fmtDepContentList(item) {
        let ret = [];
        for (let it of item) {
            if (it.Nav_Department && it.Nav_Department.NAME)
                ret.push(it.Nav_Department.NAME);
        }
        return ret.join('、');
    }
    onClickApprove = (appVisible) => {
        const newtmpData = {
            data: { id: this.props.data.id, isShow: true, key: guid(), appVisible: appVisible, BtnAgreeDisplay: 'inline' },
            formCode: "PF123",
        }
        this.setState({
            tmpData: newtmpData,
        });
    }
    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;
    }
    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();
                }
            }
        })
    }
    render() {
        const { data } = this.state;
        return <div>
            <div style={{ padding: '10px' }}>
                <Button onClick={() => this.onClickApprove(true)} icon="export">审批详情</Button>
                <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>
                {
                    data ?
                        <div>
                            <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={3} rowSpan={1} >{data.PLAN_YEAR}</td>
                                        <td colSpan={3} rowSpan={1} className={styles.fontBold}>发起时间</td>
                                        <td colSpan={3} rowSpan={1} >{data.LAUNCH_TIME}</td>
                                        <td colSpan={3} rowSpan={1} className={styles.fontBold}>发起部门</td>
                                        <td colSpan={3} rowSpan={1} >{data.Nav_LaunchDepartment ? data.Nav_LaunchDepartment.NAME : ''}</td>
                                        <td colSpan={3} rowSpan={1} className={styles.fontBold}>发起人员</td>
                                        <td colSpan={3} rowSpan={1} >{data.Nav_LaunchUser ? data.Nav_LaunchUser.NAME : ''}</td>
                                    </tr>
                                    <tr>
                                        <td colSpan={3} rowSpan={1} className={styles.fontBold}>计划名称</td>
                                        <td colSpan={21} rowSpan={1} >{data.PLAN_NAME}</td>
                                    </tr>
                                    <tr>
                                        <td colSpan={3} rowSpan={1} className={styles.fontBold}>培训目标</td>
                                        <td colSpan={21} rowSpan={1} >{data.TRAIN_GOAL}</td>
                                    </tr>
                                    <tr>
                                        <td colSpan={3} rowSpan={1} className={styles.fontBold}>实施要求</td>
                                        <td colSpan={21} rowSpan={1} >{data.IMPLEMENT_REQUIREMENT}</td>
                                    </tr>
                                    <tr>
                                        <td colSpan={3} rowSpan={1} className={styles.fontBold}>发送部门</td>
                                        <td colSpan={21} rowSpan={1} >{this.fmtDepContentList(data.Nav_YearTrainPlanDepartment)}</td>
                                    </tr>
                                    <tr>
                                        <td colSpan={1} rowSpan={1} className={styles.fontBold}>培训月份</td>
                                        <td colSpan={1} rowSpan={1} className={styles.fontBold}>培训类型</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>培训名称</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>培训内容</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>培训级别</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>培训对象</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>培训地点</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>培训形式</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>培训教材</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>培训老师</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>考核方式</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>责任部门</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>责任人</td>
                                    </tr>
                                    {
                                        data.Nav_YearTrainPlanDetail ? data.Nav_YearTrainPlanDetail.map((item, index) => {
                                            return <tr >
                                                <td colSpan={1} rowSpan={1} >{item.MOON}</td>
                                                <td colSpan={1} rowSpan={1} >{item.Nav_TrainTypeEnum ? item.Nav_TrainTypeEnum.NAME : ''}</td>
                                                <td colSpan={2} rowSpan={1} >{item.NAME}</td>
                                                <td colSpan={2} rowSpan={1} >{this.fmtPlanContentList(item)}</td>
                                                <td colSpan={2} rowSpan={1} >{this.fmtEnum("FMDepartmentType", item.LEVEL)}</td>
                                                <td colSpan={2} rowSpan={1} >{item.OBJECT}</td>
                                                <td colSpan={2} rowSpan={1} >{item.ADDRESS}</td>
                                                <td colSpan={2} rowSpan={1} >{this.returnModel(item.TRAIN_MODEL)}</td>
                                                <td colSpan={2} rowSpan={1} >{item.TEACHING_MATERIAL}</td>
                                                <td colSpan={2} rowSpan={1} >{item.TEACHER}</td>
                                                <td colSpan={2} rowSpan={1} >{item.Nav_TrainCheckTypeEnum ? item.Nav_TrainCheckTypeEnum.NAME : ''}</td>
                                                <td colSpan={2} rowSpan={1} >{item.Nav_ResponsibilityDep ? item.Nav_ResponsibilityDep.NAME : ''}</td>
                                                <td colSpan={2} rowSpan={1} >{item.Nav_ResponsibilityUser ? item.Nav_ResponsibilityUser.NAME : ''}</td>
                                            </tr>
                                        }) : null
                                    }
                                    <tr>
                                        <td colSpan={3} rowSpan={1} >附件</td>
                                        <td colSpan={21} 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>
                                </tbody>
                            </table>
                            <FormPage {...this.state.tmpData} />
                        </div>
                        : null
                }
            </div>
        </div>
    }
}
export default connect(({ login, app }) => ({ login, app }))(SE011ShowPrint) 