import { Button, Descriptions } from 'antd';
import React from 'react';
import { initFilter, extendRule, guid, extendInclude, extendIgnoreDataRule } from "../../../utils/common";
import ReactToPrint from "react-to-print";
import { ExportToExcel } from '@woowalker/feui'
import XLSX from 'xlsx';
import { connect } from 'dva';
import config from "../../../config.js";
import FormPage from '../../../components/FormPage'


class BS048ShowPrint 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);
        }
    }

    loadData = () => {
        let json = initFilter(this.props.login.OrgId);
        extendRule(json, 'ID', 1, this.props.data.id);
        extendInclude(json, 'Nav_CheckProject');
        extendInclude(json, 'Nav_CheckQuestion');
        extendInclude(json, 'Nav_CheckType');
        extendInclude(json, 'Nav_CheckTypeLevel.Nav_Enums');
        extendInclude(json, 'Nav_RiskArea');
        extendInclude(json, 'Nav_SafeCheck');
        extendInclude(json, 'Nav_SafeCheckDetail');
        extendIgnoreDataRule(json)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'BS/RiskSubmitContentTemp/Get',
            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(), param: this.props.data.param, appVisible: appVisible, BtnAgreeDisplay: 'none' },
            formCode: "PF123",
        }
        this.setState({
            tmpData: newtmpData,
        });
    }
    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="检查范围">{this.props.app.enums.BSMineTypeEnum.enums[data.CHECKOBJECT]}</Descriptions.Item>
                            <Descriptions.Item label="检查项目">{data.Nav_CheckProject.NAME}</Descriptions.Item>
                            <Descriptions.Item label="检查问题描述">{data.DESCREPTION}</Descriptions.Item>

                            <Descriptions.Item label="整改建议与措施">{data.DEMAND}</Descriptions.Item>
                            <Descriptions.Item label="检查内容">{data.CHECKCONTENT}</Descriptions.Item>
                            <Descriptions.Item label="检查依据">{data.CHECKPROOF}</Descriptions.Item>
                            <Descriptions.Item label="问题等级">{this.props.app.enums.BSQuestionLevelEnum.enums[data.QUESTION_LEVEL]}</Descriptions.Item>
                            <Descriptions.Item label="是否重大隐患">{this.props.app.enums.BSSeriousRiskEnum.enums[data.SERIOUS_RISK]}</Descriptions.Item>
                            <Descriptions.Item label="检查类型">{data.Nav_CheckType.NAME}</Descriptions.Item>
                            <Descriptions.Item label="检查层级">{data.Nav_CheckTypeLevel.Nav_Enums.NAME}</Descriptions.Item>
                            <Descriptions.Item label="检查区域">{data.Nav_RiskArea.NAME}</Descriptions.Item>
                            <Descriptions.Item label="状态">{this.props.app.enums.OrderType.enums[data.ORDERTYPE]}</Descriptions.Item>

                        </Descriptions>
                    </div> : null
                }
            </div>
        </div>
    }
}
export default connect(({ login, app }) => ({ login, app }))(BS048ShowPrint) 