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 OG015ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            BtnAgreeDisplay: 'none',
            confirmDisplay: 'none',
            isMobile: false
        };
    };

    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();
    }
    onTableBtnAgree() {
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'OG/OGInnerContact/PersonalAgree',
            payload: {
                ID: this.props.data.id,
                TaskID: this.props.data.TaskID,
                IgnoreDataRule: true
            },
            onComplete: (ret) => {
                if (ret) {
                    message.success('确认成功');
                    this.setState({ BtnAgreeDisplay: 'none' })
                    this.BtnClose();
                }
            }
        })
    }
    onTableConfirm() {
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'HM/HMNoticeRelease/PersonalAgree',
            payload: {
                ID: this.props.data.id,
                TaskID: this.props.data.TaskID,
            },
            onComplete: (ret) => {
                if (ret) {
                    message.success('确认成功！');
                    this.setState({ BtnAgreeDisplay: 'none' })
                    this.BtnClose();
                }
            }
        })
    }
    loadData = (dataId) => {
        let json = initFilter(this.props.login.OrgId, '', null, null, null);
        extendRule(json, 'ID', 1, dataId);
        json.OrgType = 2;

        extendInclude(json, 'Nav_FormDepartment');
        extendInclude(json, 'Nav_ToDepartment');
        extendInclude(json, 'Nav_FromUser.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_ToUser.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_DoDepartment');
        extendInclude(json, 'Nav_DoUser.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_User');
        extendInclude(json, 'Nav_ItemFiles.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_DoFiles.Nav_ImgFile.Nav_File');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'OG/InnerContact/Get',
            onComplete: (ret) => {
                if (!ret)
                    return;
                if (this.props.data.tableKey == "2" || this.props.data.tableKey == undefined) {
                    this.setState({ BtnAgreeDisplay: 'none' })
                } else if (ret.STATUS == 1) {
                    this.setState({ BtnAgreeDisplay: 'inline' })
                } else if (ret.STATUS == 2) {
                    this.setState({ confirmDisplay: 'inline' })
                }
                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, isMobile } = 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>
                <Button type={'primary'} style={{ marginLeft: isMobile ? '0' : '20px', display: this.state.BtnAgreeDisplay }} onClick={() => this.onTableBtnAgree()} icon="check" >确认</Button>
                <Button type={'primary'} style={{ marginLeft: isMobile ? '0' : '20px', display: this.state.confirmDisplay }} onClick={() => this.onTableConfirm()} icon="check" >已阅</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={12} rowSpan={1}>{data && data.DATE}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>填写人员</td>
                            <td colSpan={8} rowSpan={1}>{data && data.Nav_User && data.Nav_User.NAME}</td>

                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>沟通部门</td>
                            <td colSpan={5} rowSpan={1}>{data && data.Nav_FormDepartment && data.Nav_FormDepartment.NAME}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>沟通人员</td>
                            <td colSpan={5} rowSpan={1}>{data && data.Nav_FromUser && data.Nav_FromUser.NAME}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>沟通人签名</td>
                            <td colSpan={8} rowSpan={1}>{
                                data && data.FORM_USER_TIME == "0001-01-01 00:00:00" ? "" : data && <img width={'150px'} alt={data.Nav_FromUser.NAME} src={config.picServerHost + data.Nav_FromUser.Nav_UserSignFiles[0].Nav_ImgFile.Nav_File.FILE_PATH} />
                            }
                            </td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>被沟通人部门</td>
                            <td colSpan={5} rowSpan={1}>{data && data.Nav_ToDepartment && data.Nav_ToDepartment.NAME}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>被沟通人员</td>
                            <td colSpan={5} rowSpan={1}>{data && data.Nav_ToUser && data.Nav_ToUser.NAME}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>被沟通签名</td>
                            <td colSpan={8} rowSpan={1}>{
                                data && data.TO_USER_TIME == "0001-01-01 00:00:00" ? "" : data && <img width={'150px'} alt={data.Nav_ToUser.NAME} src={config.picServerHost + data.Nav_ToUser.Nav_UserSignFiles[0].Nav_ImgFile.Nav_File.FILE_PATH} />
                            }</td>
                        </tr>

                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>沟通目的</td>
                            <td colSpan={22} rowSpan={1}>{data && data.PURPOSE}</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>沟通事项名称</td>
                            <td colSpan={22} rowSpan={1}>{data && data.NAME}</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>沟通事项描述</td>
                            <td colSpan={22} rowSpan={1}>{data && data.DESC}</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>沟通事项文件</td>
                            <td colSpan={22} rowSpan={1}>
                                {
                                    data && data.Nav_ItemFiles && data.Nav_ItemFiles.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={5} rowSpan={1}>{data && data.Nav_DoDepartment && data.Nav_DoDepartment.NAME}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>落实人员</td>
                            <td colSpan={5} rowSpan={1}>{data && data.Nav_DoUser && data.Nav_DoUser.NAME}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>落实人签名</td>
                            <td colSpan={8} rowSpan={1}>{
                                data && data.STATUS != 2 ? "" : data && <img width={'150px'} alt={data.Nav_DoUser.NAME} src={config.picServerHost + data.Nav_DoUser.Nav_UserSignFiles[0].Nav_ImgFile.Nav_File.FILE_PATH} />
                            }</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>落实情况描述</td>
                            <td colSpan={22} rowSpan={1}>{data && data.DO_DESC}</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>落实文件</td>
                            <td colSpan={22} rowSpan={1}>
                                {
                                    data && data.Nav_DoFiles && data.Nav_DoFiles.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 }))(OG015ShowPrint) 