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";

const Option = Select.Option;

// 草稿 = 0,
// 受理中 = 1,
// 调查中 = 2,
// 评价中 = 3,
// 完成 = 4,

class OG025ShowPrint 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);
        extendInclude(json, 'Nav_ExternalContactDetail');
        extendInclude(json, 'Nav_User');
        extendInclude(json, 'Nav_ItemEnum');
        extendInclude(json, 'Nav_FileList.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_Department');
        extendInclude(json, 'Nav_Assignee');
        extendInclude(json, 'Nav_SurveyDepartment');
        extendInclude(json, 'Nav_SurveyUser');
        extendInclude(json, 'Nav_SurveyFileList.Nav_ImgFile.Nav_File');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'OG/ExternalComplainAdvise/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_ExternalContactDetail ? data.Nav_ExternalContactDetail.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={14} rowSpan={1}>{data && data.NAME}</td>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>建议事项类别</td>
                            <td colSpan={6} rowSpan={1}>{data && data.Nav_ItemEnum && data.Nav_ItemEnum.NAME}</td>
                        </tr>
                        <tr>
                            <td colSpan={2} rowSpan={1} className={styles.fontBold}>建议事项描述</td>
                            <td colSpan={22} rowSpan={1}>{data && data.DESCRIPTION}</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>
                        {
                            data && data.STATUS > 0 ?
                                <>
                                    <tr>
                                        <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_Assignee ? data.Nav_Assignee.NAME : null}</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>受理状况</td>
                                        <td colSpan={6} rowSpan={1}>{data && this.fmtEnum('OGAssigneeStatus', data.ASSIGNEE_STATUS)}</td>
                                    </tr>
                                    <tr>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>受理意见描述</td>
                                        <td colSpan={22} rowSpan={1}>{data && data.ASSIGNEE_DESCRIPTION}</td>
                                    </tr>
                                    <tr>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>调查部门</td>
                                        <td colSpan={10} rowSpan={1}>{data && data.Nav_SurveyDepartment && data.Nav_SurveyDepartment.NAME}</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>调查人员</td>
                                        <td colSpan={10} rowSpan={1}>{data && data.Nav_SurveyUser && data.Nav_SurveyUser.NAME}</td>
                                    </tr>
                                </> : ''
                        }
                        {
                            data && data.STATUS > 1 ?
                                <>
                                    <tr>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>调查情况描述</td>
                                        <td colSpan={22} rowSpan={1}>{data && data.SURVEY_DESCRIPTION}</td>
                                    </tr>
                                    <tr>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>调查文件</td>
                                        <td colSpan={22} rowSpan={1}>
                                            {
                                                data && data.Nav_SurveyFileList && data.Nav_SurveyFileList.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={22} rowSpan={1}>{data && this.fmtEnum('OGSatisfactionStatus', data.SATISFACTION_STATUS)}</td>
                                    </tr>
                                </> : ''
                        }
                    </tbody>
                </table>
            </div>
        </div>
    }
}

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