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, showUsersSign, showUserSign } 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 AE018ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            item: null,
            btndisplay: 'none',
            isCheck: false,
            listSign: []
        }
    }

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

    onClickCheck = () => {
        let json = initFilter(this.props.login.OrgId, this.props.data.id, null, null, null, this.props.data.TaskID);
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'AE/AEInvestigationRecord/Read',
            onComplete: (ret) => {
                this.props.data.onCancel();
            }
        });
    }

    //审批意见 改变
    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_Department');
        extendInclude(json, 'Nav_User');
        extendInclude(json, 'Nav_Accident');
        extendInclude(json, 'Nav_UsersInvestigation.Nav_User.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_UsersAsk.Nav_User');
        extendInclude(json, 'Nav_FilesAcc.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_FilesChart.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_NotesFilesNotes.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_LeaderUser.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendIgnoreDataRule(json)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'AE/InvestigationRecord/Get',
            onComplete: (ret) => {    //返回查询结果ret
                var IsCheck = true
                if ((this.props.data.tableKey == null) || (this.props.data.tableKey != undefined && this.props.data.tableKey == "2")) {
                    IsCheck = false
                }

                var ListSign = []
                if (ret.Nav_UsersInvestigation != null && ret.Nav_UsersInvestigation.length > 0) {
                    for (let i = 0; i < ret.Nav_UsersInvestigation.length; i++) {
                        if (ret.Nav_UsersInvestigation[i].STATUS != 0) {
                            ListSign.push(ret.Nav_UsersInvestigation[i]);
                        }
                    }
                }
                this.setState({   //设置setState全局变量
                    data: ret,      //将ret对象赋值给data, data供页面调用
                    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>
                        <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 onClick={() => this.onClickCheck()} style={{ marginLeft: '8px' }} type={'primary'} icon={'save'} >确认</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>
                        <Descriptions title="" size="middle" bordered>
                            <Descriptions.Item label="创建日期">{data.LAUNCH_TIME?.split(' '[0])}</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_Accident.CODE}</Descriptions.Item>
                            <Descriptions.Item label="事故事件名称">{data.Nav_Accident.NAME}</Descriptions.Item>
                            <Descriptions.Item label="勘察时间">{data.INVESTIGATION_TIME}</Descriptions.Item>
                            <Descriptions.Item label="勘察地点">{data.INVESTIGATION_LOCATION}</Descriptions.Item>
                            <Descriptions.Item label="勘察事由">{data.INVESTIGATION_REASON}</Descriptions.Item>
                            <Descriptions.Item label="勘察人员">{data.Nav_UsersInvestigation?.map((item, i) => {
                                return <a style={{ color: item.STATUS == 0 ? "red" : "#000000A6" }}> {(i > 0 ? "，" : "") + item.Nav_User.NAME}</a>
                            })}</Descriptions.Item>
                            <Descriptions.Item label="询问人员">{data.Nav_UsersAsk?.map((item, i) => {
                                return (i > 0 ? "，" : "") + item.Nav_User.NAME
                            })}</Descriptions.Item>
                            <Descriptions.Item label="事故现场外围和环境情况">{data.SCENE_ENV_DESC}</Descriptions.Item>
                            <Descriptions.Item label="事故现场情况">{data.SCENE_DESC}</Descriptions.Item>
                            <Descriptions.Item label="现场物证情况">{data.SCENE_EVIDENCE_DESC}</Descriptions.Item>
                            <Descriptions.Item label="事故照片">{
                                showFiles(data.Nav_FilesAcc, config.picServerHost)}</Descriptions.Item>
                            <Descriptions.Item label="绘制事故图">{
                                showFiles(data.Nav_FilesChart, config.picServerHost)}</Descriptions.Item>
                            <Descriptions.Item label="现场笔录" span={3}>{
                                showFiles(data.Nav_NotesFilesNotes, config.picServerHost)
                            }</Descriptions.Item>

                            <Descriptions.Item span={3} label="勘察人员签名">{
                                // showFiles(data.Nav_NotesFilesNotes, config.picServerHost)
                                showUsersSign(listSign, config.picServerHost, 80, 35)
                            }</Descriptions.Item>

                            <Descriptions.Item span={3} label="审阅意见">{
                                data.STATUS > 20 ?
                                    showUserSign(data.Nav_LeaderUser, config.picServerHost, 80, 35)
                                    : null
                                // showFiles(data.Nav_NotesFilesNotes, config.picServerHost)

                            }</Descriptions.Item>

                        </Descriptions>
                    </div> : null
                }

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