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, initFilterGroup, extendFilterGroup, extendGroupRule, 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';
class SE007AllView extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            ROWS: null,
            data: null,
            total: 0,
            reportCount: 0,
            year:new Date().getFullYear(),
        };
    };

    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, '', null, null);
        json.PageIndex = 1;
        json.Limit = 100;
        json.Start = 0;
        json.Sort = 'CREATE_TIME';
        json.Order = 1;
        let filter = initFilterGroup(true);
        extendGroupRule(filter, "CREATE_TIME", 6, `${this.state.year}-01-01`);
        extendGroupRule(filter, "CREATE_TIME", 3, `${this.state.year}-12-31`);
        extendFilterGroup(json, filter);
        extendSelectField(json, 'NAME');
        extendSelectField(json, 'LAUNCH_TIME');
        extendSelectField(json, 'END_TIME');
        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/SETrainSurvey/OrderPaged',
            onComplete: (ret) => {
                if (!ret)
                    return;
                this.updateData(ret);
            }
        });
    }
    updateData(datas) {
        let r = {};
        let ROWS = [];
        let total = 0;
        let reportCount = 0;
        datas.forEach(data => {
            data.Nav_Demands.forEach(it => {
                total++;
                if (it.OK) {
                    reportCount++;
                }
                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++;
                    }
                })
            });
        })
        this.setState({
            data: datas,
            ROWS,
            total,
            reportCount,
        })
    }
    onChange = (value) => {
        this.setState({
            year: value
        })
    }
    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, reportCount, total } = this.state;
        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>
                <Input style={{width:'100px',marginLeft:'8px'}} onChange={(evt) => this.onChange(evt.target.value)} placeholder="统计年份" value={this.state.year}/>
                <Button type={'primary'} style={{ marginLeft: '8px' }} onClick={() => this.loadData()} >查询</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={5} rowSpan={1}>{'全公司'}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>调查份数</td>
                            <td colSpan={4} rowSpan={1}>{total}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>反馈份数</td>
                            <td colSpan={4} rowSpan={1}>{reportCount}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>反馈比例</td>
                            <td colSpan={3} 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={14} 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={14} 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 }))(SE007AllView) 