<template>
    <ele-modal
        :width="460"
        title="库位导入"
        :body-style="{ paddingTop: '8px' }"
        v-model="visible"
    >
        <div class="flex mb-2">
            <div class="text-sm">请先下载导入模板:</div>
            <el-button
                @click="
                    downloadOssLink(
                        'https://shuhi.oss-cn-qingdao.aliyuncs.com/default/godown_import_template.xlsx',
                        '库位导入模板'
                    )
                "
                link
                type="primary"
                >下载库位导入模板</el-button
            >
        </div>

        <div class="flex flex-col mb-2">
            <div class="text-sm text-yellow-500">库位导入规则：</div>
            <div class="text-sm">1.文件格式必须与模板完全一致！</div>
            <div class="text-sm"
                >2.同一库位编码的多次导入，会覆盖之前的信息。</div
            >
        </div>
        <div v-loading="loading" class="user-import-upload">
            <el-upload
                v-model:file-list="fileList"
                ref="uploadRef"
                action=""
                accept=".xls,.xlsx"
                :before-upload="doUpload"
                :auto-upload="false"
                :limit="1"
                :on-exceed="handleExceed"
            >
                <el-button type="primary" class="mr-2">选择文件</el-button>
                <ele-text type="placeholder">只能上传 xls、xlsx 文件</ele-text>
            </el-upload>
        </div>

        <template #footer>
            <el-button @click="visible = false">关闭</el-button>
            <el-button type="primary" @click="handleSumbit" :loading="loading"
                >导入</el-button
            >
        </template>
    </ele-modal>
</template>

<script setup>
    import { ref, h } from 'vue';
    import { genFileId } from 'element-plus';
    import { ElMessageBox } from 'element-plus/es';
    import { EleMessage } from 'ele-admin-plus/es';
    import { CloudUploadOutlined } from '@/components/icons';
    import { downloadOssLink } from '@/utils/common';
    import request from '@/utils/request';

    const emit = defineEmits(['done']);

    /** 弹窗是否打开 */
    const visible = defineModel({ type: Boolean });

    /** 导入请求状态 */
    const loading = ref(false);

    const uploadRef = ref(null);
    const fileList = ref([]);

    function handleExceed(files) {
        uploadRef.value?.clearFiles();
        const file = files[0];
        file.uid = genFileId();
        uploadRef.value?.handleStart(file);
    }

    //导入图书
    async function importLocation(file) {
        const formData = new FormData();
        formData.append('file', file);

        const res = await request.post(
            '/baseinfo/godownposition/import',
            formData
        );
        if (res.data.code === 200) {
            return res.data.msg;
        }
        return Promise.reject(new Error(res.data.msg));
    }

    /** 提交 */
    const handleSumbit = () => {
        loading.value = true;
        let file = fileList.value[0];
        importLocation(file.raw)
            .then((msg) => {
                loading.value = false;
                ElMessage.success(msg);
                visible.value = false;
                emit('done');
            })
            .catch((e) => {
                loading.value = false;
            });
    };

    /** 上传 */
    const doUpload = (file) => {
        if (
            ![
                'application/vnd.ms-excel',
                'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            ].includes(file.type)
        ) {
            EleMessage.error('只能选择 excel 文件');
            return false;
        }
        if (file.size / 1024 / 1024 > 10) {
            EleMessage.error('大小不能超过 10MB');
            return false;
        }
        return false;
    };
</script>
