import { Button, Descriptions,Popconfirm, Row, Col, Form,Modal, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude,extendIgnoreDataRule, setDataFieldValue, guid, initQueryFilter } from "../../../utils/common";
import ReactToPrint from "react-to-print";
import stylesStep from '../HI/StepForm.css';
import config from "../../../config";
import XLSX from 'xlsx';
import { connect } from 'dva';
import moment from 'moment';
import { message } from "antd/lib/index";
import FormPage from '../../../components/FormPage'
class PR060ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            BtnAgreeDisplay: 'none',
            tmpData: {},
        };
    };

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

    onTableBtnAgree() {
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'PE/PECorrectivePreventive/IdentityUpdate',
            payload: {
                ID: this.props.data.id,
                TaskID: this.props.data.TaskID,
            },
            onComplete: (ret) => {
                if (ret) {
                    message.success('确认成功！');
                    this.setState({ BtnAgreeDisplay: 'none' })
                    this.BtnClose();
                }
            }
        })
    }

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

    loadData = (dataId) => {
        let that=this;
        let json = initFilter(that.props.login.OrgId);
        extendRule(json, 'ID', 1, dataId);
        extendInclude(json, 'Nav_Deals');
        extendInclude(json, 'Nav_Drugs'); 
        extendInclude(json, 'Nav_Machs');
        extendInclude(json, 'Nav_Inves');
        extendInclude(json, 'Nav_Tungs');
        extendIgnoreDataRule(json)
       that.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'PR/DutyOperationRecord/Get',
            onComplete: (ret) => {
                if (ret) {
                    if(that.props.data && that.props.data.loadDataDeal)
                    {
                        that.props.data.loadDataDeal(1);
                    }
                    that.setState({ data: ret });
                    if (that.props.data.tableKey==="2" ||that.props.data.tableKey===undefined) {
                        that.setState({ BtnAgreeDisplay: 'none' })
                    }else{
                        that.setState({ BtnAgreeDisplay: 'inline' })
                    }
                    this.onClickApprove(false);
                    //归档才显示打印和导出按钮
                    // if(ret.PRE_MEETING_STATUS==5){
                    //     this.setState({ isFinished: 'inline' })
                    // }
                }
                else
                { message.error('请先尝试刷新，若仍然加载失败，请联系管理员排查!');}
            }
        });
    }

    onTableBtnExport() {
        const enums=this.props.data.enums ? this.props.data.enums :this.props.app.enums ;
        let TableWrap = document.getElementById('tableId' + this.props.data.id);
        let Table = TableWrap.getElementsByTagName('table')[0];
        const wb = XLSX.utils.table_to_book(Table);
        XLSX.writeFile(wb, "选矿一部值班运行记录.xlsx")
    }

    onClickApprove=(appVisible)=>{
        const newtmpData = {
            data: { id: this.props.data.id, isShow: true,key:guid(),param:this.props.data.param,appVisible:appVisible,BtnAgreeDisplay:'inline'},
            formCode: "PF123",
        }
        this.setState({
            tmpData: newtmpData,
        });
    }
    render() {
        const { data } = this.state;
        const enums=this.props.data.enums ? this.props.data.enums :this.props.app.enums ;
        // let aaa = Math.random();
        return <div>
            <div style={{ padding: '10px' }}>
            <Button onClick={() => this.onClickApprove(true)} style={{ marginRight: '8px'}} icon="export">审批详情</Button>
                <ReactToPrint
                    trigger={() => <Button style={{ marginLeft: '8px', display: data && data.IS_PUBLISH === 5?"inline":"none" }} type={'default'} icon={'printer'} >打印</Button>}
                    content={() => this.componentRef}
                />
                <Button onClick={() => this.onTableBtnExport()} icon="export" style={{ marginLeft: '8px', display: data && data.IS_PUBLISH === 5?"inline":"none" }}>导出</Button>
                {/* <Button type="primary" style={{marginLeft: '8px', display: this.state.BtnAgreeDisplay }} onClick={() => this.onTableBtnAgree()} icon="check" >已阅</Button> */}
            </div>
            <div ref={el => (this.componentRef = el)} style={{ padding: '20px' }} id={'tableId' + this.props.data.id}>
                <h1 style={{ textAlign: 'center', margin: '15px' }}>选矿一部值班运行记录</h1>
                 {
                    data ?  <table style={{ width: '100%', textAlign: 'left'}} className={stylesStep.PrintForm1}>
                    <tr>
                        <td  width='150px' textAlign='center' className={stylesStep.fontBold}>序号</td>
                        <td  width='150px' className={stylesStep.fontBold}>系列</td>
                        <td  width='150px' className={stylesStep.fontBold}>运行时间</td>
                        <td  width='150px' className={stylesStep.fontBold}>矿量</td>
                    </tr>
                    {
                                    data.Nav_Deals && data.Nav_Deals.map((item,i)=>{
                                        return <tr>
                                            <td>
                                                {i+1}
                                            </td>
                                            <td>
                                                {item.NO}
                                            </td>
                                            <td>
                                                {item.HOUR}
                                            </td>
                                            <td>
                                                {item.COUNT}
                                            </td>
                                        </tr>
                                    })
                                }
                    </table>:null 
                }
                {
                    data ?  <table style={{ width: '100%', textAlign: 'left'}} className={stylesStep.PrintForm1}>
                    <tr>
                        <td  width='150px' textAlign='center' className={stylesStep.fontBold}>序号</td>
                        <td  width='150px' className={stylesStep.fontBold}>药剂名称</td>
                        <td  width='150px' className={stylesStep.fontBold}>用量</td>
                        <td  width='150px' className={stylesStep.fontBold}>单耗</td>
                    </tr>
                    {
                                    data.Nav_Drugs && data.Nav_Drugs.map((item,i)=>{
                                        return <tr>
                                            <td>
                                                {i+1}
                                            </td>
                                            <td>
                                                {item.NO}
                                            </td>
                                            <td>
                                                {item.QTY}
                                            </td>
                                            <td>
                                                {item.ACERAGE}
                                            </td>
                                        </tr>
                                    })
                                }
                    </table>:null 
                }
                {
                    data ?  <table style={{ width: '100%', textAlign: 'left'}} className={stylesStep.PrintForm1}>
                    <tr>
                        <td  width='150px' textAlign='center' className={stylesStep.fontBold}>序号</td>
                        <td   className={stylesStep.fontBold}>本班压力</td>
                        <td   className={stylesStep.fontBold}>本班放浆桶数</td>
                        <td   className={stylesStep.fontBold}>本班加温数</td>
                    </tr>
                    {
                                    data.Nav_Machs && data.Nav_Machs.map((item,i)=>{
                                        return <tr>
                                            <td>
                                                {i+1}
                                            </td>
                                            <td>
                                                {item.PRESS}
                                            </td>
                                            <td>
                                                {item.QTY}
                                            </td>
                                            <td>
                                                {item.HEAT_QTY}
                                            </td>
                                        </tr>
                                    })
                                }
                    </table>:null 
                }
                {
                    data ?  <table style={{ width: '100%', textAlign: 'left'}} className={stylesStep.PrintForm1}>
                    <tr>
                        <td  width='150px' textAlign='center' className={stylesStep.fontBold}>序号</td>
                        <td  className={stylesStep.fontBold}>接班库存量</td>
                        <td  className={stylesStep.fontBold}>本班入库量</td>
                        <td  className={stylesStep.fontBold}>本班使用量</td>
                        <td  className={stylesStep.fontBold}>交班库存量</td>
                        <td  className={stylesStep.fontBold}>交班时高水池水位</td>
                    </tr>
                    {
                                    data.Nav_Inves && data.Nav_Inves.map((item,i)=>{
                                        return <tr>
                                            <td>
                                                {i+1}
                                            </td>
                                            <td>
                                                {item.ACCEPT_QTY}
                                            </td>
                                            <td>
                                                {item.INPUT_QTY}
                                            </td>
                                            <td>
                                                {item.USE_QTY}
                                            </td>
                                            <td>
                                                {item.SEND_QTY}
                                            </td>
                                            <td>
                                                {item.HEIGHT}
                                            </td>
                                        </tr>
                                    })
                                }
                    </table>:null 
                }
                 {
                    data ?  <table style={{ width: '100%', textAlign: 'left'}} className={stylesStep.PrintForm1}>
                    <tr>
                        <td  width='150px' textAlign='center' className={stylesStep.fontBold}>序号</td>
                        <td  width='150px' className={stylesStep.fontBold}>系列</td>
                        <td  width='150px' className={stylesStep.fontBold}>原矿品位%</td>
                        <td  width='150px' className={stylesStep.fontBold}>精矿品位%</td>
                        <td  width='150px' className={stylesStep.fontBold}>尾矿品位%</td>
                        <td  width='150px' className={stylesStep.fontBold}>回收率%</td>
                        <td  width='150px' className={stylesStep.fontBold}>富集比</td>
                        <td  className={stylesStep.fontBold}>备注</td>
                    </tr>
                    {
                                    data.Nav_Tungs && data.Nav_Tungs.map((item,i)=>{
                                        return <tr>
                                            <td>
                                                {i+1}
                                            </td>
                                            <td>
                                                {item.NO}
                                            </td>
                                            <td>
                                                {item.UNDRESS_ORE}
                                            </td>
                                            <td>
                                                {item.COARSE_FINE}
                                            </td>
                                            <td>
                                                {item.TAILING}
                                            </td>
                                            <td>
                                                {item.RECOVERY_RATE}
                                            </td>
                                            <td>
                                                {item.ENRICHMENT}
                                            </td>
                                            <td>
                                                {item.REMARK}
                                            </td>
                                        </tr>
                                    })
                                }
                    </table>:null 
                }
                <FormPage {...this.state.tmpData} />
            </div>
        </div>
    }

}

export default connect(({ login,app }) => ({ login,app }))(PR060ShowPrint) 