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, 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 SE005ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            safe: 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);
        json.OrgType = 2;
        extendRule(json, 'ID', 1, this.props.data.id);
        extendInclude(json, 'Nav_LaunchDepartment');
        extendInclude(json, 'Nav_LaunchUser');
        extendInclude(json, 'Nav_Points');
        extendInclude(json, 'Nav_Points.Nav_Point');
        extendInclude(json, 'Nav_JoinDepartment');
        extendInclude(json, 'Nav_JoinDepartment.Nav_Department');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'SE/SafeSurvey/Get',
            onComplete: (ret) => {
                if (ret) {
                    this.setState({ safe: 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);
        let name = '安全意识调查问卷';
        name += '.xlsx';
        XLSX.writeFile(wb, name)
    }
    getJoinDepartment() {
        let arr = [];
        if (this.state.safe && this.state.safe.Nav_JoinDepartment) {
            for (let it of this.state.safe.Nav_JoinDepartment) {
                if (it.Nav_Department) {
                    if (it.Nav_Department.NAME == "宁化行洛坑钨矿有限公司") {
                        arr.push("公司领导")
                    } else {
                        arr.push(it.Nav_Department.NAME)
                    }
                }
            }
        }
        return arr;
    }
    render() {
        const { safe } = this.state;
        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>
            </div>
            <div ref={el => (this.componentRef = el)} id={'tableId' + this.props.data.id} style={{ padding: '20px' }}>
                <h1 style={{ textAlign: 'center'}}>安全意识调查问卷</h1>
                <table style={{ width: '100%', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                    <tbody>
                        <tr>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>调查名称</td>
                            <td colSpan={20} rowSpan={1}>{safe ? safe.NAME : ''}</td>
                        </tr>
                        <tr>
                            <td colSpan={3} rowSpan={1} className={styles.fontBold}>发起时间</td>
                            <td colSpan={3} rowSpan={1}>{safe ? safe.LAUNCH_TIME : null}</td>
                            <td colSpan={3} rowSpan={1} className={styles.fontBold}>发起部门</td>
                            <td colSpan={3} rowSpan={1}>{safe ? safe.Nav_LaunchDepartment.NAME : ''}</td>
                            <td colSpan={3} rowSpan={1} className={styles.fontBold}>发起人员</td>
                            <td colSpan={3} rowSpan={1}>{safe ? safe.Nav_LaunchUser.NAME : ''}</td>
                            <td colSpan={3} rowSpan={1} className={styles.fontBold}>截止时间</td>
                            <td colSpan={3} rowSpan={1}>{safe ? moment(safe.END_TIME).format('YYYY-MM-DD') : ''}</td>
                        </tr>
                        <tr>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>参与人员</td>
                            <td colSpan={20} rowSpan={1}>{this.getJoinDepartment().join('、')}</td>
                        </tr>
                        <tr>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>调查内容</td>
                            <td colSpan={20} rowSpan={1}>{safe && safe.Nav_Points && safe.Nav_Points.map(it => it.Nav_Point.NAME).join('、')}</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    }
}

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