<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="业务类型" prop="bizType">
        <el-select v-model="queryParams.bizType" placeholder="全部" clearable style="width: 160px">
          <el-option label="水量" value="SL" />
          <el-option label="水质" value="SZ" />
          <el-option label="方案" value="FA" />
          <el-option label="邮件" value="YJJT_MAIL" />
        </el-select>
      </el-form-item>
      <el-form-item label="文件名" prop="fileViewname">
        <el-input v-model="queryParams.fileViewname" placeholder="请输入文件名" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['yjjt:file:remove']">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="fileList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="文件名" prop="fileViewname" :show-overflow-tooltip="true" />
      <el-table-column label="业务类型" prop="bizType" width="100" align="center">
        <template #default="scope">
          <el-tag :type="bizTypeTag(scope.row.bizType)" size="small">{{ bizTypeText(scope.row.bizType) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="文件大小" prop="fileSize" width="120" align="center">
        <template #default="scope">
          <span>{{ formatSize(scope.row.fileSize) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文件类型" prop="fileType" width="100" align="center" />
      <el-table-column label="上传人" prop="userName" width="100" align="center" />
      <el-table-column label="上传时间" prop="fileTime" width="170" align="center" />
      <el-table-column label="操作" width="100" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['yjjt:file:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script setup name="YjjtFile">
import { listFile, delFile } from "@/api/yjjt/file";

const { proxy } = getCurrentInstance();

const fileList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const multiple = ref(true);
const total = ref(0);

const bizTypeMap = { SL: '水量', SZ: '水质', FA: '方案', YJJT_MAIL: '邮件' };
const bizTypeTagMap = { SL: 'primary', SZ: 'success', FA: 'warning', YJJT_MAIL: 'info' };

function bizTypeText(val) { return bizTypeMap[val] || val; }
function bizTypeTag(val) { return bizTypeTagMap[val] || ''; }
function formatSize(bytes) {
  if (!bytes) return '-';
  if (bytes < 1024) return bytes + ' B';
  if (bytes < 1048576) return (bytes / 1024).toFixed(1) + ' KB';
  return (bytes / 1048576).toFixed(1) + ' MB';
}

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  bizType: undefined,
  fileViewname: undefined,
});

function getList() {
  loading.value = true;
  listFile(queryParams.value).then((response) => {
    fileList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  multiple.value = !selection.length;
}

function handleDelete(row) {
  const deleteIds = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除文件编号为"' + deleteIds + '"的数据项？').then(() => {
    return delFile(deleteIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
</script>
