import { Button, Descriptions, Badge, Popconfirm, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude,extendIgnoreDataRule, setDataFieldValue, showApprove, guid, initQueryFilter, 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 BS006View 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_CheckType');
        extendInclude(json, 'Nav_CheckTypeLevel.Nav_Enums');
        extendInclude(json, 'Nav_ListRiskSubmitContent');

        extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_SafeCheckDetail');
        // extendInclude(json, 'Nav_CheckTypeLevel.Nav_CheckType.Nav_ListCheckTypeLevel');
        // extendInclude(json, 'Nav_ListCheckRiskArea.Nav_RiskArea');
        // extendInclude(json, 'Nav_ListCheckProject.Nav_CheckProject');
        // extendInclude(json, 'Nav_ListCheckProjectCategory.Nav_CheckProjectCategory');
        extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_CheckMain');
        extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_Question');
        extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_CheckMain.Nav_CheckProject');
        // extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_User');
        extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_UserDeal');
        extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_UserCheck');
        extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_Department');
        extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_DepartmentDeal');
        extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_ListRiskQuestionReason.Nav_RiskReason');
        extendInclude(json, 'Nav_Files.Nav_ImgFile.Nav_File');
        // extendInclude(json, 'Nav_ApproveCheckAudit.Nav_ApproveDetails');
        extendIgnoreDataRule(json)

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

    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>
                    <tbody>
                        <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>
                    </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' }}>隐患上报查看</h1>
                {
                    //paddingLeft: '50px' 
                    data ? <div >
                        <Descriptions title="" size="middle" bordered>
                            <Descriptions.Item label="编码">{data.CODE}</Descriptions.Item>
                            <Descriptions.Item label="隐患来源">{data.NAME}</Descriptions.Item>
                            <Descriptions.Item label="检查任务">{data.Nav_Check != null ? data.Nav_Check.NAME : ""}</Descriptions.Item>
                            <Descriptions.Item label="检查类型">{data.Nav_CheckType != null ? data.Nav_CheckType.NAME : ""}</Descriptions.Item>
                            <Descriptions.Item label="检查层级">{data.Nav_CheckTypeLevel != null ? data.Nav_CheckTypeLevel.Nav_Enums.NAME : ""}</Descriptions.Item>
                            <Descriptions.Item label="检查时间">{data.CHECKTIME}</Descriptions.Item>

                            <Descriptions.Item label="附件">{
                                showFiles(data?.Nav_Files, config.picServerHost)
                                // 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
                }

                {
                    // data ? <table style={{ width: '100%', marginTop: '15px', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                    //     <tbody>
                    //         <tr>
                    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>
                                <td>验收人</td>

                            </tr>
                            {data.Nav_ListRiskSubmitContent?.map((item, i) => {
                                return <tr key={i}>
                                    {/* <td>{item?.Nav_CheckMain?.CHECKCONTENT}</td> */}
                                    <td>{item.Nav_RiskArea?.NAME}</td>
                                    <td>{item.DESCREPTION}</td>
                                    <td>{this.props.app.enums.BSQuestionLevelEnum.enums[item.QUESTION_LEVEL]}</td>
                                    {/* <td>{(item.Nav_SafeCheckDetail != null && item.Nav_SafeCheckDetail.CHECKRESULT != null && item.Nav_SafeCheckDetail.CHECKRESULT == 20) ? (item.Nav_SafeCheckDetail.Nav_CheckMain.Nav_CheckQuestion == null ? "" : item.Nav_SafeCheckDetail.Nav_CheckMain.Nav_CheckQuestion.DESCREPTION) : (item.Nav_SafeCheckDetail != null ? item.Nav_SafeCheckDetail.REMARK_RESULT : "")}</td> */}
                                    <td>{item.DEMAND}</td>
                                    {/* 后台获取代码 有问题  api/BS/BSRiskSubmit/Get */}
                                    <td>{
                                        item?.Nav_ListRiskQuestionReason?.map((itemReason, j) => {
                                            return (j > 0 ? "," : "") + itemReason?.Nav_RiskReason?.NAME
                                        })
                                    }</td>

                                    <td>{item.ADDRESS}</td>
                                    {/* <td>{item?.Nav_User?.NAME}</td>
                                    <td>{item?.Nav_Department?.NAME}</td> */}

                                    <td>{item.LastDateUser?.split(' ')[0]}</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={6}>
                                    {
                                        showApprove(data.Nav_ApproveCheckAudit?.Nav_ApproveDetails, this.props.app.enums.NodeApproveStatus.enums, config.picServerHost)
                                    }
                                </td>
                            </tr> */}
                        </tbody>
                    </table> : null
                }
            </div>
        </div>
    }
}

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