import { Button, Descriptions, Badge, Popconfirm, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, setDataFieldValue, showApprove, guid, showFiles, initQueryFilter, ShowDateTime, ShowPrintClose } 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 CM034ShowPrint 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);
    }

    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,
        });
    }
    loadData = (id) => {
        var isCheck = (this.props.data.tableKey != undefined && this.props.data.tableKey == '1') ? true : false
        var orgId = this.props.login ? this.props.login.OrgId : '';
        let json = initFilter(orgId);
        extendRule(json, 'ID', 1, id);
        extendInclude(json, 'Nav_Name')
        extendInclude(json, 'Nav_UserRecord')
        extendInclude(json, 'Nav_UserManage')
        extendInclude(json, 'Nav_ListSummaryRequirement.Nav_TypeRequirement')
        extendInclude(json, 'Nav_ListUserEstimate.Nav_User')
        extendInclude(json, 'Nav_ListDepartment.Nav_Department')
        extendInclude(json, 'Nav_FilesSummary.Nav_ImgFile.Nav_File')
        extendInclude(json, 'Nav_FilesRecord.Nav_ImgFile.Nav_File')
        extendInclude(json, 'Nav_FilesStander.Nav_ImgFile.Nav_File')
        extendInclude(json, 'Nav_Approve.Nav_ApproveDetails.Nav_ApproveUser.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'CM/CMDrillRecord/GetOrder',
            onComplete: (ret) => {
                if (ret) {
                    this.setState({
                        data: ret,
                        isCheck: false
                    })
                }
            }
        });
    }
    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/CMEquipmentDemandSurvey/Sign',
            onComplete: (ret) => {
                ShowPrintClose(this.props)
            }
        });
    }
    render() {
        const { data, isCheck } = 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>
                        {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="应急演练编号">{data.CODE}</Descriptions.Item>
                            <Descriptions.Item label="演练主题">{data.NAME}</Descriptions.Item>
                            <Descriptions.Item label="应急演练名称">{data.Nav_Name?.NAME}</Descriptions.Item>
                            <Descriptions.Item label="演练时间">{data.TIME?.split(' ')[0]}</Descriptions.Item>
                            <Descriptions.Item label="演练地点">{data.PLACE}</Descriptions.Item>
                            <Descriptions.Item label="演练方式">{data.METHOD ? enums.CMDrillMethodEnum.enums[data.METHOD] : ""}</Descriptions.Item>
                            <Descriptions.Item label="记录人">{data.Nav_UserRecord?.NAME}</Descriptions.Item>
                            <Descriptions.Item label="现场总指挥">{data.Nav_UserManage?.NAME}</Descriptions.Item>
                            <Descriptions.Item label="评估人员">{data.Nav_ListUserEstimate?.map((item, i) => {
                                return (i > 0 ? ',' : '') + item.Nav_User.NAME
                                // return item.ISAGREE ? (<a style={{ color: "black" }}>{(i > 0 ? ',' : '') + item.Nav_User.NAME}</a>) : (<a style={{ color: "red" }}>{(i > 0 ? ',' : '') + item.Nav_User.NAME}</a>)
                            })}</Descriptions.Item>
                            <Descriptions.Item label="演练内容">{data.CONTENT}</Descriptions.Item>
                            <Descriptions.Item label="内部参与演练部门">{data.Nav_ListDepartment?.map((item, i) => {
                                return (i > 0 ? ',' : '') + item.Nav_Department.NAME
                            })}</Descriptions.Item>
                            <Descriptions.Item label="外部演练单位">{data.DEPARTMENT_OUT}</Descriptions.Item>
                            <Descriptions.Item label="演练总结要求">{data.Nav_ListSummaryRequirement?.map((item, i) => {
                                return (i > 0 ? ',' : '') + item.Nav_TypeRequirement.NAME
                            })}</Descriptions.Item>
                            <Descriptions.Item label="演练总结报告">{showFiles(data.Nav_FilesSummary, config.picServerHost)}</Descriptions.Item>
                            <Descriptions.Item label="演练活动记录">{showFiles(data.Nav_FilesRecord, config.picServerHost)}</Descriptions.Item>
                            <Descriptions.Item span={3} label="应急预案">{showFiles(data.Nav_FilesStander, config.picServerHost)}</Descriptions.Item>
                            {
                                data.STATUS >= 10 ?
                                    <Descriptions.Item label="审核意见" span={3}>{
                                        showApprove(data.Nav_Approve?.Nav_ApproveDetails, this.props.app.enums.NodeApproveStatus.enums, config.picServerHost)
                                    }</Descriptions.Item>
                                    : null
                            }

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