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, showUsersSign } 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 '../../FormPage'

class CM023ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            btndisplay: 'none',
            isCheck: false, //是否待办 审批
            listSign: null
        }
    }
    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');
        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,
        });
    }

    sign = () => {
        let json = initFilter(this.props.login.OrgId, this.props.data.id, null, null, null, this.props.data.TaskID);
        extendRule(json, 'ID', 1, this.props.data.id);
        extendRule(json, "Parameter1", 1, this.props.data.TaskID)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'CM/CMRspEstimate/Sign',
            onComplete: (ret) => {
                this.BtnClose()
            }
        });
    }

    BtnClose = () => {
        if (typeof this.props.data.onCancel != "undefined" && typeof this.props.data.onCancel == 'function')
            this.props.data.onCancel();
    }
    loadData = (id) => {
        var orgId = this.props.login ? this.props.login.OrgId : '';
        var isCheck = (this.props.data.tableKey != undefined && this.props.data.tableKey == "1") ? true : false
        let json = initFilter(orgId);
        extendRule(json, 'ID', 1, id);
        extendInclude(json, 'Nav_Department')
        extendInclude(json, 'Nav_User')
        extendInclude(json, 'Nav_UserReport')
        extendInclude(json, 'Nav_DepartmentReport')
        // extendInclude(json, 'Nav_AccidentType')
        extendInclude(json, 'Nav_ListJoin.Nav_User')
        extendInclude(json, 'Nav_ListRspConditions.Nav_RspEstimateCondition')
        // extendInclude(json, 'Nav_ListEventKind.Nav_RspAccdentKind')
        extendInclude(json, 'Nav_ListEstimate.Nav_User.Nav_UserSignFiles.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: 'CM/RspEstimate/Get',
            url: 'CM/CMRspEstimate/GetOrder',
            onComplete: (ret) => {
                if (ret) {
                    var listSign = []
                    if (isCheck && ret.STATUS == 10) {
                        if (ret.Nav_ListEstimate != null && ret.Nav_ListEstimate.length > 0) {
                            for (let i = 0; i < ret.Nav_ListEstimate.length; i++) {
                                if (ret.Nav_ListEstimate[i].ISSIGN) {
                                    listSign.push(ret.Nav_ListEstimate[i])
                                }

                                if (ret.Nav_ListEstimate[i].USER_ID == this.props.login.userId) {
                                    if (!ret.Nav_ListEstimate[i].ISSIGN) {
                                        isCheck = true
                                    } else {
                                        isCheck = false
                                    }
                                }
                            }
                        }
                    } else if (ret.STATUS > 10) {
                        listSign = ret.Nav_ListEstimate
                    }

                    this.setState({
                        data: ret,
                        isCheck: isCheck,
                        listSign: listSign
                    })
                }
            }
        });
    }
    render() {
        const { data, isCheck, listSign } = this.state;
        const enums = this.props.data.enums ? this.props.data.enums : this.props.app.enums;
        return <div>
            <div style={{ padding: '10px' }}>
                <table>
                    <tr>
                        {data?.STATUS > 10 ? <td><Button onClick={() => this.onClickApprove(true)} style={{ marginRight: '8px' }} icon='export'>审批详情</Button></td> : null}
                        {/* {isCheck ? <td><Button onClick={() => this.onClickApprove(true)} style={{ marginRight: '8px' }} icon='export'>确认</Button></td> : null} */}
                        <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>
                        {isCheck ? <td><Button type={'primary'} icon={'check'} onClick={() => this.sign()} >确认</Button>  </td> : null}
                    </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="发起时间">{ShowDateTime(data.LAUNCH_TIME, "yyyy-MM-dd")}</Descriptions.Item>
                            <Descriptions.Item label="发起部门">{data.Nav_Department.NAME}</Descriptions.Item>
                            <Descriptions.Item label="发起人">{data.Nav_User.NAME}</Descriptions.Item>
                            <Descriptions.Item label="事故/事件名称">{data.EVENT_NAME}</Descriptions.Item>
                            <Descriptions.Item label="事故/事件时间">{data.ACCIDENT_TIME}</Descriptions.Item>
                            <Descriptions.Item label="事故/事件地点">{data.ACCIDENT_PLACE}</Descriptions.Item>
                            <Descriptions.Item label="报告人员">{data.Nav_UserReport.NAME}</Descriptions.Item>
                            <Descriptions.Item label="事故/事件单位">{data.Nav_DepartmentReport.NAME}</Descriptions.Item>
                            {/* <Descriptions.Item label="事故/事件种类">{data.Nav_ListEventKind?.map((item, i) => {
                                return (i > 0 ? ',' : '') + item.Nav_RspAccdentKind.NAME
                            })}</Descriptions.Item> */}
                            {/* <Descriptions.Item label="事故类型">{data.Nav_AccidentType.NAME}</Descriptions.Item> */}
                            <Descriptions.Item label="事故等级">{enums.AEAccidentLevel.enums[data.ACCIDENT_LEVEL]}</Descriptions.Item>
                            <Descriptions.Item label="事故描述">{data.DESC}</Descriptions.Item>
                            <Descriptions.Item label="应急响应开始时间">{data.RSP_START_TIME}</Descriptions.Item>
                            <Descriptions.Item label="应急响应结束时间">{data.RSP_STOP_TIME}</Descriptions.Item>
                            <Descriptions.Item label="响应级别">{enums.AERspLevel.enums[data.RSP_LEVEL]}</Descriptions.Item>
                            <Descriptions.Item label="参与救援部门">{data.JoinDepartment}</Descriptions.Item>
                            <Descriptions.Item label="参与人员">{
                                data.Nav_ListJoin?.map((item, i) => {
                                    return (i > 0 ? ',' : '') + item.Nav_User.NAME
                                })
                            }</Descriptions.Item>
                            <Descriptions.Item label="预案运行情况">{data.RUN_SITUATION}</Descriptions.Item>
                            <Descriptions.Item label="现场恢复情况">{data.RESTORE_SITUATION}</Descriptions.Item>
                            <Descriptions.Item label="评估人员" column={16} >{
                                data.Nav_ListEstimate?.map((item, i) => {
                                    if (item.ISSIGN == true) {
                                        return <a style={{ color: "black" }}>{(i > 0 ? ',' : '') + item.Nav_User.NAME}</a>
                                    } else {
                                        return <a style={{ color: "red" }}>{(i > 0 ? ',' : '') + item.Nav_User.NAME}</a>
                                    }
                                })
                            }</Descriptions.Item>

                            {/* <Descriptions.Item column={24} label="应急响应情况">{data.Nav_ListRspConditions?.map((item, i) => {
                                return <p>{(i + 1) + "） " + item.Nav_RspEstimateCondition.NAME + " ：" + (item.RSP_CONDITION_STATUS != null ? enums.CMRspConditionStatus.enums[item.RSP_CONDITION_STATUS] : "")}</p>
                            })}</Descriptions.Item> */}

                            <Descriptions.Item label="应急响应条件">{enums.CMRspCondition.enums[data.RSP_CONDITION]}</Descriptions.Item>
                            <Descriptions.Item label="响应程序">{enums.CMRspOrder.enums[data.RSP_ORDER]}</Descriptions.Item>
                            <Descriptions.Item label="培训水平">{enums.CMRspTrainingLevel.enums[data.RSP_TRAINING_LEVEL]}</Descriptions.Item>
                            <Descriptions.Item label="指挥系统">{enums.CMRspCommandLevel.enums[data.RSP_COMMAND_LEVEL]}</Descriptions.Item>
                            <Descriptions.Item label="存在问题及改进建议">{data.ADVISE}</Descriptions.Item>
                            <Descriptions.Item label="评价结论">{data.CONCLUSION}</Descriptions.Item>
                            {
                                data.STATUS > 0 ? <Descriptions.Item label="签到区" span={3}>{
                                    showUsersSign(listSign, config.picServerHost)
                                }</Descriptions.Item> : null
                            }
                            {
                                data.STATUS > 10 ? <Descriptions.Item label="审核意见" span={3}>{
                                    showApprove(data.Nav_Approve?.Nav_ApproveDetails, this.props.app.enums.PFStandardStatus.enums, config.picServerHost)
                                }</Descriptions.Item> : null
                            }
                        </Descriptions>
                    </div> : null
                }

                {
                    data?.Nav_ListRspConditions ?
                        <table className={styles.PrintFormLight}>
                            <tbody>
                                <tr className={styles.PrintFormLightTh}>
                                    <td>序号</td>
                                    <td>应急响应评估项目</td>
                                    <td>结果</td>
                                </tr>
                                {data?.Nav_ListRspConditions?.map((item, i) => {
                                    return <tr>
                                        <td>{i + 1} </td>
                                        <td>{item.Nav_RspEstimateCondition.NAME} </td>
                                        <td>{item.RSP_CONDITION_STATUS != null ? enums.CMRspConditionStatus.enums[item.RSP_CONDITION_STATUS] : ""} </td>
                                    </tr>
                                })}
                            </tbody></table>
                        : null
                }
            </div>
        </div >
    }
}
export default connect(({ login, app }) => ({ login, app }))(CM023ShowPrint)