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

class FM204ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            item: null,
            enumData: null,
            btndisplay: '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);
        }
    }

    onTableBtnExport() {
        // let TableWrap = document.getElementById('table1' + this.props.record.ID);
        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")
    }

    //加载数据(传过来的参数都在props里面，包括登录信息)
    loadData = (id) => {
        var orgId = this.props.login ? this.props.login.OrgId : '';   //登录后有存储登录信息
        let json = initFilter(orgId);
        extendRule(json, 'SNC_LOG_ID', 1, id);    //ID=‘’，字段，等于，条件值， 1代表=，2代表不等于，3小于，4小于等于，5大于，6大于等于，7以开始的Like,8以结束的Like，9包含 

        extendIgnoreDataRule(json)
        let attendanceAry = [], leaveAry = [];
        let that = this;
        let btndisplay = 'none';
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            //url: 'FO/TeamActivity/Get',
            url: 'FM/SyncLogDetail/OrderEntities',
            onComplete: (ret) => {    //返回查询结果ret
                if (ret) {    //判断是否有记录
                    this.setState({   //设置setState全局变量
                        data: ret,      //将ret对象赋值给data, data供页面调用
                    })
                }
            }
        });
    }

    //渲染页面
    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} pageStyle={"#tableId" + this.props.data.id + " { padding:0 40px;} img{width :120px}"} /></td>
                        <td><ExportToExcel fileName='安全检查' tableId={'tableId' + this.props.data.id} /></td>
                    </tr>
                </table>
            </div>

            <div ref={el => (this.componentRef = el)} style={{ padding: '20px', paddingTop: '20px' }} id={'tableId' + this.props.data.id}>
                <h1 style={{ textAlign: 'center', margin: '15px' }}>跑批日志明细</h1>
                {
                    <table className={styles.PrintFormLight}>
                        <tbody>
                            <tr className={styles.PrintFormLightTh}>
                                <td style={{ width: '100px' }}>序号</td>
                                <td>数量</td>
                                <td>请求编码</td>
                                <td>创建时间</td>
                            </tr>
                            {
                                (data && data.length > 0) ?
                                    data.map((item, i) => {
                                        return <tr>
                                            <td>{i + 1}</td>
                                            <td >{item.COUNT}</td>
                                            <td>{item.TRACEIDENTIFIER}</td>
                                            <td>{item.CREATE_TIME}</td>
                                        </tr>
                                    })
                                    : <tr ><td colSpan={4}>查无数据</td></tr>
                            }
                        </tbody>
                    </table>
                }
            </div>
        </div>
    }
}
export default connect(({ login, app }) => ({ login, app }))(FM204ShowPrint) 