import { ElMessage, ElMessageBox, ElInput, ElSegmented } from 'element-plus'
export const downLoadFile = (name: string, key: string, res: any) => {
    let blob = new Blob([res], {
        type: getFileType(key)
    })
    const url = window.URL.createObjectURL(blob)
    const link = document.createElement('a') // 创建a标签
    link.href = url
    link.download = name // 设置下载的文件名
    document.body.appendChild(link)
    link.click() //执行下载
    document.body.removeChild(link)
    setTimeout(() => {
        ElMessage.success('下载成功')
    }, 2000)
}

const getFileType = (url: string) => {
    const ext = url.split('.').pop()?.toLowerCase() || ''
    const mimeMap: Record<string, string> = {
        // Excel
        xls: 'application/vnd.ms-excel',
        xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        // CSV
        csv: 'text/csv',
        // Word
        doc: 'application/msword',
        docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        // PDF
        pdf: 'application/pdf',
        // PowerPoint
        ppt: 'application/vnd.ms-powerpoint',
        pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
        // 图片
        png: 'image/png',
        gif: 'image/gif',
        jpeg: 'image/jpeg',
        jpg: 'image/jpeg',
        bmp: 'image/bmp',
        ico: 'image/vnd.microsoft.icon',
        tif: 'image/tiff',
        tiff: 'image/tiff',
        svg: 'image/svg+xml',
        webp: 'image/webp',
        // 音频
        mp3: 'audio/mpeg',
        aac: 'audio/aac',
        mid: 'audio/midi',
        midi: 'audio/midi',
        oga: 'audio/ogg',
        wav: 'audio/wav',
        weba: 'audio/webm',
        // 视频
        avi: 'video/x-msvideo',
        mpeg: 'video/mpeg',
        ogv: 'video/ogg',
        webm: 'video/webm',
        '3gp': 'video/3gpp',
        '3g2': 'video/3gpp2',
        // 网页/代码
        html: 'text/html',
        css: 'text/css',
        js: 'text/javascript',
        mjs: 'text/javascript',
        json: 'application/json',
        jsonld: 'application/ld+json',
        xhtml: 'application/xhtml+xml',
        xml: 'application/xml',
        xul: 'application/vnd.mozilla.xul+xml',
        // 文档
        abw: 'application/x-abiword',
        odp: 'application/vnd.oasis.opendocument.presentation',
        ods: 'application/vnd.oasis.opendocument.spreadsheet',
        odt: 'application/vnd.oasis.opendocument.text',
        rtf: 'application/rtf',
        txt: 'text/plain',
        vsd: 'application/vnd.visio',
        // 字体
        otf: 'font/otf',
        ttf: 'font/ttf',
        woff: 'font/woff',
        woff2: 'font/woff2',
        eot: 'application/vnd.ms-fontobject',
        // 压缩/归档
        arc: 'application/x-freearc',
        bz: 'application/x-bzip',
        bz2: 'application/x-bzip2',
        rar: 'application/x-rar-compressed',
        tar: 'application/x-tar',
        zip: 'application/zip',
        '7z': 'application/x-7z-compressed',
        // 其他
        bin: 'application/octet-stream',
        csh: 'application/x-csh',
        epub: 'application/epub+zip',
        azw: 'application/vnd.amazon.ebook',
        ics: 'text/calendar',
        jar: 'application/java-archive',
        mpkg: 'application/vnd.apple.installer+xml',
        ogx: 'application/ogg',
        sh: 'application/x-sh',
        swf: 'application/x-shockwave-flash'
    }
    return mimeMap[ext] || ''
}
