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

    onClickApprove = (appVisible) => {
        const newtmpData = {
            data: { id: this.props.data.id, isShow: true, key: guid(), param: this.props.data.param, appVisible: appVisible, BtnAgreeDisplay: 'none' },
            formCode: "PF123",
        }
        this.setState({
            tmpData: newtmpData,
        });
    }

    //审批意见 改变
    NOTJOINREASONCHANGE = (val) => {
        this.state.NOTJOINREASON = val
    }


    //加载数据(传过来的参数都在props里面，包括登录信息)
    loadData = (id) => {
        var taskID = this.props.data.TaskID
        // var loginID = this.props.login.user.ID
        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_Department');
        extendInclude(json, 'Nav_User');
        extendInclude(json, 'Nav_SafePdtCostRecordType.Nav_CostType');
        extendInclude(json, 'Nav_Files.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_Approve.Nav_ApproveDetails.Nav_ApproveUser.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendIgnoreDataRule(json)

        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'TI/SafePdtCostRecord/Get',// url: 'SC/MtMeeting/Get',           
            onComplete: (ret) => {    //返回查询结果ret
                if (ret) {    //判断是否有记录
                    var isTaskCheck = false
                    var isHasOpinion = false
                    if (ret.STATUS == 30 && taskID != undefined && taskID.length > 0) {
                        if (ret.USER_ID_HOST == loginID || ret.USER_ID_RECORDER == loginID) {
                            //后台查看 task 是否已经操作过
                            isTaskCheck = true
                        }
                        if (!isTaskCheck) {
                            for (let i = 0; i < ret.Nav_ListPAll.length; i++) {
                                // JOINRESULT
                                // debugger
                                if (ret.Nav_ListPAll[i].USER_ID == loginID && (ret.Nav_ListPAll[i].JOINRESULT == undefined || ret.Nav_ListPAll[i].JOINRESULT == null)) {
                                    isTaskCheck = true
                                    break
                                }
                            }
                        }
                    }

                    if (isTaskCheck && ((that.props.data.tableKey == null) || (that.props.data.tableKey != undefined && that.props.data.tableKey == "2"))) {
                        isTaskCheck = false
                    }

                    if (ret.Nav_ListMeetingContent != null && ret.Nav_ListMeetingContent.length > 0) {
                        for (let j = 0; j < ret.Nav_ListMeetingContent.length; j++) {
                            if (!isHasOpinion && ret.Nav_ListMeetingContent[j].Nav_ContentName != null && ret.Nav_ListMeetingContent[j].Nav_ContentName.Nav_ListContent != null && ret.Nav_ListMeetingContent[j].Nav_ContentName.Nav_ListContent.length > 0) {
                                isHasOpinion = true
                            }
                        }
                    }
                }

                this.setState({   //设置setState全局变量
                    data: ret,      //将ret对象赋值给data, data供页面调用
                    isTaskCheck: isTaskCheck,//有taskID 状态 审核完成  登陆者 属于参会人
                    isHasOpinion: isHasOpinion//有会议意见
                })
            }
        });
    }

    //渲染页面
    render() {
        const { data, isTaskCheck, isHasOpinion } = this.state;
        return <div>
            <div style={{ padding: '10px' }}>
                <table>
                    <tr>
                        <td><Button onClick={() => this.onClickApprove(true)} style={{ marginRight: '8px' }} icon="export">审批详情</Button></td>
                        <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="使用日期">{data.USETIME}</Descriptions.Item>
                            <Descriptions.Item label="使用部门">{data.Nav_Department.NAME}</Descriptions.Item>
                            <Descriptions.Item label="记录人">{data.Nav_User.NAME}</Descriptions.Item>
                            <Descriptions.Item label="使用项目"  >{data.PROJECT}</Descriptions.Item>
                            <Descriptions.Item label="费用类型">{
                                data.Nav_SafePdtCostRecordType?.map((item, i) => {
                                    return (i > 0 ? "，" : "") + item.Nav_CostType.NAME
                                })
                            }</Descriptions.Item>
                            <Descriptions.Item label="用途">{data.PURPOSE}</Descriptions.Item>
                            <Descriptions.Item label="单位">{data.UNIT}</Descriptions.Item>
                            <Descriptions.Item label="数量">{data.COUNT}</Descriptions.Item>
                            <Descriptions.Item label="使用金额（万元）">{data.USE_AMOUNT}</Descriptions.Item>
                            <Descriptions.Item label="附件" span={3}>{
                                showFiles(data.Nav_Files, config.picServerHost)
                            }</Descriptions.Item>
                            {
                                data.STATUS > 20 ?
                                    <Descriptions.Item label="审阅意见" span={3}>{
                                        showApprove(data.Nav_Approve?.Nav_ApproveDetails, this.props.app.enums.NodeApproveStatus.enums, config.picServerHost, 100)
                                    }</Descriptions.Item>
                                    : null
                            }

                        </Descriptions>
                    </div> : null
                }
            </div>
        </div>
    }
}
export default connect(({ login, app }) => ({ login, app }))(TI008ShowPrint) 