<template>
  <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" v-model="upload.open" width="400px" append-to-body>
        <el-upload
            ref="upload"
            :limit="1"
            accept=".xlsx, .xls"
            :headers="upload.headers"
            :action="upload.processurl + upload.url"
            :disabled="upload.isUploading"
            :on-progress="handleFileUploadProgress"
            :on-success="handleFileSuccess"
            :on-error="handleFileError"
            :auto-upload="false"
            drag
        >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <template #tip>
                <div class="el-upload__tip text-center">
                    <span style="display:inline-block;margin-top:10px">仅允许导入xls、xlsx格式文件。</span>
                    <el-link type="primary" v-if="upload.showModel" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
                </div>
            </template>
        </el-upload>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="submitFileForm">确 定</el-button>
                <el-button @click="upload.open = false">取 消</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script>
import { getToken } from '@/api/auth'
export default {
    data() {
        return {
            // 用户导入参数
            upload: {
                // 展示下载模板
                showModel: true,
                // 是否显示弹出层（用户导入）
                open: false,
                // 弹出层标题（用户导入）
                title: "",
                // 是否禁用上传
                isUploading: false,
                // 是否更新已经存在的用户数据
                updateSupport: 0,
                // 设置上传的请求头部
                headers: { 
                    // Authorization: "Bearer ",
                    token: getToken() 
                },
                // 上传的地址
                processurl: (process.env.NODE_ENV === 'production') ? process.env.PRO_BASE_API : process.env.BASE_API,
                url: '',
                // 上传模板名称
                urlName: '',
                // 上传文件名称
                proName: ''
            },
        }
    },
    methods:{
        /** 下载模板操作 */
        importTemplate() {
            // this.download('system/user/importTemplate', {
            //     ...this.queryParams
            // }, `user_template_${new Date().getTime()}.xlsx`)
            let url = '/predict/api/configureShare/dexcel'
            let parasm = {
                templateName: this.upload.urlName
            }
            this.$utils.downloadTemplate(url, parasm, this.upload.proName)
        },
        // 文件上传中处理
        handleFileUploadProgress(event, file, fileList) {
            this.upload.isUploading = true;
            this.$emit('importLoading', true)
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
            this.upload.open = false;
            this.upload.isUploading = false;
            this.$refs.upload.clearFiles();
            // this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
            if (response.code === 500) {
                if (response.msg.indexOf('运行时异常') !== -1) {
                    this.$message.error('模板数据不匹配,请使用模板导入');
                    this.importTemplate()
                } else {
                    this.$message.error(response.msg);
                }
            } else {
                this.$message({
                    message: response.msg,
                    type: 'success'
                });
            }
            this.$emit('successImport', response)
            // this.getList();
        },
        handleFileError(response, file, fileList) {
            this.$message.error(response.msg);
        },
        // 提交上传文件
        submitFileForm() {
            this.$refs.upload.submit();
        }
    }
}
</script>

<style lang="less">
.el-overlay{
    .el-dialog{
        .el-dialog__body{
            padding: 20px;
        }
    }
}
</style>