<template>
    <div :id="winId" class="go-upload-excel-win hidden">
        <div class="winBody" v-show="showExcelUpload">
            <div class="PopupTitle">{{title}}</div>
            <Row class="clearfix" style="margin-top: 3px;">
                <Upload
                        ref="upload"
                        :data="posData"
                        :multiple="false"
                        :action="uploadFileAction||uploadAction"
                        :before-upload="handleBeforeUpload"
                        :format="formateAry"
                        :on-format-error="handleFormatError"
                        :on-exceeded-size="handleMaxSize"
                        :on-success="handleSuccess"
                        :on-error="handleError"
                        type="drag"
                        :on-remove="handleRemove"
                        :headers="headers"
                        :max-size="maxSize"
                        >
                    <div style="padding: 20px 0">
                        <Icon type="ios-cloud-upload" size="52" style="color: #3399ff"></Icon>
                        <p>选择或者拖拽相应的excel文件</p>
                    </div>
                </Upload>

            </Row>
            <div class="row-div">
                <slot></slot>
            </div>
        </div>
        <div v-show="!showExcelUpload" class="back_body" style="padding-top: 10px;">
            <Row class="go-small-row">
                <go-input-list
                    v-model="this.title"
                    label="导入报告"
                    labelWidth="90"
                    width="big"/>
            </Row>
            <Row class="go-small-row">
                <go-input-list
                    v-model="this.fileName"
                    label="导入文件名"
                    labelWidth="90"
                    width="big"/>
            </Row>
            <Table class="go_md_table blue" :max-height="210" :columns="tableCols" :data="tableData"></Table>
        </div>
    </div>
</template>

<script>
    import {GoingUtils,StorageUtils,AssertUtils,QueryCommand} from 'goingutils';
    import UploadFileMixins from "../ctlMixins/UploadFileMixins";

    export default {
        name: "uploadExcelForm",
        mixins: [UploadFileMixins],
        data(){
            const token = StorageUtils.getSessionItem("sessionId");
            return {
                dialogWinId:"",
                tableData:[],
                tableCols: [
                    {
                        title: '数据',
                        width: 130,
                        key: 'name'
                    },
                    {
                        title: '行号',
                        width: 70,
                        key: 'rowNum'
                    },
                    {
                        title: '结果',
                        width: 70,
                        key: 'result'
                    },
                    {
                        title: '详情',
                        key: 'msg'
                    }
                ],
                fileName:'',
                showExcelUpload:true,
                headers:{
                    "Blade-Auth": `bearer ${token}`,
                    Authorization: `Basic c2FiZXI6c2FiZXJfc2VjcmV0`
                },
                posData:{...this.params},
                formateAry:['xls','xlsx'],
                winId:GoingUtils.getUUid(10),
            };
        },
        props:{
            //下载导出结果的url
            downResultFileAction:{type:String,default:''},
            //上传时的参数
            params:{type:Object,default:{}},
            //显示回退表格
            showBackTable:{type:Boolean,default: true},
            //上传文件的action
            uploadFileAction:{},
            //标题
            title:{type:String,default:''},
            //是否显示窗口
            value:{},
        },
        methods:{
            beforeUpload(){
                this.$refs.upload.clearFiles();
                return true;
            },
            //关闭窗口
            closeWin(){
                this.$emit("input", false);
                this.$emit('close', true);
            },
            //弹出窗口
            showFileUploadWin: function ({url, params, title = "选择导入的Excel"}) {
                let  vueObj=this;
                let dialogWinId=this.$WinUtils.showInfoOnWin({
                    confirmLabel:"导出结果",
                    cancelLabel:'关闭',
                    width: 550,
                    height: 400,
                    url: url,
                    params: params,
                    showBottomBar: true,
                    title: title,
                    confirmBtn:()=>{
                        //进行导出操作
                        if(AssertUtils.isNotEmpty(this.downResultFileAction)){
                            let queryCommand = new QueryCommand();
                            queryCommand.executeExport(
                                this.downResultFileAction,
                                "exportResult.xlsx"
                            );
                            // window.location.href=this.downResultFileAction;
                        }
                    },
                    closeWin(){
                        vueObj.tableData=[];
                        vueObj.showExcelUpload=true;
                        vueObj.closeWin();
                        vueObj.$refs.upload.clearFiles();
                    },
                    anim: null
                });
                this.dialogWinId=dialogWinId;
                //隐藏确定按钮
                $("#layui-layer"+dialogWinId).find("._confirmBtn").hide();
            },
            //弹出窗口
            showWin(){
                this.showFileUploadWin({
                    url: this.winId,
                    fileType: "*"
                });
            },
            //处理附件上传成功事件
            handleSuccess(res) {
                GoingUtils.closeLoading();
                if(res.data&&res.data.length>0){
                    res.data.forEach((item)=>{
                        this.tableData.push(item);
                    });
                    this.$emit("success", {});
                    // //附件上传后的事件
                    if(this.showBackTable){
                        //显示导出结果按钮
                        $("#layui-layer"+this.dialogWinId).find("._confirmBtn").show();
                        //修改窗口的标题
                        $("#layui-layer"+this.dialogWinId).find(".layui-layer-title").html("导入结果信息");
                        setTimeout(()=>{
                            //获取附件的文件名称
                            this.fileName=$("#layui-layer"+this.dialogWinId).find(".ivu-upload-list-file-finish").text();
                        },100)
                        this.showExcelUpload=false;

                    }else{
                        this.$WinUtils.alert("导入成功！");
                    }
                    return;
                }
                //如果存在错误信息则提示用户
                GoingUtils.showErrorMessage(res.message);
            },
            //隐藏窗口
            hideWin(){
                this.$WinUtils.closeParentCurWin();
            }
        },
        watch:{
            params(){
                //如果传入的参数发生变化
                this.posData = {
                    ...this.params,
                    bid: this.bid,
                    sessionId: StorageUtils.getSessionItem("sessionId")
                };
            },
            //如果为true  则弹出窗口 如果为false 则关闭窗口
            value(newValue){
                if(newValue){
                    this.showWin();
                }else{
                    this.hideWin();
                }
            }
        }
    };
</script>

<style scoped>

</style>
