import { Button, Descriptions, Row, Col, Form, Input, Select, Table } 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 '../HI/StepForm.css';
import { connect } from 'dva';
import config from "../../../config";
import XLSX from 'xlsx';
import { message } from "antd/lib/index";

class HM010ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            BtnAgreeDisplay:"none"
        };
    };
    componentDidMount() {
        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) => {
        let json = initFilter(this.props.login.OrgId);
        extendRule(json, 'ID', 1, dataId);
        extendInclude(json, 'Nav_ReleasDepartment');
        extendInclude(json, 'Nav_ReleaseUser');
        extendInclude(json, 'Nav_ReleaseRange.Nav_Department');
        extendInclude(json, 'Nav_Files.Nav_ImgFile.Nav_File');
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'HM/NoticeRelease/Get',
            onComplete: (ret) => {
                if (ret) {
                    this.setState({ data: ret })
                    if (this.props.data.tableKey=="2" ||this.props.data.tableKey==undefined) {
                        this.setState({ BtnAgreeDisplay: 'none' })
                    }else{
                        this.setState({ BtnAgreeDisplay: 'inline' })
                    }
                }
            }
        });
    }
    onTableBtnAgree() {
        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();
                }
            }
        })
    }
    onTableBtnExport() {
        let TableWrap = document.getElementById('tableId' + this.props.data.id);
        let Table = TableWrap.getElementsByTagName('table')[0];
        const wb = XLSX.utils.table_to_book(Table);
        XLSX.writeFile(wb, this.props.record.Nav_ReleasDepartment.NAME + "通知发布.xlsx")
    }

    render() {
        const { data } = this.state;
        const HMFileLevelEnmu = ['普通', '机密', '绝密'];
        return <div>
            <div style={{ padding: '10px' }}>
                <ReactToPrint
                    trigger={() => <Button type={'default'} icon={'printer'} >打印</Button>}
                    content={() => this.componentRef}
                />
                <Button onClick={() => this.onTableBtnExport()} icon="export" style={{ marginLeft: '8px' }}>导出</Button>
                <Button type="primary" style={{ marginLeft: '8px', display: this.state.BtnAgreeDisplay }} onClick={() => this.onTableBtnAgree()} icon="check" >确认</Button>
            </div>
            <div ref={el => (this.componentRef = el)} id={'tableId' + this.props.data.id}>
                <h1 style={{ textAlign: 'center'}}>通知发布</h1>

                {
                   data ? <div style={{ width: '95%'}}>
                        <Descriptions title="" size="middle" bordered>
                            <Descriptions.Item label="通知名称" span={3}>{data.NOTICE_NAME}</Descriptions.Item>
                            <Descriptions.Item label="发布时间">{data.CREATE_TIME}</Descriptions.Item>
                            <Descriptions.Item label="发布部门">{data.Nav_ReleasDepartment ? data.Nav_ReleasDepartment.NAME : ''}</Descriptions.Item>
                            <Descriptions.Item label="发布人员">{data.Nav_ReleaseUser ? data.Nav_ReleaseUser.NAME : ''}</Descriptions.Item>
                            <Descriptions.Item label="文件编号">{data.DOCUMENT_CODE}</Descriptions.Item>
                            <Descriptions.Item label="文件版次">{data.DOCUMENT_VERSION}</Descriptions.Item>
                            <Descriptions.Item label="文件密级">{HMFileLevelEnmu[data.DOCUMENT_LEVEL - 1]}</Descriptions.Item>
                            <Descriptions.Item label="发布范围" span={3}>{
                                    data.Nav_ReleaseRange && data.Nav_ReleaseRange.map((item, i) => {
                                        if (i == (data.Nav_ReleaseRange.length - 1)) {
                                            return item.Nav_Department.NAME
                                        } else {
                                            return item.Nav_Department.NAME + '  '
                                        }
                                    })
                                }</Descriptions.Item>
                            <Descriptions.Item label="附件" span={3}>{
                                data.Nav_Files && data.Nav_Files.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>
                                })
                            }</Descriptions.Item>
                        </Descriptions>
                    </div> : null
                }
            </div>
        </div>
    }

}

export default connect(({ login }) => ({ login }))(HM010ShowPrint) 