import { message } from "antd/lib/index";
import {Button, Popconfirm, Row, Col,Checkbox,Radio, Form, Input, Select, Table,Upload, Icon, PageHeader,Modal,Spin } 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.Option;

const answer = {
    color: 'blue',
    textDecoration: 'underline',
}


class SE005ReportEdit extends React.Component {
    constructor(props) {
        super(props);
        let ALL = props.formCode === 'SE005_ALLREPORT';
        this.state = {
            ALL,
            list: null,
            taskid: null,
            surveyid: !ALL && this.props.data.id,
            data: null,
            type: '部门',
            loading: false,
        };
    };
    componentDidMount() {
        if (this.state.ALL){
            this.loadList();
        }else{
            this.loadData();
        }
    }
    BtnClose = () => {
        if (typeof this.props.data.onCancel != "undefined" && typeof this.props.data.onCancel == 'function')
            this.props.data.onCancel();
    }
    loadList = () => {
        let json = initFilter(this.props.login.OrgId);
        json.OrgType = 2;
        this.setState({loading: true})
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'SE/SESafeSurvey/GetReportList',
            onComplete: (ret) => {
                this.setState({loading: false})
                if (!ret)
                    return;
                if (ret.length == 0){
                    message.warn("当年未查到有效的公司调查问卷报表");
                    return;
                }
                let taskid = this.props.data.id;
                if (!ret.find(it => it.Key === taskid)){
                    taskid = ret[0].Key;
                }

                this.setState({
                    list: ret,
                    taskid,
                })
                this.loadData(); 
            }
        });
        
    }
    loadData = () => {
        let json = initFilter(this.props.login.OrgId);
        json.OrgType = 2;
        json.Parameter3 = this.state.type;
        if (this.state.ALL){
            if (!this.state.taskid)
                return;
            json.Parameter2 = this.state.taskid;
        }else{
            if (!this.state.surveyid)
                return;
            json.Parameter1 = this.state.surveyid;
        }
        this.setState( { 
            data: null,
            loading: true
        });
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'SE/SESafeSurvey/GetReport',
            onComplete: (ret) => {
                this.setState({loading: false})
                if (!ret){
                    return;
                }
                this.setState({data: ret})
            }
        });
    }
    onApprove = ()=>{
        const { ALL,data } = this.state;
        let json = {
            Parameter1: data.SURVEY[0].ID,
            Parameter2: this.props.data.TaskID,
        }
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'SE/SESafeSurvey/Approve',
            onComplete: (ret) => {
                message.success("审阅完成");
                this.BtnClose();
            }
        });
    }
    fmtEnum(name,value){
        const enums = this.props.app.enums;
        if (!enums || !enums[name]) return '';
        return enums[name].enums[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)
    }
    getJoinDepartment(depart){
        let arr = [];
        for(let it of depart){
            if (it.Nav_Department){
                if (it.Nav_Department.NAME == "宁化行洛坑钨矿有限公司"){
                    arr.push("公司领导")
                }else{
                    arr.push(it.Nav_Department.NAME)
                }
            }
        }
        return arr;
    }
    render() {
        const { ALL,data,list,loading } = this.state;
        const count = data && data.ROWS && data.ROWS.length > 0 && data.ROWS[0].length || 1;
        const SPAN = Math.floor(24 / count) || 1;
        const TITLE_SPAN = Math.max(24 - count * SPAN,0) + SPAN;
        const canApprove = this.props.data.tableKey == "1"
        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>
                {
                    ALL &&  
                    <Select style={{ fontSize: 14, width: "100px",marginLeft: '8px' }}
                        showSearch
                        value={this.state.taskid}
                        onChange={val => {
                            this.setState({taskid: val})
                            this.loadData();
                        } }
                    >
                        {
                            list && list.map( it => {
                                    return <Option key={it.Key} value={it.Key}>{it.Value}</Option>
                                } 
                            )
                        }
                    </Select>
                }

                <Select style={{ fontSize: 14, width: "100px", marginLeft: '8px'  }}
                    value={this.state.type}
                    onChange={val => {
                        this.state.type = val;
                        this.loadData();
                    } }
                >
                    <Option value={"部门"}>{"部门"}</Option>
                    <Option value={"人员"}>{"人员"}</Option>
                    <Option value={"岗位"}>{"岗位"}</Option>
                </Select>
                {
                    canApprove && <Button style={{ marginLeft: '8px' }} type={'primary'} onClick={() => this.onApprove()} icon="check" >同意</Button>
                }
            </div>
            <div ref={el => (this.componentRef = el)} id={'tableId' + this.props.data.id} style={{ padding: '20px' }}>
                <h1 style={{ textAlign: 'center'}}>{this.state.ALL ? '安全意识调查问卷公司报表' : '安全意识调查问卷部门报表'}</h1>
                <Spin spinning={this.state.loading}>
                    {
                        data &&  <>
                            <table style={{ width: '100%', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                                <tbody>
                                    {
                                        data.SURVEY && data.SURVEY.map(SURVEY =>{
                                            return <>
                                                <tr>
                                                    <td colSpan={4} rowSpan={1} className={styles.fontBold}>调查名称</td>
                                                    <td colSpan={20} rowSpan={1}>{SURVEY.NAME }</td>
                                                </tr>
                                                <tr>
                                                    <td colSpan={3} rowSpan={1} className={styles.fontBold}>发起时间</td>
                                                    <td colSpan={3} rowSpan={1}>{SURVEY.LAUNCH_TIME}</td>
                                                    <td colSpan={3} rowSpan={1} className={styles.fontBold}>发起部门</td>
                                                    <td colSpan={3} rowSpan={1}>{SURVEY.Nav_LaunchDepartment && SURVEY.Nav_LaunchDepartment.NAME}</td>
                                                    <td colSpan={3} rowSpan={1} className={styles.fontBold}>发起人员</td>
                                                    <td colSpan={3} rowSpan={1}>{SURVEY.Nav_LaunchUser && SURVEY.Nav_LaunchUser.NAME}</td>
                                                    <td colSpan={3} rowSpan={1} className={styles.fontBold}>截止时间</td>
                                                    <td colSpan={3} rowSpan={1}>{SURVEY.END_TIME}</td>
                                                </tr>
                                                <tr>
                                                    <td colSpan={4} rowSpan={1} className={styles.fontBold}>参与人员</td>
                                                    <td colSpan={20} rowSpan={1}>{this.getJoinDepartment(SURVEY.Nav_JoinDepartment).join('、')}</td>
                                                </tr>
                                                <tr>
                                                    <td colSpan={4} rowSpan={1} className={styles.fontBold}>调查内容</td>
                                                    <td colSpan={20} rowSpan={1}>{ SURVEY.Nav_Points && SURVEY.Nav_Points.map(it => it.Nav_Point.NAME).join('、')}</td>
                                                </tr>
                                            </>
                                        })   
                                    }
                                    <tr>
                                        <td colSpan={24} rowSpan={1} className={styles.fontBold}>答案正确率(百分比)</td>
                                    </tr>
                                    {
                                        data.ROWS && data.ROWS.map((row,ridx) =>{
                                            return <tr>
                                                {
                                                    row.map((col,cidx) =>{
                                                        return <td colSpan={cidx == 0 ? TITLE_SPAN : SPAN} rowSpan={1} className={ridx == 0 || cidx == 0 ? styles.fontBold : null}>{col}{ridx == 0 || cidx == 0 ? null : "%"}</td>
                                                    })
                                                }
                                            </tr>
                                        })  
                                    }
                                </tbody>
                            </table>
                        </>
                    }
                    {
                        !data && !loading && <div style={{ width: '100%', textAlign: 'center'}}> 暂未查到有效数据 </div>
                    }
                </Spin>

            </div>
        </div>
    }
}

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