import { Button, Descriptions, Badge, Popconfirm, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, extendIgnoreDataRule, setDataFieldValue, guid, showUsersSign, showApprove, initQueryFilter, ShowPrintClose, showFiles } 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 BS034View extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            safeCheckID:'',
            BtnAgreeDisplay: 'none',
            tmpData: {},
            checkSign: null
        };
    };

    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 = () => {
        var that = this
        let json = initFilter(this.props.login.OrgId);
        extendRule(json, 'ID', 1, this.props.data.id);
        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_ImgFile.Nav_File');
        extendInclude(json, 'Nav_ListSafeCheckDetail.Nav_Files.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/GetCheckAllInfo034View',//GetCheckAllInfo
            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


                    // 制表审核
                    // 检查结果审核
                    var checkSign = []
                    var checkerID = []
                    for (let i = 0; i < ret.Nav_ListSafeCheckDetail.length; i++) {
                        for (let j = 0; j < ret.Nav_ListSafeCheckDetail[i].Nav_ListCheckDetailUser.length; j++) {
                            if (ret.Nav_ListSafeCheckDetail[i].Nav_ListCheckDetailUser[j].SAFECHECKSTATE != 80) {
                                continue
                            }
                            if (checkerID.indexOf(ret.Nav_ListSafeCheckDetail[i].Nav_ListCheckDetailUser[j].USER_ID) > -1) {
                                continue
                            }

                            checkerID.push(ret.Nav_ListSafeCheckDetail[i].Nav_ListCheckDetailUser[j].USER_ID)
                            checkSign.push(ret.Nav_ListSafeCheckDetail[i].Nav_ListCheckDetailUser[j])
                        }
                    }
                    this.setState({   //设置setState全局变量
                        data: ret,      //将ret对象赋值给data, data供页面调用
                        safeCheckID:ret.ID,
                        checkSign: checkSign
                    })

                    // 偶尔 没有做安全检查  待办就归档了
                    // 如果是 已办事项打开  检查日期是今天   自己是主要检查人  自己红色的
                    //修改待办为 未办  update T_FM_NOTIFICATION_TASK set NOTICE_STATUS=0 ,SOURCE_FORMCODE='BS057'  where  ID='ABFE83C7-E912-48B3-95DC-FA0464D5FBEF'
                    //记录日志


                    // 检查 主要检查人是登陆者 所有的明细 没有检查问题
                    if (that.props.data.tableKey != undefined && that.props.data.tableKey == "2") {
                        var isUpdateTask = true
                        for (let j = 0; j < ret.Nav_ListSafeCheckDetail.length; j++) {
                            if (isUpdateTask && (ret.Nav_ListSafeCheckDetail[j].Nav_ListCheckDetailQuestion != null && ret.Nav_ListSafeCheckDetail[j].Nav_ListCheckDetailQuestion.length > 0)) {
                                isUpdateTask = false
                            }
                        }
                        if (isUpdateTask) {
                            let json2 = initFilter(that.props.login.OrgId, that.props.data.TaskID);
                            that.props.dispatch({
                                type: 'app/getDataByPost',
                                payload: json2,
                                url: 'BS/BSSafeCheck/BS034View2BS057',
                                onComplete: (ret2) => {
                                    if (ret2 != undefined && ret2 != null && ret2 == 1) {
                                        alert("请刷新页面后再操作！")
                                        that.props.data.onCancel();
                                    }
                                }
                            })
                        }
                    }
                }
            }
        });
    }

    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' }, 数据结构有修改故这边也修改
            data: { id: this.state.safeCheckID, isShow: true, key: guid(), PARAM2: this.props.data.PARAM2, appVisible: appVisible, BtnAgreeDisplay: 'none' },
            formCode: "PF123",
        }
        this.setState({
            tmpData: newtmpData,
        });
    }
    render() {
        const { data, checkSign } = 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 style={{ textAlign: 'center', margin: '15px' }}>{data?.Nav_CheckType?.NAME}记录表</h1>
                {
                    //, paddingLeft: '50px'
                    data ? <div>
                        <Descriptions title="" size="middle" bordered>
                            <Descriptions.Item span={2} label="安全检查名称">{data.NAME?.replace('任务表', '记录表')}</Descriptions.Item>
                            <Descriptions.Item span={2} label="安全检查编号">{data.CODE}</Descriptions.Item>
                            <Descriptions.Item span={2} label="检查类型">{data.Nav_CheckType.NAME}</Descriptions.Item>
                            <Descriptions.Item span={2} label="检查范围">{data.CHECKOBJECT_DESCRIPTION}</Descriptions.Item>
                            <Descriptions.Item span={2} label="检查层级">{data.Nav_CheckTypeLevel.Nav_Enums.NAME}</Descriptions.Item>
                            <Descriptions.Item span={2} label="检查时间">{data.CHECKTIME}</Descriptions.Item>
                            <Descriptions.Item span={2} label="检查区域">{
                                // data.RiskAreaNAME
                                data?.Nav_ListCheckRiskArea?.map((item, i) => {
                                    return <p className={styles.pLin}> {item.Nav_RiskArea.NAME}</p>
                                })
                            }</Descriptions.Item>
                            <Descriptions.Item span={2} label="检查项目">{
                                //data.CheckProjectNAME
                                data?.Nav_ListCheckProject?.map((item, i) => {
                                    return <p className={styles.pLin}> {item.Nav_CheckProject.NAME}</p>
                                })
                            }</Descriptions.Item>
                            <Descriptions.Item span={2} label="检查项目分类">{
                                data?.Nav_ListCheckProjectCategory?.map((item, i) => {
                                    // return (i > 0 ? "," : "") + item.Nav_CheckProjectCategory.NAME
                                    return <p className={styles.pLin}> {item.Nav_CheckProjectCategory.NAME}</p>
                                })
                            }</Descriptions.Item>
                            <Descriptions.Item span={2} label="检查人员">{data.CheckUsers}</Descriptions.Item>
                            <Descriptions.Item span={2} label="附件">{
                                showFiles(data.Nav_Files, config.picServerHost)
                                // data.Nav_Files && data.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
                }

                {
                    data ? <table className={styles.PrintFormLight}>
                        <tbody>
                            <tr className={styles.PrintFormLightTh}>
                                {/* <td>检查项目</td>
                                <td>检查内容</td>
                                <td>检查依据</td>
                                <td>问题描述</td>
                                <td>整改建议与措施</td>
                                <td width={100}>问题等级</td>
                                <td>检查结果</td>
                                <td>备注</td>
                                <td>检查人员</td> */}


                                <td>检查区域</td>
                                <td>检查项目</td>
                                <td>检查内容</td>
                                {/* <td>检查依据</td> */}
                                <td>问题描述</td>
                                <td>问题等级</td>
                                <td style={{ maxWidth: "20%" }}>整改建议与措施</td>
                                <td style={{ minWidth: "110px" }} >检查人员</td>
                                {/* <td>备注</td> */}
                                <td>附件</td>
                            </tr>
                            {data.Nav_ListSafeCheckDetail.map((item, i) => {
                                return <tr key={i}>
                                    <td >{item.Nav_CheckArea?.NAME}</td>
                                    <td>{item.Nav_CheckProject.NAME}</td>
                                    <td>{item.CHECKCONTENT}</td>
                                    {/* <td>{item.CHECKPROOF}</td> */}
                                    {/* <td>{(item.CHECKRESULT != null && item.CHECKRESULT == 20) ? item.Nav_CheckQuestion.DESCREPTION : item.CHECKRESULT_DESCRIPTION}</td> */}

                                    <td>{(item.CHECKRESULT == 0 || (item.CHECKRESULT != null && item.CHECKRESULT == 20)) ?
                                        item.Nav_ListCheckDetailQuestion?.map((question, j) => {
                                            return <p className={styles.pLin}> {question.Nav_Question.DESCREPTION}</p>
                                            // return (j > 0 ? ',' : '') + question.Nav_Question.DESCREPTION
                                        })
                                        : item.CHECKRESULT_DESCRIPTION}</td>

                                    <td>{item.QUESTION_LEVEL_DESCRIPTION}</td>
                                    {/* <td>{(item.CHECKRESULT != null && item.CHECKRESULT == 10) ? "" : item.Nav_CheckQuestion?.DEMAND}</td> */}

                                    <td>{(item.CHECKRESULT != null && item.CHECKRESULT == 20) ?
                                        item.Nav_ListCheckDetailQuestion?.map((question, j) => {
                                            return <p className={styles.pLin}> {question.Nav_Question.DEMAND}</p>

                                            // return (j > 0 ? ',' : '') + question.Nav_Question.DEMAND
                                            //return (j > 0 ? ',' : '') + (question.Nav_Question.ID == "55555555-5555-5555-5555-555555555555" || question.Nav_Question.ID == "77777777-7777-7777-7777-777777777777") ? "" : question.Nav_Question.DEMAND
                                        })
                                        : ""}</td>
                                    {/* <td>{item.CHECKRESULT_DESCRIPTION}</td> */}

                                    <td>{
                                        item.Nav_ListCheckDetailUser.map((user, j) => {
                                            if (user.SAFECHECKSTATE == 80) {
                                                return <a style={{ color: "rgba(0, 0, 0, 0.65)" }}>{(j > 0 ? "," : "") + user.Nav_User.NAME}</a>
                                            } else {
                                                return <a style={{ color: "red" }}>{(j > 0 ? "," : "") + user.Nav_User.NAME}</a>
                                            }
                                        })

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


                                    <td>{
                                        showFiles(item.Nav_Files, config.picServerHost)
                                    }</td>
                                    {/* <td>{item.Nav_CheckMain.Nav_CheckProject.NAME}</td>
                                    <td>{item.Nav_CheckMain.CHECKCONTENT}</td>
                                    <td>{item.Nav_CheckMain.CHECKPROOF}</td>
                                    <td>{item.Nav_CheckMain.Nav_CheckQuestion.DEMAND}</td>
                                    <td >{item.QUESTION_LEVEL_DESCRIPTION}</td>
                                    <td>{item.CHECKRESULT_DESCRIPTION}</td>
                                    <td>{item.REMARK_RESULT}</td>

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

                            {/* <tr>
                                <td>制表审核
                                </td>
                                <td colSpan={7}>
                                    {
                                        (data.Nav_Approve && data.Nav_Approve.Nav_ApproveDetails) ? data.Nav_Approve?.Nav_ApproveDetails?.map((approvedetail, i) => {
                                            return (i > 0 ? ";" : "") + (approvedetail.Nav_ApproveUser.NAME ? approvedetail.Nav_ApproveUser.NAME : '') + "(" + approvedetail.NAME ? approvedetail.NAME : '' + "):" + (approvedetail.NODE_APPROVE_TIME == '0001-01-01 00:00:00' ? "待审核" : approvedetail.NODE_APPROVE_TIME)
                                        }) : ''
                                    }
                                </td>
                            </tr> */}

                            <tr>
                                <td>检查人员签字
                                </td>
                                <td colSpan={7} style={{ textAlign: "left" }}>
                                    {
                                        showUsersSign(checkSign, config.picServerHost, 200)
                                    }
                                </td>
                            </tr>
                            <tr>
                                <td>审核意见
                                </td>
                                <td colSpan={7} style={{ textAlign: "left" }}>
                                    {
                                        showApprove(data.Nav_ApproveCheckAudit?.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 }))(BS034View) 