import { Button, Descriptions, Badge, Popconfirm, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, extendIgnoreDataRule, setDataFieldValue, showApprove, guid, initQueryFilter, 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 CM030ShowPrint 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 orgId = this.props.login ? this.props.login.OrgId : '';
        let json = initFilter(orgId);
        var isCheck = (this.props.data.tableKey != undefined && this.props.data.tableKey == "1") ? true : false
        extendRule(json, 'ID', 1, id);
        extendInclude(json, 'Nav_Department')
        extendInclude(json, 'Nav_User')
        extendInclude(json, 'Nav_ListDetail.Nav_MaterialType')
        extendInclude(json, 'Nav_ListDetail.Nav_MaterialLibrary')
        extendIgnoreDataRule(json)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'CM/CMEquipmentDemandSurvey/GetOrder',
            onComplete: (ret) => {
                if (ret) {
                    this.setState({
                        data: ret,
                        isCheck: isCheck
                    })
                }
            }
        });
    }

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

    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)
                // this.BtnClose()
            }
        });
    }

    render() {
        const { data, isCheck } = this.state;
        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.START_TIME}</Descriptions.Item>
                            <Descriptions.Item label="截止时间">{data.END_TIME}</Descriptions.Item>
                            <Descriptions.Item label="发起部门">{data.Nav_Department.NAME}</Descriptions.Item>
                            <Descriptions.Item label="发起人">{data.Nav_User.NAME}</Descriptions.Item>
                        </Descriptions>
                    </div> : null
                }
                {
                    (data?.Nav_ListDetail && data?.Nav_ListDetail.length > 0) ?
                        <table className={styles.PrintFormLight}>
                            <tbody>
                                <tr className={styles.PrintFormLightTh}>
                                    <td>类型</td>
                                    <td>装备名称</td>
                                    <td>规格/型号</td>
                                    <td>应急物资库名称</td>
                                    <td>库内现有数量</td>
                                    <td>需求数量</td>
                                    <td>单位</td>
                                    <td>用途</td>
                                </tr>
                                {data?.Nav_ListDetail?.map((item, i) => {
                                    return <tr>
                                        <td>{item.Nav_MaterialType.NAME} </td>
                                        <td>{item.Nav_MaterialLibrary.NAME} </td>
                                        <td>{item.SPECIFICATION} </td>
                                        <td>{item.MATERIAL_LOCATION} </td>
                                        <td>{item.AVAILABLE_COUNT} </td>
                                        <td>{item.NEED_COUNT} </td>
                                        <td>{item.UNIT} </td>
                                        <td>{item.PURPOSE} </td>
                                    </tr>
                                })}
                            </tbody>
                        </table>
                        : null
                }
            </div>
        </div>
    }
}
export default connect(({ login }) => ({ login }))(CM030ShowPrint) 
