import { Checkbox, message, Radio } from "antd/lib/index";
import { Button, Descriptions, Badge, Popconfirm, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, extendIgnoreDataRule, setDataFieldValue, guid, initQueryFilter, showFiles, showApprove } from "../../../utils/common";
import { ExportToExcel } from '@woowalker/feui'
import styles from '../HI/StepForm.css';
import ReactToPrint from "react-to-print";
import moment from 'moment';
import config from "../../../config.js";
import XLSX from 'xlsx';
import { connect } from 'dva';
import FormPage from '../../FormPage'

class AE006ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            item: null,
            btndisplay: 'none',
            ALLTOTAL: 0,
            ALLUSE: 0,
            ALLLEFT: 0
        }
    }

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

    onTableBtnExport() {
        // let TableWrap = document.getElementById('table1' + this.props.record.ID);
        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,
        });
    }

    //审批意见 改变
    NOTJOINREASONCHANGE = (val) => {
        this.state.NOTJOINREASON = val
    }


    //加载数据(传过来的参数都在props里面，包括登录信息)
    loadData = (id) => {
        var orgId = this.props.login ? this.props.login.OrgId : '';   //登录后有存储登录信息
        let json = initFilter(orgId);
        extendRule(json, 'ID', 1, id);    //ID=‘’，字段，等于，条件值， 1代表=，2代表不等于，3小于，4小于等于，5大于，6大于等于，7以开始的Like,8以结束的Like，9包含 
        extendInclude(json, 'Nav_User');
        extendInclude(json, 'Nav_ListAccidentListKind.Nav_AccidentKind');
        extendInclude(json, 'Nav_AccidentType');
        extendInclude(json, 'Nav_Department');
        extendInclude(json, 'Nav_Files.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_Approve.Nav_ApproveDetails.Nav_ApproveUser.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendIgnoreDataRule(json)

        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'AE/AccidentEventReport/Get',
            onComplete: (ret) => {    //返回查询结果ret
                this.setState({   //设置setState全局变量
                    data: ret,      //将ret对象赋值给data, data供页面调用
                })
            }
        });
    }

    //渲染页面
    render() {
        const { data, ALLTOTAL, ALLUSE, ALLLEFT } = 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>
                        <Descriptions title="" size="middle" bordered>

                            <Descriptions.Item label="事故编号">{data.CODE}</Descriptions.Item>
                            <Descriptions.Item label="事故名称">{data.NAME}</Descriptions.Item>
                            <Descriptions.Item label="上报时间">{data.TIME}</Descriptions.Item>
                            <Descriptions.Item label="上报人员">{data.Nav_User.NAME}</Descriptions.Item>
                            <Descriptions.Item label="事故时间">{data.ACCIDENT_TIME}</Descriptions.Item>
                            <Descriptions.Item label="事故地点">{data.ACCIDENT_PLACE}</Descriptions.Item>
                            <Descriptions.Item label="事故种类">{
                                data.Nav_ListAccidentListKind?.map((item, i) => {
                                    return (i > 0 ? "，" : "") + item.Nav_AccidentKind.NAME
                                })
                            }</Descriptions.Item>
                            <Descriptions.Item label="事故类型">{data.Nav_AccidentType.NAME}</Descriptions.Item>
                            <Descriptions.Item label="事故部门">{data.Nav_Department.NAME}</Descriptions.Item>
                            <Descriptions.Item label="事故等级">{enums.AEAccidentLevel.enums[data.ACCIDENT_LEVEL]}</Descriptions.Item>
                            <Descriptions.Item label="事故起因">{data.ACCIDENT_REASON}</Descriptions.Item>
                            <Descriptions.Item label="预估伤亡人数">{data.ESTIMATE_CASUALTY_COUNT}</Descriptions.Item>
                            <Descriptions.Item label="直接经济损失（元）">{data.ECONOMIC_LOSSES}</Descriptions.Item>
                            <Descriptions.Item label="波及范围">{data.IMPACT_SCOPE}</Descriptions.Item>
                            <Descriptions.Item label="已采取措施">{data.TAKE_MEASURES}</Descriptions.Item>
                            <Descriptions.Item label="其他现状备注">{data.REMARK}</Descriptions.Item>
                            <Descriptions.Item label="事故现场">{
                                showFiles(data.Nav_Files, config.picServerHost)
                            }</Descriptions.Item>
                            <Descriptions.Item label="响应级别">{enums.AERspLevel.enums[data.RSP_LEVEL]}</Descriptions.Item>
                            <Descriptions.Item label="下一步工作建议" span={3}>{data.WORK_SUGGEST}</Descriptions.Item>
                            {
                                data.STATUS >= 20 ?
                                    <Descriptions.Item label="审核意见" span={3}>{
                                        showApprove(data.Nav_Approve?.Nav_ApproveDetails, this.props.app.enums.NodeApproveStatus.enums, config.picServerHost, 100, 65)
                                    }</Descriptions.Item>
                                    : null
                            }
                        </Descriptions>
                    </div> : null
                }

            </div>
        </div>
    }
}
export default connect(({ login, app }) => ({ login, app }))(AE006ShowPrint) 