<template>
    <!-- 富文本编辑器----上传功能 -->
    <div class="editor-upload-wrap">
        <el-upload
            class="upload-demo"
            ref="upload"
            action=""
            :accept="accept"
            :show-file-list="false"
            :http-request="httpRequest"
        >
            <i class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
    </div>
</template>

<script>
import file from '@/utils/file'
import commonApi from '@/services/fetchApi'
export default {
    props: {
        title: {
            type: String,
            default: '',
        },
        accept: {
            type: String,
            default: '*',
        },
        otherId: {
            // 外接id[比如planId，工单id，资产id等等]
            type: [String, Number],
            default: ''
        }
    },
    data() {
        return {
            file: file,
            fileList: [],
            activeIndex: -1,
            fileIds: [], // 文件上传成功后返回的文件id集合
        }
    },
    created() {

    },
    methods: {
        httpRequest(param) {
            let name = file.getFileExtension(param.file.name)
            // 验证文件格式
            if (this.accept != '*') {
                if (this.accept.indexOf(name) === '-1') {
                    this.$message.error(`请上传格式为${this.accept}的文件`)
                    return
                }
            }
            // 验证文件大小
            let fileSize = param.file.size / 1024 / 1024 //单位为MB
            if (fileSize > 10) {
                this.$message.error('允许上传的文件最大尺寸为10MB')
                return
            }
            let formdata = new FormData()
            formdata.append('file', param.file)
            commonApi.uploadFile(formdata).then((res) => {
                // if (!res.cede) {
                    // this.fileIds.push(res.id)
                    this.fileList.push(res.url)
                    this.handleUpalodSuccess(res.url)
                // }
            })
            // this.$emit('handleImport', param.file)
        },
        handleUpalodSuccess(data) {
            this.$emit('handleUploadSuccess', data)
        },
        handleDelete(item, i) {
            console.log(item)
            // commonApi.deleteFile({ id: item.id }).then(res => {
            //     console.log('删除',res)
            //     if(!res.code) {
            //         this.$message.success('成功')
            //         this.fileList.splice(i,1)
            //     }
            // })
        },
        handleDownload(item, i) {
            let obj = {
                bucketName: 'device-manage',
                objectName: item.objectName
            }
            // commonApi.getMinioToken(obj).then(res => {
            //     if(!res.code) {
            //         commonApi.downloadFile({
            //             ...obj,
            //             token: res
            //         }).then(res => {
            //             file.downloadFileByBlob(res, item.fileName)
            //         })
            //     }
            // })
        },
        getFileName: fileName => {
            return fileName.substring(0, fileName.lastIndexOf('.'))
        }
    },
}
</script>

<style lang="scss" scoped>
.editor-upload-wrap{
    display: none;
}
</style>