import { Button, Descriptions, Badge, Popconfirm, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule,extendIgnoreDataRule, extendInclude, setDataFieldValue, showApprove, guid, initQueryFilter } 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 '../HI/StepForm.css';
import config from "../../../config.js";
import FormPage from '../../../components/FormPage'


class BS006ShowPrint 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 = () => {
        // let json = initFilter(this.props.login.OrgId);
        // extendRule(json, 'ID', 1, this.props.data.id);
        // extendInclude(json, 'Nav_Submit');
        // extendInclude(json, 'Nav_Submit.Nav_Check.Nav_CheckType');
        // extendInclude(json, 'Nav_Submit.Nav_Check.Nav_CheckTypeLevel.Nav_Enums');
        // extendInclude(json, 'Nav_ListRiskSubmitContent');
        // extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_SafeCheckDetail');
        // extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_Question');
        // extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_ListRiskQuestionReason');
        // extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_ListRiskQuestionReason.Nav_RiskReason');
        // extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_User');
        // extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_Department');
        // extendInclude(json, 'Nav_Submit.Nav_Files.Nav_ImgFile.Nav_File');
        // extendInclude(json, 'Nav_Approve.Nav_ApproveDetails.Nav_ApproveUser.Nav_UserSignFiles.Nav_ImgFile.Nav_File');

        // this.props.dispatch({
        //     type: 'app/getDataByPost',
        //     payload: json,
        //     url: 'BS/RiskSubmitNotice/Get',//RiskSubmitNotice  RiskSubmit
        //     onComplete: (ret) => {
        //         if (ret) {
        //             this.setState({   //设置setState全局变量
        //                 data: ret,      //将ret对象赋值给data, data供页面调用
        //             })
        //         }
        //     }
        // });

