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 FormPage from '../../../components/FormPage'

class SE009ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        let showBtn = props.formCode !== 'SE011_WF';
        if (this.props.data.id !== "" && props.data.btnConfig && props.data.btnConfig.customParams) {
            let params = props.data.btnConfig.customParams;
            if (params.indexOf('VIEW=1') >= 0) {
                showBtn = false;
            }
        }
        this.state = {
            data: null,
            showBtn,
            tmpData: {},
            BtnAgreeDisplay: 'none'
        };
    };

    componentDidMount() {
        this.loadData();
    }

    onAgree(ok) {
        const { data } = this.state;
        if (!data || data.STATUS != 2) {
            message.error("当前状态不可提交")
            return;
        }
        let json = initFilter(this.props.login.OrgId, data.ID);
        json.Parameter1 = ok ? '1' : '0'
        json.OrgType = 2;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'SE/SEDepTrainPlan/Agree',
            onComplete: (ret) => {
                if (ret) {
                    message.success('提交成功！');
                    this.BtnClose();
                }
            }
        });
    }

    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_DepTrainPlanDetail');
        extendInclude(json, 'Nav_DepTrainPlanDetail.Nav_TrainTypeEnum');
        extendInclude(json, 'Nav_DepTrainPlanDetail.Nav_DepTrainPlanContent');
        extendInclude(json, 'Nav_DepTrainPlanDetail.Nav_DepTrainPlanContent.Nav_TestEnumPoint');
        extendInclude(json, 'Nav_DepTrainPlanDetail.Nav_TrainCheckTypeEnum');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'SE/DepTrainPlan/Get',
            onComplete: (ret) => {
                if (ret) {
                    ret.Nav_DepTrainPlanDetail.sort((a,b)=>{return a.MOON-b.MOON});
                    this.onClickApprove(false);
                    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")
    }
    fmtPlanContentList(item) {
        if (!item || !item.Nav_DepTrainPlanContent)
            return '';
        let ret = [];
        for (let it of item.Nav_DepTrainPlanContent) {
            if (it.Nav_TestEnumPoint && it.Nav_TestEnumPoint.NAME)
                ret.push(it.Nav_TestEnumPoint.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;
    }
    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}
                    pageStyle=".printDIV { padding:0 40px} img{width :120px}"
                />
                <Button style={{ marginLeft: '8px' }} onClick={() => this.onTableBtnExport()} icon="export" >导出</Button>
                {this.state.showBtn && data && data.STATUS == 2 && (
                    <>
                        <Button style={{ marginLeft: '8px' }} onClick={() => this.onAgree(true)} icon="check" >同意</Button>
                        <Button style={{ marginLeft: '8px' }} onClick={() => this.onAgree(false)} 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 style={{margin: '0 0 20px 0' }}>
                                        <td colSpan={4} rowSpan={1} className={styles.fontBold}>计划年度</td>
                                        <td colSpan={3} rowSpan={1} >{data.PLAN_YEAR}</td>
                                        <td colSpan={4} rowSpan={1} className={styles.fontBold}>起止时间</td>
                                        <td colSpan={5} rowSpan={1} >{moment(data.LAUNCH_TIME).format('YYYY-MM-DD')} ~ {moment(data.END_TIME).format('YYYY-MM-DD')}</td>
                                        <td colSpan={4} rowSpan={1} className={styles.fontBold}>发起部门</td>
                                        <td colSpan={3} rowSpan={1} >{data.Nav_LaunchDepartment ? data.Nav_LaunchDepartment.NAME : ''}</td>
                                        <td colSpan={4} rowSpan={1} className={styles.fontBold}>发起人员</td>
                                        <td colSpan={3} rowSpan={1} >{data.Nav_LaunchUser ? data.Nav_LaunchUser.NAME : ''}</td>
                                    </tr>
                                    <tr>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>培训月份</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>培训类型</td>
                                        <td colSpan={6} rowSpan={1} className={styles.fontBold}>培训名称</td>
                                        <td colSpan={5} 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={3} 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_DepTrainPlanDetail ? data.Nav_DepTrainPlanDetail.map((item, index) => {
                                            return <tr >
                                                <td colSpan={2} rowSpan={1} >{item.MOON}</td>
                                                <td colSpan={2} rowSpan={1} >{item.Nav_TrainTypeEnum ? item.Nav_TrainTypeEnum.NAME : ''}</td>
                                                <td colSpan={6} rowSpan={1} >{item.NAME}</td>
                                                <td colSpan={5} rowSpan={1} >{this.fmtPlanContentList(item)}</td>
                                                <td colSpan={2} rowSpan={1} >{this.props.app.enums.FMDepartmentType.enums[item.LEVEL]}</td>
                                                <td colSpan={2} rowSpan={1} >{item.OBJECT}</td>
                                                <td colSpan={3} 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>
                                            </tr>
                                        }) : null
                                    }
                                </tbody>
                            </table>
                            <FormPage {...this.state.tmpData} />
                        </div>
                        : null
                }
            </div>
        </div>
    }

}

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