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';
import config from "../../../config";

class OG023ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: 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 = (dataId) => {
        if (dataId == "")
            return;
        let json = initFilter(this.props.login.OrgId, '', null, null, null);
        extendRule(json, 'ID', 1, dataId);
        json.OrgType = 2;
        extendInclude(json, 'Nav_User');
        extendInclude(json, 'Nav_Department');
        extendInclude(json, 'Nav_ExternalContactDetail');
        extendInclude(json, 'Nav_FileList.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_DoDepartment');
        extendInclude(json, 'Nav_DoUser');
        extendInclude(json, 'Nav_ImpFileList.Nav_ImgFile.Nav_File');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'OG/ExternalContactCommunicate/Get',
            onComplete: (ret) => {
                if (!ret)
                    return;
                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);
        let name = '外部联系沟通表';
        name += '.xlsx';
        XLSX.writeFile(wb, name)
    }
    fmtEnum(name, value) {
        const enums = this.props.app.enums;
        if (!enums || !enums[name]) return '';
        return enums[name].enums[value] || '';
    }
    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>
            </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={2} rowSpan={1} className={styles.fontBold}>日期</td>
                            <td colSpan={6} rowSpan={1}>{data ? data.START_TIME : null}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>责任部门</td>
                            <td colSpan={6} rowSpan={1}>{data && data.Nav_Department ? data.Nav_Department.NAME : ''}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>沟通人员</td>
                            <td colSpan={6} rowSpan={1}>{data && data.Nav_User ? data.Nav_User.NAME : ''}</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>外部单位</td>
                            <td colSpan={6} rowSpan={1}>{data && data.Nav_ExternalContactDetail ? data.Nav_ExternalContactDetail.NAME : ''}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>外部单位沟通人</td>
                            <td colSpan={6} rowSpan={1}>{data ? data.EXTERNAL_COMMUNICATE_PERSON : ''}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>外部单位电话</td>
                            <td colSpan={6} rowSpan={1}>{data && data.Nav_ExternalContactDetail ? data.Nav_ExternalContactDetail.PHONE : ''}</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>沟通目的</td>
                            <td colSpan={22} rowSpan={1}>{data && data.COMMUNICATE_PURPOSE ? data.COMMUNICATE_PURPOSE : ''}</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>沟通事项名称</td>
                            <td colSpan={22} rowSpan={1}>{data && data.COMMUNICATE_NAME ? data.COMMUNICATE_NAME : ''}</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>沟通事项描述</td>
                            <td colSpan={22} rowSpan={1}>{data && data.COMMUNICATE_DES ? data.COMMUNICATE_DES : ''}</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>沟通文件</td>
                            <td colSpan={22} rowSpan={1}>
                                {
                                    data && data.Nav_FileList && data.Nav_FileList.map((item, i) => {
                                        return <a width={'20%'} title={item.Nav_ImgFile.FILE_NAME} target='_blank' href={config.picServerHost + item.Nav_ImgFile.Nav_File.FILE_PATH} >{item.Nav_ImgFile.FILE_NAME} </a>
                                    })
                                }
                            </td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>落实部门</td>
                            <td colSpan={10} rowSpan={1}>{data && data.Nav_DoDepartment && data.Nav_DoDepartment.NAME}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>落实人员</td>
                            <td colSpan={10} rowSpan={1}>{data && data.Nav_DoUser && data.Nav_DoUser.NAME}</td>
                        </tr>
                        {
                            data && data.DO_USER_ID ?
                                <>
                                    <tr>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>落实情况描述</td>
                                        <td colSpan={22} rowSpan={1}>{data && data.IMPLEMENT_DESCRIPTION ? data.IMPLEMENT_DESCRIPTION : ''}</td>
                                    </tr>
                                    <tr>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>落实文件</td>
                                        <td colSpan={22} rowSpan={1}>
                                            {
                                                data && data.Nav_ImpFileList && data.Nav_ImpFileList.map((item, i) => {
                                                    return <a width={'20%'} title={item.Nav_ImgFile.FILE_NAME} target='_blank' href={config.picServerHost + item.Nav_ImgFile.Nav_File.FILE_PATH} >{item.Nav_ImgFile.FILE_NAME} </a>
                                                })
                                            }
                                        </td>
                                    </tr></> : ''
                        }
                    </tbody>
                </table>
            </div>
        </div>
    }
}
export default connect(({ login, app }) => ({ login, app }))(OG023ShowPrint) 