<template>
  <el-form :model="attachment" inline style="margin-top: 15px">
    <el-form-item label="标签" prop="tag">
      <el-select v-model="attachment.tag" placeholder="请选择标签" clearable filterable style="width: 300px">
        <el-option
            v-for="item in tagsOption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
        />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button @click="handleSearch" color="#DED0B2">搜索</el-button>
      <el-button v-if="uploadState" color="#DED0B2" @click="handleUpload">上传附件</el-button>
    </el-form-item>
  </el-form>
  <el-card style="width: 100%">
    <fvTable style="width: 100%;max-height: 250px" v-if="showTable" :tableConfig="tableConfig"
             :data="otherFileList" :isSettingCol="false" :pagination="false">
      <template #empty>
        <el-empty :image-size="90" description="暂无数据" style="padding: 0"/>
      </template>
    </fvTable>
  </el-card>
</template>

<script setup lang="jsx">
import {downloadFile} from "@/api/project-demand";
import {searchFileList} from "@/api/project-manage/attachment.js";
import {getTags} from "@/api/project-manage";
import {ElNotification} from "element-plus";

const route = useRoute()
const router = useRouter()
const uploadState = ref(true)
const attachment = reactive({
  tag: ''
})
const tagsOption = ref([])
const tableConfig = reactive({
  columns: [
    {
      prop: 'index',
      type: 'index',
      label: '序号',
      align: 'center',
      width: '80',
    },
    {
      prop: 'originalFileName',
      label: '文件名',
      align: 'center',
    },
    {
      prop: 'tag',
      label: '标签',
      align: 'center'
    },
    {
      prop: 'size',
      label: '文件大小',
      align: 'center',
      currentRender: ({row, index}) => (parseInt(row.size / 1024) + 'KB')
    },
    {
      prop: 'oper',
      label: '操作',
      align: 'center',
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        return (
            <div>
              <el-button type="primary" link onClick={() => handleDownload(row)}>下载</el-button>
            </div>
        )
      }
    }
  ]
})
const showTable = ref(true)
const otherFileList = ref([])
const getTagsOption = () => {
  if (!route.query.id) return
  getTags(route.query.id).then(res => {
    if (res.code === 1000) {
      tagsOption.value = res.data
    }else{
      ElNotification({
        title: '提示',
        message: res.msg,
        type: 'error'
      })
    }
  })
}
const handleSearch = () => {
  let params = {
    targetId: route.query.id,
    targetState: "40"
  }
  if (attachment.tag) {
    tagsOption.value.forEach(item => {
      if (item.value === attachment.tag) {
        attachment.tag = item.label
      }
    })
    params.tag = attachment.tag
  }
  searchFileList(params).then(res => {
    showTable.value = false
    if (res.code === 1000) {
      otherFileList.value = res.data.fileList
      uploadState.value = res.data.upload
      nextTick(() => {
        showTable.value = true
      })
    }else{
      ElNotification({
        title: '提示',
        message: res.msg,
        type: 'error'
      })
    }
  })
}
const handleUpload = () => {
  router.push({
    name: 'Implementation/upload',
    query: {
      id: route.query.id
    }
  })
}
const handleDownload = (row) => {
  downloadFile(row.fileId).then(res => {
    const blob = new Blob([res])
    let a = document.createElement('a')
    a.href = URL.createObjectURL(blob)
    a.download = row.originalFileName
    a.click()
  })
}

handleSearch()
getTagsOption()
</script>

<style scoped>

</style>
