<script setup lang="ts">

import {useI18n} from "vue-i18n";
import type {IFileNameList} from "@/interface/page/fileManage/IFileNameList.ts";
import {onMounted, ref} from "vue";
import MessageUtils from "@/utils/MessageUtils.ts";
import FileApi from "@/api/file.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import type {AppServeDataResponse} from "@/interface/api/AppServeDataResponse.ts";

const { t } = useI18n();
const tableData = ref<IFileNameList[]>([]);

onMounted(() => {
  query();
});

// 查询按钮
const query = () => {
  const loadingInstance = MessageUtils.loading();
  FileApi.getFileNameList().then(res => {
    const stringify = JSON.stringify(res.data);
    const response = JSON.parse(stringify) as AppServeDataResponse<string[]>;
    if (response && response.code == AppServeResponseCodeEnum.SUCCESS && response.data != null) {
      const data = response.data;
      tableData.value = data.map(item => {
        return {
          fileName: item
        }
      });
      MessageUtils.successMessage('查询成功');
      return;
    }
    MessageUtils.errMessage(response.msg);
  }).catch(() => {}).finally(() => {
    loadingInstance.close();
  });
};
// 下载
const downLoad = (row: IFileNameList) => {
  MessageUtils.confirmMessageBox('确定下载该文件？','导出确认').then(() => {
    const loadingInstance = MessageUtils.loading('请稍后');
    FileApi.downloadFile(row.fileName).then(res => {
      if (res.status == 200) {
        const blob = new Blob([res.data]);
        const downloadElement = document.createElement('a');
         //创建下载的链接
        downloadElement.href = URL.createObjectURL(blob);
        downloadElement.download = row.fileName; //下载后文件名
        document.body.appendChild(downloadElement);
        MessageUtils.successMessage('您的下载将尽快开始，请稍后');
        downloadElement.click(); //点击下载
        document.body.removeChild(downloadElement); //下载完成移除元素
        return;
      }
      MessageUtils.errMessage('请求服务器失败：' + res.statusText);
    }).catch(() => {}).finally(() => {
      loadingInstance.close();
    })
  })

}
</script>

<template>
  <div style="margin-left: 10px;margin-top: 10px; width: calc(100vw - 280px);">
    <h3>{{t('tabPage.fileManage.title')}}</h3>
    <el-row class="searchCard">
      <el-button type="primary" @click="query" style="margin: 10px">{{ t('baseButton.queryOrReflush')}}</el-button>
    </el-row>
    <el-row class="dataTable">
      <el-table :data="tableData" stripe border style="width: 100%" max-height="calc(100vh - 320px)">
        <el-table-column type="index" width="80" :label="t('baseColName.id')" align="center" show-overflow-tooltip fixed="left"/>
        <el-table-column prop="fileName" :label="t('tabPage.fileManage.tableCol.fileName')" show-overflow-tooltip/>
        <el-table-column :label="t('baseColName.action')" width="120" align="center">
          <template #default="scope">
            <el-button size="small" type="primary" @click="downLoad(scope.row)">{{ t('baseButton.download')}}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
  </div>
</template>

<style scoped>

</style>