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, ShowDateTime } 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 CM018ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            btndisplay: 'none',
            isCheck: false, //是否待办 审批
        }
    }
    componentDidMount() {
        if (this.props.data?.id)
            this.loadData(this.props.data?.id, this.props.data?.ID);
    }

    componentWillReceiveProps(NextProps) {
        if (NextProps.data?.id && this.props.data?.id != NextProps.data?.id) {
            this.loadData(NextProps.data?.id, NextProps.data?.ID);
        }
    }

    onTableBtnExport() {
        let TableWrap = document.getElementById('table1');
        let Table = TableWrap.getElementsByTagName('table1')[0];
        const wb = XLSX.utils.table_to_book(Table);
        XLSX.writeFile(wb, this.props.record.Nav_Department.NAME + '.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,
        });
    }
    loadData = (id, type) => {
        var orgId = this.props.login ? this.props.login.OrgId : '';
        let json = initFilter(orgId);
        if (type)//库中过来
            extendRule(json, 'PLANID', 1, id);
        else
            extendRule(json, 'ID', 1, id);

        extendInclude(json, 'Nav_Department')
        extendInclude(json, "Nav_User")
        extendInclude(json, "Nav_EventType")
        extendInclude(json, "Nav_ListEventRiskType.Nav_RiskType")
        extendInclude(json, "Nav_ListArearInfluence.Nav_CoverageInfluence")
        extendInclude(json, "Nav_ListEventContingencyPlan.Nav_ContingencyType")
        extendInclude(json, "Nav_ListEventContingencyPlan.Nav_Department")
        extendInclude(json, "Nav_ListEventContingencyPlan.Nav_User")
        extendIgnoreDataRule(json)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'CM/CMUrgentEvent/GetShowPrint',
            onComplete: (ret) => {
                if (ret) {
                    this.setState({
                        data: ret,
                    })
                }
            }
        });
    }
    render() {
        const { data } = this.state;
        const enums = this.props.data.enums ? this.props.data.enums : this.props.app.enums;

        return <div>
            <div style={{ padding: '10px' }}>
                <table>
                    <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>
                </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}>潜在紧急事件</h1>
                {
                    data ? <div style={{ width: '95%' }}>
                        <Descriptions title='' size='middle' bordered>
                            <Descriptions.Item label="编号">{data.CODE}</Descriptions.Item>
                            <Descriptions.Item label="发起部门">{data.Nav_Department.NAME}</Descriptions.Item>
                            <Descriptions.Item label="发起人">{data.Nav_User.NAME}</Descriptions.Item>
                            <Descriptions.Item label="事件类型">{data.Nav_EventType.NAME}</Descriptions.Item>
                            <Descriptions.Item label="影响地点">{data.ADDRESS_INFLUENCE}</Descriptions.Item>
                            <Descriptions.Item label="预计影响范围">{data.Nav_ListArearInfluence?.map((item, i) => {
                                return (i > 0 ? "," : "") + item.Nav_CoverageInfluence.NAME
                            })}</Descriptions.Item>
                            <Descriptions.Item label="预计影响范围描述">{data.AREAR_INFLUENCE}</Descriptions.Item>
                            <Descriptions.Item label="事故后果">{data.ACCIDENT_CONSEQUENCE}</Descriptions.Item>
                            <Descriptions.Item label="风险类别">{data.Nav_ListEventRiskType?.map((item, i) => {
                                return (i > 0 ? "," : "") + item.Nav_RiskType.NAME
                            })}</Descriptions.Item>
                            <Descriptions.Item label="启用标志">{enums.FMEnableStatusEnum.enums[data.ENABLE_STATUS]}</Descriptions.Item>
                        </Descriptions>
                    </div> : null
                }
                {
                    (data?.Nav_ListEventContingencyPlan && data?.Nav_ListEventContingencyPlan.length > 0) ?
                        <table className={styles.PrintFormLight}>
                            <tbody>
                                <tr className={styles.PrintFormLightTh}>
                                    <td>预案类型</td>
                                    <td>是否编制</td>
                                    <td>责任部门</td>
                                    <td>责任人</td>
                                    <td>要求完成时间</td>
                                </tr>
                                {data?.Nav_ListEventContingencyPlan?.map((item, i) => {
                                    return <tr>
                                        <td>{item.Nav_ContingencyType.NAME} </td>
                                        <td>{enums.YesNoEnum.enums[item.IS_DRAW_UP]} </td>
                                        <td>{item.Nav_Department.NAME} </td>
                                        <td>{item.Nav_User.NAME} </td>
                                        <td>{ShowDateTime(item.COMPLETE_TIME, "yyyy-MM-dd")} </td>
                                    </tr>
                                })}
                            </tbody>
                        </table>
                        : null
                }
            </div>
        </div>
    }
}
export default connect(({ login, app }) => ({ login, app }))(CM018ShowPrint) 
