import { Button, Descriptions, Badge, Popconfirm, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, setDataFieldValue, showApprove, guid, initQueryFilter, ShowDateTime } from '../../../utils/common';
import ReactToPrint from 'react-to-print';
import { ExportToExcel } from '@woowalker/feui'
import XLSX from 'xlsx';
import { connect } from 'dva';
import moment from 'moment';
import { message } from 'antd/lib/index';
import styles from '../../../components/CustomPages/HI/StepForm.css';
import FormPage from '../../FormPage'
import config from "../../../config";

class SC073ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            btndisplay: 'none',
            isCheck: false, //是否待办 审批
        }
    }

    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);
        }
    }

    onTableBtnExport() {
        let TableWrap = document.getElementById('table1');
        let Table = TableWrap.getElementsByTagName('table1')[0];
        const wb = XLSX.utils.table_to_book(Table);
        XLSX.writeFile(wb, this.props.record.Nav_Department.NAME + '.xlsx')

    }

    loadData = (id) => {
        var orgId = this.props.login ? this.props.login.OrgId : '';
        let json = initFilter(orgId);
        extendRule(json, 'ID', 1, id);
        extendInclude(json, 'Nav_Details.Nav_User')
        extendInclude(json, 'Nav_Details.Nav_Files.Nav_ImgFile.Nav_File');
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'SC/DocumentLibrary/Get',
            onComplete: (ret) => {
                if (ret) {
                    ret.Nav_Details.sort((a,b) => {return b.VERSION - a.VERSION}); 
                    this.setState({
                        data: ret,
                    })
                }
            }
        });
    }
    render() {
        const { data } = this.state; const enums = this.props.data.enums ? this.props.data.enums : this.props.app.enums;
        return <div>
            <div style={{ padding: '10px' }}>
                <table>
                    <tr>
                        <td><ReactToPrint trigger={() => <Button type={'default'} icon={'printer'} >打印</Button>} content={() => this.componentRef} /></td>
                        <td><ExportToExcel fileName='文件库' tableId={'tableId' + this.props.data.id} /></td>
                    </tr>
                </table>
            </div>
            <FormPage {...this.state.tmpData} />
            <div ref={el => (this.componentRef = el)} style={{ padding: '20px' }} id={'tableId' + this.props.data.id}>
                <h1 className={styles.showPrintHead}>文件库</h1>
                {
                    data ? <div style={{ width: '95%' }}>
                        <Descriptions title='' size='middle' bordered>
                            <Descriptions.Item label="模块">{data.MODULE}</Descriptions.Item>
                            <Descriptions.Item label="表单">{data.FORM}</Descriptions.Item>
                            <Descriptions.Item label="文件名称">{data.FILE_NAME}</Descriptions.Item>
                            <Descriptions.Item label="文件类型">{data.FILE_TYPE}</Descriptions.Item>
                            <Descriptions.Item label="有效日期">{data.VALID_TIME.substring(0, 10)}</Descriptions.Item>
                        </Descriptions>
                    </div> : null
                }
                {
                    (data?.Nav_Details && data?.Nav_Details.length > 0) ?
                        <table className={styles.PrintFormLight}>
                            <tbody>
                                <tr className={styles.PrintFormLightTh}>
                                    <td>文件名称</td>
                                    <td>版本号</td>
                                    <td>年份</td>
                                    <td>编制时间</td>
                                    <td>颁布时间</td>
                                    <td>上传人员</td>
                                    <td>附件</td>
                                    <td>文件状态</td>
                                </tr>
                                {data?.Nav_Details?.map((item, i) => {
                                    return <tr>
                                        <td>{item.NAME} </td>
                                        <td>{item.VERSION} </td>
                                        <td>{item.YEAR} </td>
                                        <td>{item.COMPILE_TIME} </td>
                                        <td>{item.PUBLISH_TIME} </td>
                                        <td>{item.Nav_User.NAME} </td>
                                        <td>{
                                            item.Nav_Files && item.Nav_Files.map((item1, i) => {
                                                return <a width={'20%'} title={item1.Nav_ImgFile.FILE_NAME} target='_blank' href={config.picServerHost + item1.Nav_ImgFile.Nav_File.FILE_PATH} >{item1.Nav_ImgFile.FILE_NAME} </a>
                                            })
                                        } </td>
                                        <td>{enums.SCStandardSystemEnums.enums[item.STATUS]} </td>
                                    </tr>
                                })}
                            </tbody>
                        </table>
                        : null
                }
            </div>
        </div>
    }
}
export default connect(({ login, app }) => ({ login, app }))(SC073ShowPrint) 