        let json = initFilter(this.props.login.OrgId);
        extendRule(json, 'ID', 1, this.props.data.id);
        extendIgnoreDataRule(json)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'BS/BSRiskSubmitNotice/GetNotice',//RiskSubmitNotice  RiskSubmit
            onComplete: (ret) => {
                if (ret) {
                    this.setState({   //设置setState全局变量
                        data: ret,      //将ret对象赋值给data, data供页面调用
                    })
                }
            }
        });
    }

    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, "隐患上报表.xlsx")
    }

    onClickApprove = (appVisible) => {
        const newtmpData = {
            data: { id: this.props.data.id, isShow: true, key: guid(), PARAM2: this.props.data.PARAM2, appVisible: appVisible, BtnAgreeDisplay: 'none' },
            formCode: "PF123",
        }
        this.setState({
            tmpData: newtmpData,
        });
    }
    render() {
        const { data } = this.state;
        return <div>
            <div style={{ padding: '10px' }}>
                <table>
                    <tbody>
                        <tr>
                            <Button onClick={() => this.onClickApprove(true)} style={{ marginRight: '8px' }} icon="export">审批详情</Button>
                            <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>
                    </tbody>
                </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>
                {
                    //, paddingLeft: '50px'
                    data ? <div>
                        <Descriptions title="" size="middle" bordered>
                            {/* <Descriptions.Item label="隐患上报名称">{data.NAME}</Descriptions.Item>
                            <Descriptions.Item label="隐患上报编号">{data.Nav_Submit.CODE}</Descriptions.Item>
                            <Descriptions.Item label="检查任务">{data.Nav_Submit.Nav_Check ? data.Nav_Submit.Nav_Check.NAME : ""}</Descriptions.Item>
                             */}
                            <Descriptions.Item label="检查类型">{data.Nav_Submit?.Nav_CheckType?.NAME}</Descriptions.Item>
                            <Descriptions.Item label="检查层级">{data.Nav_Submit?.Nav_CheckTypeLevel.Nav_Enums.NAME}</Descriptions.Item>

                            <Descriptions.Item label="检查时间">{data.Nav_Submit.CHECKTIME}</Descriptions.Item>
                            {/* <Descriptions.Item label="状态">{data.SUBMIT_STATUS}</Descriptions.Item> */}
                            {/* <Descriptions.Item label="检查时间">{data.CHECKTIME}</Descriptions.Item> */}
                            {/* <Descriptions.Item label="检查区域">{data.RiskAreaNAME}</Descriptions.Item>
                            <Descriptions.Item label="检查项目">{data.CheckProjectNAME}</Descriptions.Item> */}
                            {/* ret.Nav_ListCheckProjectCategory[i].Nav_CheckProjectCategory.NAME */}
                            {/* <Descriptions.Item label="检查项目分类">{
                                // data.ProjectCategoryNAME
                                data.Nav_ListCheckProjectCategory && data.Nav_ListCheckProjectCategory.map((item, i) => {
                                    return (i > 0 ? "," : "") + item.Nav_CheckProjectCategory.NAME
                                })
                            }</Descriptions.Item> */}
                            <Descriptions.Item label="附件">{
                                data.Nav_Submit && data.Nav_Submit.Nav_Files && data.Nav_Submit.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
                }

                {
                    //  style={{ width: '100%', marginTop: '15px', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}
                    data ? <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>
                                <td>整改责任人</td>
                                <td>验收人</td>
                            </tr>
                            {data.Nav_ListRiskSubmitContent.map((item, i) => {
                                console.log(item)
                                return <tr>
                                    <td>{item.DESCREPTION}</td><td>{item.DEMAND}</td>
                                    <td>{this.props.app.enums.BSQuestionLevelEnum.enums[item.QUESTION_LEVEL]}</td>
                                    <td>
                                        {
                                            item.Nav_ListRiskQuestionReason && item.Nav_ListRiskQuestionReason.map((item2, i) => {
                                                if (i == (item.Nav_ListRiskQuestionReason.length - 1)) {
                                                    return item2.Nav_RiskReason.NAME
                                                } else {
                                                    return item2.Nav_RiskReason.NAME + ','
                                                }
                                            })
                                        }
                                    </td>
                                    <td >{item.ADDRESS}</td>
                                    <td >{
                                        item.Nav_Files && item.Nav_Files.map((itemF, i) => {
                                            return <a width={'20%'} title={itemF.Nav_ImgFile.FILE_NAME} target='_blank' href={config.picServerHost + itemF.Nav_ImgFile.Nav_File.FILE_PATH} >{itemF.Nav_ImgFile.FILE_NAME} </a>
                                        })
                                    }</td>
                                    {/* <td>{item.Nav_User?.NAME}</td>
                                    <td>{item.Nav_Department?.NAME}</td> */}

                                    <td>{item.LastDateUser}</td>
                                    <td>{item.Nav_DepartmentDeal?.NAME} </td>
                                    <td>{item.Nav_UserDeal?.NAME}</td>
                                    <td>{item.Nav_UserCheck?.NAME}</td>

                                </tr>
                            })}

                            <tr>
                                <td>审核意见
                                </td>
                                <td colSpan={11} style={{ textAlign: "left" }}>
                                    {
                                        showApprove(data.Nav_Approve?.Nav_ApproveDetails, this.props.app.enums.NodeApproveStatus.enums, config.picServerHost, 200)

                                        // // 如果第一个有人 并且不是当前节点  不显示信息
                                        // (data.STATUCHECK > 5 && data.Nav_ApproveCheckAudit && data.Nav_ApproveCheckAudit.Nav_ApproveDetails) ? data.Nav_ApproveCheckAudit?.Nav_ApproveDetails?.map((approvedetail, i) => {
                                        //     return (i > 0 ? ";" : "") + ((approvedetail.Nav_ApproveUser && approvedetail.Nav_ApproveUser.NAME) ? approvedetail.Nav_ApproveUser.NAME : '审批流未匹配到人员') + "(" + approvedetail.NAME + "):" + (approvedetail.NODE_APPROVE_TIME == '0001-01-01 00:00:00' ? "待审核" : approvedetail.NODE_APPROVE_TIME)
                                        // }) : ''
                                    }
                                </td>
                            </tr>
                        </tbody>
                    </table> : null
                }
            </div>
        </div>
    }

}

export default connect(({ login, app }) => ({ login, app }))(BS006ShowPrint) 