<template>
    <transition-group class="upload-file-list flex1 ov-hd" :class="{ white }" name="el-fade-in-linear" tag="div">
        <el-upload v-if="fileList.length < limit || loading" :multiple="multiple" :action="uploadFileUrl" :before-upload="handleBeforeUpload" :file-list="fileList" :limit="limit" :on-error="handleUploadError" :on-exceed="handleExceed" :on-success="handleUploadSuccess" :show-file-list="false" :headers="headers" :accept="fileType.map(item => '.'+ item).toString()" class="upload-file-uploader" ref="fileUploadRef" :on-progress="handleOnProgress">
            <!-- 上传按钮 -->
            <!-- <div class="btn-file f-14 c-666">上传</div> -->
            <el-button class="h-plain mb10" type="primary" plain>
                <el-icon><Upload /></el-icon>
                {{ btnText }}
            </el-button>
        </el-upload>
        <el-row :gutter="60">
            <el-col :span="span" v-for="(file, index) in fileList" :key="index">
                <div class="upload-list-img d-flex a-c" :class="{ mt10: index }">
                    <template v-if="file.url">
                        <a v-if="['png', 'jpg', 'jpeg', 'bmp'].includes(fileExt(file.name))" class="flex1 right-wrap ov-hd" @click="show2 = true; lookIndex = index">
                            <el-tooltip class="box-item" effect="dark" :content="file.name" placement="top">
                                <div class="item-text sv-1 flex1">{{ file.name }}</div>
                            </el-tooltip>
                        </a>
                        <a v-else class="flex1 right-wrap ov-hd" :href="`${file.url}`" :underline="false" target="_blank">
                            <el-tooltip class="box-item" effect="dark" :content="file.name" placement="top">
                                <div class="item-text sv-1 flex1">{{ file.name || '-' }}</div>
                            </el-tooltip>
                        </a>
                    </template>
                    <div v-else class="flex1 right-wrap ov-hd p-rtv">
                        <el-tooltip class="box-item" effect="dark" :content="file.name" placement="top">
                            <div class="item-text sv-1">{{ file.name || '-' }}</div>
                        </el-tooltip>
                        <div class="progress" :style="{ width: file.percentage + '%' }"></div>
                    </div>
                    <div class="delete-item d-flex a-c j-c" @click="handleDelete(index)">
                        <el-icon color="#FF3A3A" :size="20"><Close /></el-icon>
                    </div>
                </div>
            </el-col>
            <el-col v-if="isShowTip" :span="span">
                <div class="c-999 f-12" style="line-height: 1.6;padding-top: 10px;">
                    {{ tipText }}
                </div>
            </el-col>
        </el-row>
    </transition-group>
    <ImageViewer v-model:show="show2" :imgs="fileList.map((item: any) => item.url)" :index="lookIndex"></ImageViewer>
</template>

<script setup lang="ts">
import { listByIds, delOss } from '@/api/system/oss';
import { propTypes } from '@/utils/propTypes';
import { globalHeaders } from '@/utils/request';
import { fileExt } from '@/utils/ruoyi';
import { ImageViewer } from '@/views/models';

const props = defineProps({
    modelValue: [String, Object, Array],
    // 数量限制
    limit: propTypes.number.def(1),
    // 大小限制(MB)
    fileSize: propTypes.number.def(5),
    // 文件类型, 例如['png', 'jpg', 'jpeg', 'bmp']
    fileType: propTypes.array.def(['doc', 'xls', 'ppt', 'txt', 'pdf', 'xlsx', 'docx', 'pptx', 'DOCX', 'DOC', 'PPTX', 'PPT']),
    // 是否显示提示
    isShowTip: propTypes.bool.def(true),
    tipText: propTypes.string.def(''),
    span: propTypes.number.def(24),
    multiple: propTypes.bool.def(true),
    white: propTypes.bool.def(false),
    // 上传数据格式
    format: propTypes.string.def('id'),
    btnText: propTypes.string.def('上传文件')
});
const loading = ref(false);
const lookIndex = ref(0);
const show2 = ref(false);
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const emit = defineEmits(['update:modelValue', 'change']);
const number = ref(0);
const uploadList = ref<any[]>([]);

const baseUrl = import.meta.env.VITE_APP_BASE_API;
const uploadFileUrl = ref(baseUrl + '/resource/oss/upload'); // 上传文件服务器地址
const headers = ref(globalHeaders());

const fileList = ref<any[]>([]);
const showTip = computed(() => props.isShowTip && (props.fileType || props.fileSize));

const fileUploadRef = ref<ElUploadInstance>();
const progressList = ref<any>([]);
const formatValue = ref(props.format);
watch(
    () => props.modelValue,
    async (val: any) => {
        if (val) {
            let temp = 1;
            // 首先将值转为数组
            let list = [];
            if (props.format === 'array' || Array.isArray(val)) {
                formatValue.value = 'object';
                list = val;
            } else if (props.format === 'object' || typeof val === 'object') {
                formatValue.value = 'object';
                list = [
                    {
                        name: val.fileName,
                        url: val.url,
                        fileSize: val.fileSize,
                        fileType: val.fileType,
                        ossId: val.ossId
                    }
                ];
            } else {
                // 是否http开头
                formatValue.value = 'id';
                const res = await listByIds(val as string);
                list = res.data.map((oss) => {
                    const data = { name: oss.originalName, url: oss.url, ossId: oss.ossId };
                    return data;
                });
            }
            // 然后将数组转为对象数组
            fileList.value = list.map((item) => {
                item = { name: item.fileName ? item.fileName : item.name, url: item.url, fileSize: item.fileSize, fileType: item.fileType };
                item.uid = item.uid || new Date().getTime() + temp++;
                return item;
            });
        } else {
            fileList.value = [];
            return [];
        }
    },
    { deep: true, immediate: true }
);

