import { message } from "antd/lib/index";
import { Button, Popconfirm, Row, Col, Checkbox, Radio, Form, Input, Select, Table, Upload, Icon, PageHeader, Modal } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, extendSelectField, setDataFieldValue, guid, initQueryFilter } from "../../../utils/common";
import ReactToPrint from "react-to-print";
import styles from '../../CustomPages/HI/StepForm.css';
import SEstyles from './SE.css';
import XLSX from 'xlsx';
import { connect } from 'dva';
import moment from 'moment';
import config from "../../../config";

const Option = Select.Option;

// public enum SETrainSurveyStatus
// {
//     草稿 = 0,
//     调查中 = 1,
//     汇总中 = 2,
//     完成 = 3,
// }


class SE007ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            ROWS: null,
            data: null,
            ROWS1: null,
            data1: null,
        };
    };

    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 = () => {
        if (this.props.data.id == "")
            return;
        let json = initFilter(this.props.login.OrgId);
        extendRule(json, 'ID', 1, this.props.data.id);
        extendSelectField(json, 'NAME');
        extendSelectField(json, 'LAUNCH_TIME');
        extendSelectField(json, 'END_TIME');
        extendSelectField(json, 'STATUS');
        extendSelectField(json, 'Nav_LaunchDepartment.NAME');
        extendSelectField(json, 'Nav_LaunchUser.NAME');
        extendSelectField(json, 'Nav_Points.Nav_Point.NAME');
        extendSelectField(json, 'Nav_Demands.Nav_User.Nav_Department.NAME');
        extendSelectField(json, 'Nav_Demands.Nav_Department.NAME');
        extendSelectField(json, 'Nav_Demands.Nav_Items.Nav_DEMAND.NAME');
        extendSelectField(json, 'Nav_Demands.OK');
        extendSelectField(json, 'Nav_Demands.OTHER');
        extendSelectField(json, 'Nav_Demands.Nav_User.NAME');
        
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'SE/TrainSurvey/Get',
            onComplete: (ret) => {
                if (!ret)
                    return;
                this.updateData(ret);
            }
        });
    }
    updateData(data) {
        let r = {};
        let ROWS = [];
        let ROWS1 = [];

        data.Nav_Demands.forEach(it => {
            it.Nav_Items.forEach(d => {
                let idx = r[d.DEMAND_ID];
                if (idx === undefined) {
                    idx = ROWS.length
                    r[d.DEMAND_ID] = idx;
                    ROWS.push({
                        ID: d.DEMAND_ID,
                        NAME: d.Nav_DEMAND?.NAME,
                        department: [],
                        total: 0,
                        report: 0,
                    })
                }

                let name = it.Nav_User?.Nav_Department.NAME;
                if (name == '宁化行洛坑钨矿有限公司') {
                    name = '公司领导';
                }
                if (ROWS[idx].department.indexOf(name) == -1) {
                    ROWS[idx].department.push(name);
                }
                ROWS[idx].total++;
                if (it.OK) {
                    ROWS[idx].report++;
                }
            })
        });
        data.Nav_Demands.forEach(it => {
            if (it.OTHER != undefined && it.OTHER != "") {
                ROWS1.push({
                    NAME: it.OTHER,
                    department: [it.Nav_User?.NAME],
                    total: 1,
                    report: 1,
                })
            }
        });

        this.setState({
            data: data,
            ROWS,
            ROWS1,
        })
    }
    onSubmit = () => {
        if (!this.state.data || (this.state.data.STATUS !== 2)) {
            return;
        }
        let json = {
            Parameter1: this.props.data.id,
            Parameter2: this.props.data.TaskID,
        }
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'SE/SETrainSurvey/SubmitReport',
            payload: json,
            onComplete: (ret) => {
                if (ret) {
                    message.success('提交成功！');
                    this.BtnClose();
                }
            }
        })

    }
    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 = '部门培训需求调查统计表';
        name += '.xlsx';
        XLSX.writeFile(wb, name)
    }
    render() {
        const { data } = this.state;
        const reportCount = data && data.Nav_Demands ? data.Nav_Demands.filter(i => i.OK).length : 0;
        const total = data && data.Nav_Demands ? data.Nav_Demands.length : 0;
        const rate = total === 0 ? 0 : Math.round(reportCount * 100 / total);
        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.data && this.state.data.STATUS === 2 && this.props.login.user.ID == this.state.data.LAUNCH_USER_ID && (
                        <Button type="primary" onClick={() => { this.onSubmit(); }}>
                            已阅
                        </Button>
                    )
                }
            </div>
            <div ref={el => (this.componentRef = el)} style={{ padding: '20px' }} id={'tableId' + this.props.data.id}>
                <h1 style={{ textAlign: 'center', margin: '15px' }}>部门培训需求调查统计表</h1>
                <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 && data.NAME ? data.NAME : ''}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>发起时间</td>
                            <td colSpan={4} rowSpan={1}>{data && data.LAUNCH_TIME ? data.LAUNCH_TIME : ''}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>截止时间</td>
                            <td colSpan={4} rowSpan={1}>{data && data.END_TIME ? data.END_TIME : ''}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>发起部门</td>
                            <td colSpan={4} rowSpan={1}>{data && data.Nav_LaunchDepartment ? data.Nav_LaunchDepartment.NAME : ''}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>发起人员</td>
                            <td colSpan={2} rowSpan={1}>{data && data.Nav_LaunchUser ? data.Nav_LaunchUser.NAME : ''}</td>

                        </tr>
                        <tr>


                        </tr>
                        <tr>
                            {/* <td colSpan={2} rowSpan={1} className={styles.fontBold}>调查时间</td>
                            <td colSpan={4} rowSpan={1}>{data ? data.LAUNCH_TIME : null}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>调查部门</td>
                            <td colSpan={4} rowSpan={1}>{data && data.Nav_LaunchDepartment ? data.Nav_LaunchDepartment.NAME : ''}</td> */}
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>调查目的</td>
                            <td colSpan={14} rowSpan={1}>{
                                data?.Nav_Points && data?.Nav_Points.map((item, i) => {
                                    if (i == (data?.Nav_Points.length - 1)) {
                                        return item.Nav_Point?.NAME
                                    } else {
                                        return item.Nav_Point?.NAME + '  '
                                    }
                                })
                            }</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>调查份数</td>
                            <td colSpan={2} rowSpan={1}>{total}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>反馈份数</td>
                            <td colSpan={2} rowSpan={1}>{reportCount}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>反馈比例</td>
                            <td colSpan={2} rowSpan={1}>{rate + '%'}</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>序号</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>培训需求</td>
                            <td colSpan={18} rowSpan={1} className={styles.fontBold}>需求部门</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>需求人数</td>
                        </tr>
                        {
                            this.state.ROWS && this.state.ROWS.map((r, idx) => {
                                return <tr key={r.ID}>
                                    <td colSpan={2} rowSpan={1} >{idx + 1}</td>
                                    <td colSpan={4} rowSpan={1} >{r.NAME}</td>
                                    <td colSpan={18} rowSpan={1} >{r.department.join('、')}</td>
                                    <td colSpan={4} rowSpan={1} >{r.report}</td>
                                </tr>
                            })

                        }
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>序号</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>培训其他需求</td>
                            <td colSpan={18} rowSpan={1} className={styles.fontBold}>需求人员</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>需求人数</td>
                        </tr>
                        {
                            this.state.ROWS1 && this.state.ROWS1.map((r, idx) => {
                                return <tr key={r.ID}>
                                    <td colSpan={2} rowSpan={1} >{idx + 1}</td>
                                    <td colSpan={4} rowSpan={1} >{r.NAME}</td>
                                    <td colSpan={18} rowSpan={1} >{r.department.join('、')}</td>
                                    <td colSpan={4} rowSpan={1} >{r.report}</td>
                                </tr>
                            })

                        }
                    </tbody>
                </table>
            </div>
        </div>
    }
}

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