<template>
  <div>
    <van-grid v-if="dataList.length > 0">
      <van-grid-item v-for="item in dataList" :key="item.fileId" @click="toShowFile(item)" :id="item.fileId">
        <i class="iconfont" :class="`icon-${item.type}`" />
        <div class="grid-item-text mt-10">{{ item.name }}</div>
      </van-grid-item>
    </van-grid>
    <van-empty v-else image-size="80" />
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import fileInfoStore from '@/stores/fileInfo'
const fileInfo = fileInfoStore()
import { workFileListByMenuId } from '@/utils/api'
let dataList = ref<{ fileId: '', type: '', name: '' }[]>([])
const init = async (menuId: any) => {
  const { data } = await workFileListByMenuId({ menuId: menuId })
  data.menuList.map((ele: any) => {
    ele.type = 'morenwenjianjia'
    ele.name = ele.menuName
  })
  const reg1 = /(\.xls|\.xlsx)$/
  const reg2 = /(\.txt)$/
  const reg3 = /(\.pdf)$/
  const reg4 = /(\.doc|\.docx)$/
  const reg5 = /(\.ppt|\.pptx)$/
  const reg6 = /(\.jpg|\.png)$/
  const reg7 = /(\.mp3|\.m4a)$/
  data.fileList.map((ele: any) => {
    ele.type = 'unknown'
    ele.name = ele.fileName
    if (reg1.test(ele.fileName)) {
      ele.type = 'excel'
      ele.name = ele.fileName.replace(reg1, '')
    }
    if (reg2.test(ele.fileName)) {
      ele.type = 'txt'
      ele.name = ele.fileName.replace(reg2, '')
    }
    if (reg3.test(ele.fileName)) {
      ele.type = 'pdf'
      ele.name = ele.fileName.replace(reg3, '')
    }
    if (reg4.test(ele.fileName)) {
      ele.type = 'word'
      ele.name = ele.fileName.replace(reg4, '')
    }
    if (reg5.test(ele.fileName)) {
      ele.type = 'ppt'
      ele.name = ele.fileName.replace(reg5, '')
    }
    if (reg6.test(ele.fileName)) {
      ele.type = 'image'
      ele.name = ele.fileName.replace(reg6, '')
    }
    if (reg7.test(ele.fileName)) {
      ele.type = 'yinpinwenjian'
      ele.name = ele.fileName.replace(reg7, '')
    }
  })
  dataList.value = [...data.menuList, ...data.fileList]
}

import { showFile } from '@/mixins/show-file'
const { loadingFile } = showFile()
const toShowFile = (item: any) => {
  if (item.type === 'morenwenjianjia') {
    showToast('暂时不能打开文件夹')
  } else if (item.type === 'unknown') {
    showToast('未知文件无法打开')
  } else {
    fileInfo.fileId = item.fileId
    loadingFile(`${import.meta.env.VITE_BASE_URL}/api/file/${item.fileId}`)
  }
}
defineExpose({ init })
</script>
<style lang='scss' scoped>
:deep(.van-grid-item__content) {
  padding: 8px 4px;
  font-size: 12px;
  font-weight: 400;
}

.grid-item-text {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  text-align: center;
  max-width: calc(25vw - 10px);

  @media screen and (min-width: 678px) {
    max-width: 140px;
  }
}

.iconfont {
  font-size: 30px !important;
}

.icon-morenwenjianjia,
.icon-folder6wenjianjia {
  color: #72cffb;
}

.icon-ppt {
  color: #e34221;
}

.icon-pdf {
  color: #8c181a;
}

.icon-excel {
  color: #45b058;
}

.icon-txt {
  color: #f9ca06;
}

.icon-word {
  color: #14a9da;
}

.icon-image {
  color: #49c9a7;
}

.icon-unknown {
  color: #7d99af;
}

.van-empty {
  padding: 10px 0
}
</style>
