<template>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" v-model="upload.open" width="400px" append-to-body>
        <!-- :limit="1" -->
        <el-upload ref="upload" 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" :data="upload.data" 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 baseUrl from '../../utils/baseUrl.js'
    import {
        apiGetExportMsg
    } from '../../api/api'
    import {
        saveAs
    } from 'file-saver'
    import {
        getToken
    } from '@/api/auth'
    export default {
        data() {
            return {
                // 用户导入参数
                upload: {
                    data: null,
                    // 展示下载模板
                    showModel: false,
                    // 是否显示弹出层（用户导入）
                    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,
                    processurl: process.env.NODE_ENV === "production" ? baseUrl.ROOT : baseUrl.ROOT,
                    url: '',
                    // 上传模板名称
                    urlName: '',
                    // 上传文件名称
                    proName: ''
                },
                showModelObj: {
                    url: ''
                }
            }
        },
        methods: {
            /** 下载模板操作 */
            importTemplate() {
                let url = this.showModelObj.url
                apiGetExportMsg(url).then(datas => {
                    let blob = new Blob([datas])
                    saveAs(blob, '导入模板.xlsx')
                }).catch((r) => {
                    console.error(r)
                })
            },
            // 文件上传中处理
            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.success) {
                    if (response.message.indexOf('运行时异常') !== -1) {
                        this.$message.error('模板数据不匹配,请使用模板导入');
                        this.importTemplate()
                    } else {
                        this.$message.error(response.message);
                    }
                } else {
                    this.$message({
                        message: response.message,
                        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__header {
                .el-dialog__title {
                    font-size: 14px;
                }
            }

            .el-dialog__body {
                padding: 20px;
            }
        }
    }
</style>