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, ShowPrintClose, 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 '../../../components/CustomPages/HI/StepForm.css';
import config from '../../../config.js';
import FormPage from '../../FormPage'
class TL016ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: 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');
        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_ListUser.Nav_User.Nav_UserSignFiles.Nav_ImgFile.Nav_File')
        json.IgnoreDataRule = true
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'TL/BackRecord/Get',
            onComplete: (ret) => {
                if (ret) {
                    if (isCheck) {
                        isCheck = ret.STATUS == 10 ? true : false
                    }
                    let listSign = []
                    ret.Nav_ListUser.forEach(e => {
                        if (e.ISCHECK)
                            listSign.push(e)
                    })

                    this.setState({
                        data: ret,
                        isCheck: isCheck,
                        listSign: listSign
                    })
                }
            }
        });
    }

    //确认
    sureCheck = () => {
        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: 'TL/TLBackRecord/sendCheck',
            onComplete: (ret) => {
                this.BtnClose()
            }
        });
    }

    BtnClose = () => {
        if (typeof this.props.data.onCancel != "undefined" && typeof this.props.data.onCancel == 'function')
            this.props.data.onCancel();
    }

    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} /></td>
                        <td><ExportToExcel fileName='回采记录' tableId={'tableId' + this.props.data.id} /></td>
                        {
                            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.BACKTIME}</Descriptions.Item>
                            <Descriptions.Item label="回采结束时间（时）">{data.BACKTIMEEND?.split(' ')[1]}</Descriptions.Item>
                            <Descriptions.Item label="工程名称">{data.NAME}</Descriptions.Item>
                            <Descriptions.Item label="回采方向">{data.BACK_DIREACT}</Descriptions.Item>
                            <Descriptions.Item label="回采顺序">{enums.BackOrder.enums[data.BACK_ORDER]}</Descriptions.Item>
                            <Descriptions.Item label="回采厚度（m）">{data.BACK_THICKNESS}</Descriptions.Item>
                            <Descriptions.Item label="回采量（m³）">{data.BACK_NUMBER}</Descriptions.Item>
                            <Descriptions.Item label="回采边坡坡比">{data.BACK_SCALE}</Descriptions.Item>
                            <Descriptions.Item label="安全超高（m）">{data.SAFE_HIGHT}</Descriptions.Item>
                            <Descriptions.Item label="干滩长度（m）">{data.DRIVE_LONG}</Descriptions.Item>
                            <Descriptions.Item label="值班人">{data.Nav_ListUser?.map((item, i) => {
                                return item.ISCHECK ? item.Nav_User.NAME : <a style={{ color: "red" }}>{item.Nav_User.NAME}</a>
                            })}</Descriptions.Item>

                            <Descriptions.Item label="值班人签名">{
                                showUsersSign(listSign, config.picServerHost, 200)
                            }</Descriptions.Item>
                        </Descriptions>
                    </div> : null
                }
            </div>
        </div>
    }
}
export default connect(({ login, app }) => ({ login, app }))(TL016ShowPrint) 
