import { Button, Descriptions, Badge, Popconfirm, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, extendIgnoreDataRule, setDataFieldValue, showApprove, showUsersSign, guid, showFiles, 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 BS034ShowPrint 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_ListSafeCheckDetail.Nav_CheckMain');
        // extendInclude(json, 'Nav_ListSafeCheckDetail.Nav_CheckMain.Nav_CheckProject');
        // extendInclude(json, 'Nav_ListSafeCheckDetail.Nav_CheckMain.Nav_CheckQuestion');
        // extendInclude(json, 'Nav_ListCheckUserSign.Nav_ImgFile.Nav_File');
        //extendInclude(json, 'Nav_ListSafeCheckDetail.Nav_ListCheckDetailUser.Nav_User.Nav_UserSignFiles.Nav_ImgFile.Nav_File');

        extendInclude(json, 'Nav_CheckType');
        extendInclude(json, 'Nav_CheckTypeLevel.Nav_Enums');
        extendInclude(json, 'Nav_ListCheckRiskArea.Nav_RiskArea');
        extendInclude(json, 'Nav_ListCheckProject.Nav_CheckProject');
        extendInclude(json, 'Nav_ListCheckProjectCategory.Nav_CheckProjectCategory');
        extendInclude(json, 'Nav_Files.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_ListCheckUserSign.Nav_User.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_ApproveCheckAudit.Nav_ApproveDetails.Nav_ApproveUser.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendIgnoreDataRule(json)
        
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'BS/BSSafeCheck/GetCheckInfo034ShowPrint',
            onComplete: (ret) => {
                if (ret) {
                    let strRiskAreaNAME = ''
                    if (ret.Nav_ListCheckRiskArea && ret.Nav_ListCheckRiskArea.length > 0) {
                        for (let i = 0; i < ret.Nav_ListCheckRiskArea.length; i++) {
                            strRiskAreaNAME += (strRiskAreaNAME.length > 0 ? "," : "") + ret.Nav_ListCheckRiskArea[i].Nav_RiskArea.NAME;
                        }
                    }

                    let strCheckProjectNAME = ''
                    if (ret.Nav_ListCheckProject && ret.Nav_ListCheckProject.length > 0) {
                        for (let i = 0; i < ret.Nav_ListCheckProject.length; i++) {
                            strCheckProjectNAME += (strCheckProjectNAME.length > 0 ? "," : "") + ret.Nav_ListCheckProject[i].Nav_CheckProject.NAME;
                        }
                    }

                    let strProjectCategoryNAME = ''
                    if (ret.Nav_ListCheckProjectCategory && ret.Nav_ListCheckProjectCategory.length > 0) {
                        for (let i = 0; i < ret.Nav_ListCheckProjectCategory.length; i++) {
                            strProjectCategoryNAME += (strProjectCategoryNAME.length > 0 ? "," : "") + ret.Nav_ListCheckProjectCategory[i].Nav_CheckProjectCategory.NAME;
                        }
                    }

                    ret.RiskAreaNAME = strRiskAreaNAME
                    ret.CheckProjectNAME = strCheckProjectNAME
                    ret.ProjectCategoryNAME = strProjectCategoryNAME

                    this.setState({   //设置setState全局变量
                        data: ret,      //将ret对象赋值给data, data供页面调用
                    })
                }
            }
        });
    }

    // sureCheck = () => {
    //     let json = initFilter(this.props.login.OrgId, this.props.data.id, null, null, null, this.props.data.TaskID);
    //     // extendRule(json, 'ID', 1, this.props.data.id);
    //     // extendRule(json, "Parameter1", 1, this.props.data.TaskID)
    //     this.props.dispatch({
    //         type: 'app/getDataByPost',
    //         payload: json,
    //         url: 'BS/BSSafeCheck/sendCheck',
    //         onComplete: (ret) => {
    //             this.BtnClose()
    //         }
    //     });
    // }
    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,
        });
    }
    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")
    }

    render() {
        const { data } = this.state;
        return <div>
            <div style={{ padding: '10px' }}>
                <table>
                    <tr>
                        <Button onClick={() => this.onClickApprove(true)} style={{ marginRight: '8px' }} icon="export">审批详情</Button>
                        {/* <td><Button type={'primary'} icon={'check'} onClick={() => this.sureCheck()} >确定</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} >{data?.Nav_CheckType?.NAME}记录表</h1>
                {
                    //  paddingLeft: '50px'
                    data ? <div style={{ width: '95%' }}>
                        <Descriptions title="" size="middle" bordered>
                            <Descriptions.Item label="安全检查名称">{data.NAME?.replace('任务表', '记录表')}</Descriptions.Item>
                            <Descriptions.Item label="安全检查编号">{data.CODE}</Descriptions.Item>
                            <Descriptions.Item label="检查类型">{data.Nav_CheckType.NAME}</Descriptions.Item>
                            <Descriptions.Item label="检查范围">{data.CHECKOBJECT_DESCRIPTION}</Descriptions.Item>
                            <Descriptions.Item label="检查层级">{data.Nav_CheckTypeLevel.Nav_Enums.NAME}</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.CheckUsers}</Descriptions.Item>
                            <Descriptions.Item label="附件">{
                                showFiles(data.Nav_Files, config.picServerHost)
                            }</Descriptions.Item>
                        </Descriptions>
                    </div> : null
                }

                {
                    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>
                            </tr>
                            {data.Nav_ListSafeCheckDetail.map((item, i) => {
                                return <tr>
                                    <td>{item.Nav_CheckProject.NAME}</td>
                                    <td>{item.CHECKCONTENT}</td>
                                    {/* <td>{item.CHECKPROOF}</td> */}
                                    <td>{(item.CHECKRESULT == 0 || (item.CHECKRESULT != null && item.CHECKRESULT == 20)) ?
                                        item.Nav_ListCheckDetailQuestion?.map((question, j) => {
                                            return (j > 0 ? ',' : '') + question.Nav_Question.DESCREPTION
                                        })
                                        : item.CHECKRESULT_DESCRIPTION}</td>
                                    <td>{item.CHECKRESULT == 0 ? "" : item.QUESTION_LEVEL_DESCRIPTION}</td>
                                    <td>{(item.CHECKRESULT != null && item.CHECKRESULT == 20) ?
                                        item.Nav_ListCheckDetailQuestion?.map((question, j) => {
                                            return (j > 0 ? ',' : '') + question.Nav_Question.DEMAND
                                        })
                                        : ""}</td>


                                    {/* <td>{item.CHECKRESULT_DESCRIPTION}</td> */}

                                    <td>{
                                        item.Nav_ListCheckDetailUser.map((user, j) => {
                                            return (j > 0 ? "," : "") + user.Nav_User?.NAME
                                        })
                                    }</td>
                                    {/* <td>{item.REMARK_RESULT}</td> */}

                                    <td>{item.ADDRESS}</td>
                                    <td>{item.REMARK}</td>
                                    <td>{
                                        showFiles(item.Nav_Files, config.picServerHost)
                                    }</td>
                                </tr>

                            })}


                            <tr>
                                <td>检查人员签字
                                </td>
                                <td colSpan={8} style={{ textAlign: "left" }}>
                                    {
                                        showUsersSign(data.Nav_ListCheckUserSign, config.picServerHost)
                                        // data.Nav_ListCheckUserSign.map((sign, i) => {
                                        //     return <img width={'20%'} style={{ maxHeight: "200px", maxWidth: "200px" }} alt={sign.Nav_User.NAME + "签名"} src={config.picServerHost + sign.Nav_ImgFile.Nav_File.FILE_PATH} />
                                        // })
                                    }
                                </td>
                            </tr>


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

                                        // // 如果第一个有人 并且不是当前节点  不显示信息
                                        // (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 }))(BS034ShowPrint) 