import { Button, Descriptions, Badge, Popconfirm, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, setDataFieldValue, showApprove, guid, initQueryFilter, ShowDateTime, showUsersSign, showFiles } 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 '../../../components/CustomPages/HI/StepForm.css';
import config from '../../../config.js';
import FormPage from '../../FormPage'
class TL037ShowPrint 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')

    }

    sureCheck = () => {
        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: 'TL/TLWatchDayly/sendCheck',
            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 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_Tailing')
        extendInclude(json, 'Nav_ListDetail.Nav_ListUser.Nav_User.Nav_UserSignFiles.Nav_ImgFile.Nav_File')
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'TL/WatchDayly/Get',
            onComplete: (ret) => {
                if (ret) {
                    let users = []
                    let userID = []
                    if (ret.Nav_ListDetail && ret.Nav_ListDetail.length > 0) {
                        ret.Nav_ListDetail.forEach(e => {
                            if (e.Nav_ListUser != null && e.Nav_ListUser.length > 0) {
                                e.Nav_ListUser.forEach(eu => {
                                    if (eu.ISCHECK && eu.ISCHECK == true && userID.indexOf(eu.USER_ID) == -1) {
                                        userID.push(eu.USER_ID)
                                        users.push(eu)
                                    }
                                })
                            }
                        });
                    }
                    this.setState({
                        data: ret,
                        isCheck: isCheck,
                        users: users
                    })
                }
            }
        });
    }
    render() {
        const { data } = this.state;
        return <div>
            <div style={{ padding: '10px' }}>
                <table>
                    <tr>
                        <td><ReactToPrint trigger={() => <Button type={'default'} icon={'printer'} >打印</Button>} content={() => this.componentRef} /></td>
                        <td><ExportToExcel fileName='尾矿库日常观测记录表' tableId={'tableId' + this.props.data.id} /></td>
                        {
                            this.state.isCheck ? <td><Button type={'primary'} icon={'check'} onClick={() => this.sureCheck()} >确认</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.TAILING_NAME}</Descriptions.Item>
                            <Descriptions.Item label="日期">{data.DATE?.split(' ')[0]}</Descriptions.Item>
                        </Descriptions>
                    </div> : null
                }
                {
                    (data?.Nav_ListDetail && data?.Nav_ListDetail.length > 0) ?
                        <table className={styles.PrintFormLight}>
                            <tbody>
                                <tr>
                                    <td colSpan={7} className={styles.showPrintHead2}>尾矿库日常观测记录明细</td>
                                </tr>
                                <tr className={styles.PrintFormLightTh}>
                                    <td>观测时间（时）</td>
                                    <td>干滩长度/m</td>
                                    <td>库水位/m</td>
                                    <td>降雨量/mm</td>
                                    <td>滩顶高程/m</td>
                                    <td>安全超高/m</td>
                                    <td>观测人</td>
                                </tr>
                                {data?.Nav_ListDetail?.map((item, i) => {
                                    return <tr>
                                        <td>{item.DATE?.split(' ')[1]} </td>
                                        <td>{item.LONG} </td>
                                        <td>{item.WATERLINE} </td>
                                        <td>{item.DREEP} </td>
                                        <td>{item.HIGHTEST} </td>
                                        <td>{item.HIGHT_MORE} </td>
                                        <td>{item.Nav_ListUser?.map((itemtype, j) => { return (j > 0 ? '，' : '') + itemtype.Nav_User.NAME })}</td>
                                    </tr>
                                })}


                                <tr >
                                    <td>签名</td>
                                    <td colSpan={6}>{
                                        showUsersSign(this.state.users, config.picServerHost)
                                    }</td>
                                </tr>

                            </tbody>
                        </table>
                        : null
                }
            </div>
        </div>
    }
}
export default connect(({ login }) => ({ login }))(TL037ShowPrint) 