// 上传前校检格式和大小
const handleBeforeUpload = (file: any) => {
    // 校检文件类型
    if (props.fileType.length) {
        loading.value = true;
        const fileName = file.name.split('.');
        const fileExt = fileName[fileName.length - 1];
        const isTypeOk = props.fileType.indexOf(fileExt) >= 0;
        if (!isTypeOk) {
            proxy?.$modal.msgError(`文件格式不正确, 请上传${props.fileType.join('/')}格式文件!`);
            loading.value = false;
            return false;
        }
    }
    // 校检文件大小
    if (props.fileSize) {
        const isLt = file.size / 1024 / 1024 < props.fileSize;
        if (!isLt) {
            loading.value = false;
            proxy?.$modal.msgError(`上传文件大小不能超过 ${props.fileSize} MB!`);
            return false;
        }
    }
    number.value++;
    return true;
};

// 文件个数超出
const handleExceed = () => {
    proxy?.$modal.msgError(`上传文件数量不能超过 ${props.limit} 个!`);
};

// 上传失败
const handleUploadError = () => {
    loading.value = false;
    proxy?.$modal.msgError('上传文件失败');
};

// 上传成功回调
const handleUploadSuccess = (res: any, file: UploadFile) => {
    loading.value = false;
    if (res.code === 200) {
        uploadList.value.push({
            name: res.data.fileName,
            url: res.data.url,
            ossId: res.data.ossId,
            fileSize: file?.raw.size,
            fileType: file?.raw.type
        });
        uploadedSuccessfully();
    } else {
        number.value--;
        proxy?.$modal.msgError(res.msg);
        fileUploadRef.value?.handleRemove(file);
        uploadedSuccessfully();
    }
};

// 上传结束处理
const uploadedSuccessfully = () => {
    if (number.value > 0 && uploadList.value.length === number.value) {
        fileList.value = fileList.value.filter((f) => f.url !== undefined).concat(uploadList.value);
        uploadList.value = [];
        number.value = 0;
        const valuef = fileList.value.map(({ name, url, fileType, fileSize, ossId }) => ({ fileName: name, url, fileSize, fileType, ossId }));
        if (formatValue.value === 'id') {
            emit('change', listToString(valuef));
            emit('update:modelValue', listToString(valuef));
        } else if (formatValue.value === 'object') {
            emit('change', valuef[0]);
            emit('update:modelValue', valuef[0]);
        } else {
            emit('change', valuef);
            emit('update:modelValue', valuef);
        }
    }
};
const handleOnProgress = (event: any, file: any, list: any) => {
    const progress = Math.round((event.loaded / event.total) * 100);
    fileList.value = [...list];
};
// 删除文件
const handleDelete = (index: number) => {
    fileUploadRef.value?.abort(fileList.value[index]);
    fileList.value.splice(index, 1);
    const valuef = fileList.value.map(({ name, url, fileType, fileSize }) => ({ fileName: name, url, fileSize, fileType }));
    if (formatValue.value === 'id') {
        emit('change', listToString(valuef));
        emit('update:modelValue', listToString(valuef));
    } else if (formatValue.value === 'object') {
        emit('change', null);
        emit('update:modelValue', null);
    } else {
        emit('change', valuef);
        emit('update:modelValue', valuef);
    }
    loading.value = false;
};
// 获取文件名称
const getFileName = (name: string) => {
    // 如果是url那么取最后的名字 如果不是直接返回
    if (name.lastIndexOf('/') > -1) {
        return name.slice(name.lastIndexOf('/') + 1);
    } else {
        return name;
    }
};

// 对象转成指定字符串分隔
const listToString = (list: any[], separator?: string) => {
    let strs = '';
    separator = separator || ',';
    list.forEach((item) => {
        if (item.ossId) {
            strs += item.ossId + separator;
        }
    });
    return strs != '' ? strs.substring(0, strs.length - 1) : '';
};
</script>

<style scoped lang="scss">
.btn-file {
    width: 90px;
    height: 40px;
    line-height: 40px;
    border-radius: 8px;
    color: #666;
    text-align: center;
    background-color: #f7f7f7;
}
.upload-list-img {
    background-color: #f7f7f7;
    border-radius: 8px;

    &.mt10 {
        margin-top: 10px;
    }
}
.right-item {
    padding: 10px 16px;
    cursor: pointer;
    &:hover {
        color: var(--el-color-primary);
    }
}
.delete-item {
    width: 40px;
    cursor: pointer;
}
.upload-file-list.white {
    .btn-file {
        background-color: #fff;
    }
    .upload-list-img {
        background-color: #fff;
    }
}
.right-wrap {
    padding: 6px 10px;
    box-sizing: border-box;
}
.progress {
    position: absolute;
    left: 0;
    bottom: 0;
    height: 1px;
    background-color: var(--el-color-primary);
}
</style>
