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 BS073ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            listReason: '',
            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.Nav_CheckType');
        extendInclude(json, 'Nav_Submit.Nav_CheckTypeLevel.Nav_Enums');
        extendInclude(json, 'Nav_Submit.Nav_User');
        extendInclude(json, 'Nav_Submit');
        extendInclude(json, 'Nav_Files.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_CheckProject');
        extendInclude(json, 'Nav_CheckProjectCategory');
        extendInclude(json, 'Nav_RiskArea');
        extendInclude(json, 'Nav_ListRiskQuestionReason.Nav_RiskReason');
        extendInclude(json, 'Nav_ListRiskQuestionReason');
        extendInclude(json, 'Nav_DepartmentDeal');
        extendInclude(json, 'Nav_UserDeal');
        extendInclude(json, 'Nav_UserCheck');
        extendIgnoreDataRule(json)

        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'BS/RiskSubmitContent/Get',
            onComplete: (ret) => {
                if (ret) {
                    // 制表审核
                    // 检查结果审核
                    var listReason = ''
                    ret.Nav_ListRiskQuestionReason.forEach(element => {
                        listReason += ' ' + element.Nav_RiskReason.NAME
                    });

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

    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, listReason } = this.state;
        const enums = this.props.data.enums ? this.props.data.enums : this.props.app.enums;
        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>
                {
                    data ? <div >
                        <Descriptions title="" size="middle" bordered>
                            <Descriptions.Item label="检查范围">{enums.BSMineTypeEnum.enums[data.Nav_Submit?.CHECKOBJECT]}</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?.Nav_User?.NAME}</Descriptions.Item>
                            <Descriptions.Item label="检查时间">{data.Nav_Submit.CHECKTIME}</Descriptions.Item>{/*CHECKTIME */}
                            <Descriptions.Item label="问题点描述" span={2}>{data.DESCRIBE}</Descriptions.Item>
                            <Descriptions.Item label="附件">{
                                showFiles(data?.Nav_Files, config.picServerHost)
                            }</Descriptions.Item>
                            <Descriptions.Item label="检查项目">{data.Nav_CheckProject?.NAME}</Descriptions.Item>
                            <Descriptions.Item label="检查项目分类">{data.Nav_CheckProjectCategory?.NAME}</Descriptions.Item>
                            <Descriptions.Item label="检查区域">{data.Nav_RiskArea?.NAME}</Descriptions.Item>
                            <Descriptions.Item label="隐患地点">{data.ADDRESS}</Descriptions.Item>
                            <Descriptions.Item label="检查内容">{data.CHECKCONTENT}</Descriptions.Item>
                            <Descriptions.Item label="检查问题">{data.DESCREPTION}</Descriptions.Item>
                            <Descriptions.Item label="问题等级">{enums.BSQuestionLevelEnum.enums[data.QUESTION_LEVEL]}</Descriptions.Item>
                            <Descriptions.Item label="整改建议与措施">{data.DEMAND}</Descriptions.Item>
                            <Descriptions.Item label="系统整改期限">{data.LastDateSystem?.split(' ')[0]}</Descriptions.Item>
                            <Descriptions.Item label="整改期限">{data.LastDateUser?.split(' ')[0]}</Descriptions.Item>
                            <Descriptions.Item label="隐患原因">{listReason}</Descriptions.Item>
                            <Descriptions.Item label="整改实施单位">{data.Nav_DepartmentDeal?.NAME}</Descriptions.Item>
                            <Descriptions.Item label="整改责任人">{data.Nav_UserDeal?.NAME}</Descriptions.Item>
                            <Descriptions.Item label="验收人">{data.Nav_UserCheck?.NAME}</Descriptions.Item>

                        </Descriptions>
                    </div> : null
                }
            </div>
        </div>
    }
}

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