import { Checkbox, message, Radio } from "antd/lib/index";
import { Button, Descriptions, Badge, Popconfirm, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, ShowDateTime, extendIgnoreDataRule, setDataFieldValue, guid, initQueryFilter, showFiles, showApprove } 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';
import FormPage from '../../FormPage'

class TI018ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            item: null,
            btndisplay: 'none',
            INSURE_DURATION: 0
        }
    }

    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 taskID = this.props.data.TaskID
        var orgId = this.props.login ? this.props.login.OrgId : '';   //登录后有存储登录信息
        let json = initFilter(orgId);
        extendRule(json, 'ID', 1, id);    //ID=‘’，字段，等于，条件值， 1代表=，2代表不等于，3小于，4小于等于，5大于，6大于等于，7以开始的Like,8以结束的Like，9包含 
        extendInclude(json, 'Nav_User.Nav_Person.Nav_Post');
        extendInclude(json, 'Nav_User.Nav_Department');
        extendIgnoreDataRule(json)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'TI/TIInsuranceRecord/GetUserRecord',
            onComplete: (ret) => {    //返回查询结果ret
                this.setState({   //设置setState全局变量
                    data: ret,      //将ret对象赋值给data, data供页面调用
                })
            }
        });
    }

    //渲染页面
    render() {
        const { data, INSURE_DURATION } = 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>

            <FormPage {...this.state.tmpData} />
            {/* 安全检查 */}
            {/* <div ref={el => (this.componentRef = el)} style={{ padding: '20px', paddingTop: '20px' }} id={'tableId' + this.props.data.id}>
                <h1 className={styles.showPrintHead}>参保记录</h1>
                {
                    data ? <div>
                        <Descriptions title="" size="middle" bordered>
                            <Descriptions.Item label="参保类型">{enums.TIInsuranceTypeEnum.enums[data.TYPE]}</Descriptions.Item>
                            <Descriptions.Item label="附件" span={3}>{
                                showFiles(data.Nav_Files, config.picServerHost)
                            }</Descriptions.Item>
                        </Descriptions>
                    </div> : null
                }
                {
                    data?.Nav_UserList ?
                        <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>
                                    <td>参保状态</td>
                                    <td>停缴时间</td>
                                </tr>
                                {data.Nav_UserList?.map((item, i) => {
                                    return <tr>
                                        <td>{i + 1}</td>
                                        <td>{item.Nav_User.CODE} </td>
                                        <td>{item.Nav_User.NAME}</td>
                                        <td>{item.Nav_User.SEX} </td>
                                        <td>{item.DEPARTMENTNAME} </td>
                                        <td>{item.USERPOSTNAME} </td>
                                        <td>{item.Nav_User.ID_CARD} </td>
                                        <td>{item.INSURE_TIME} </td>
                                        <td>{enums.TIInsuranceStatusEnum.enums[item.INSURE_STATUS] } </td>
                                        <td>{item.STOP_TIME} </td>
                                    </tr>
                                })}
                            </tbody>
                        </table>
                        : null
                }
            </div> */}



            <div ref={el => (this.componentRef = el)} style={{ padding: '20px', paddingTop: '20px' }} id={'tableId' + this.props.data.id}>
                <h1 className={styles.showPrintHead}>参保信息</h1>
                {
                    data ? <div>
                        <Descriptions title="" size="middle" bordered>
                            <Descriptions.Item label="工号">{data.Nav_User.CODE}</Descriptions.Item>
                            <Descriptions.Item label="姓名">{data.Nav_User.NAME}</Descriptions.Item>
                            <Descriptions.Item label="性别">{enums.FMSexEnum.enums[data.Nav_User.SEX]}</Descriptions.Item>
                            <Descriptions.Item label="部门名称">{data.Nav_User.Nav_Department.NAME}</Descriptions.Item>
                            <Descriptions.Item label="岗位名称">{data.Nav_User.Nav_Person.Nav_Post.NAME}</Descriptions.Item>
                            <Descriptions.Item label="身份证">{data.Nav_User.ID_CARD}</Descriptions.Item>
                            <Descriptions.Item label="参保类型">{enums.TIInsuranceTypeEnum.enums[data.TYPE]}</Descriptions.Item>
                            <Descriptions.Item label="参保时间">{ShowDateTime(data.INSURE_TIME)}</Descriptions.Item>
                            <Descriptions.Item label="参保状态">{enums.TIInsuranceStatusEnum.enums[data.INSURE_STATUS]}</Descriptions.Item>
                            <Descriptions.Item label="停缴时间">{ShowDateTime(data.STOP_TIME)}</Descriptions.Item>
                            <Descriptions.Item label="参保期限(月)">{data.INSURE_DURATION}</Descriptions.Item>
                        </Descriptions>
                    </div> : null
                }
            </div>

        </div>
    }
}
export default connect(({ login, app }) => ({ login, app }))(TI018ShowPrint) 