<!-- 用户管理 -->
<template>
  <div class="app-container">
    <el-card shadow="never" class="table-container">
      <template #header>
        <div class="flex justify-between">
          <div>
            <el-button type="primary" @click="handleAdd()"
              ><i-ep-plus />添加数据集</el-button
            >
          </div>
          <div>
            <el-form ref="queryFormRef" :model="queryParams" :inline="true">
              <el-form-item label="数据集名称" prop="dataset_name">
                <el-input
                  v-model="queryParams.dataset_name"
                  placeholder="请输入数据集名称"
                  clearable
                  style="width: 200px"
                  @keyup.enter="handleQuery"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="handleQuery"
                  ><i-ep-search />搜索</el-button
                >
                <el-button @click="resetQuery">
                  <i-ep-refresh />
                  重置</el-button
                >
              </el-form-item>
            </el-form>
          </div>
        </div>
      </template>
      <el-table
        v-loading="loading"
        :data="pageData"
        stripe
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="数据集名称" align="left" prop="dataset_name" width="250" />
        <el-table-column label="版本" align="left" width="80" prop="dataset_version" />
        <el-table-column label="数据集描述" align="left" prop="dataset_desc" />
        <el-table-column label="适用模型类型" width="120" align="left" prop="modl_sub_type_name" />
        <el-table-column label="数据格式" width="200" align="left" prop="dats_dataset_format" />
        <el-table-column label="图像分辨率" width="150" align="left" prop="image_resolution" />
        <el-table-column label="数量" width="80" align="left" prop="frame_count" />
        <el-table-column label="标注类型" width="180" align="left" prop="dats_label_type" />
        <el-table-column label="操作" align="center" fixed="right" width="200">
          <template #default="scope">
            <el-button
              text
              type="primary"
              size="small"
              @click="handleDetail(scope.row)"
              ><i-ep-edit />查看</el-button
            >
            <el-button
              text
              type="primary"
              size="small"
              @click="handleDelete(scope.row)"
              ><i-ep-delete />删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.page_num"
        v-model:limit="queryParams.page_size"
        @pagination="handleQuery"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import DataSetApi from "@/api/dataSet";

const router = useRouter();

const loading = ref(false); //  加载状态
const total = ref(0); // 数据总数
const pageData = ref([]); // 分页数据
const removeIds = ref([]); // 删除ID集合 用于批量删除
const queryFormRef = ref(ElForm); // 查询表单
const queryParams = reactive({
  page_num: 1,
  page_size: 10,
  dataset_name: null,
  modl_sub_type: null,
  dats_label_type: null,
  dats_dataset_format: null
});

/** 查询 */
function handleQuery() {
  loading.value = true;
  DataSetApi.list(queryParams)
    .then((res) => {
      pageData.value = res.data.data.raw_dataset_list;
      total.value = res.data.data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.page_num = 1;
  handleQuery();
}

/** 行选中 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.dataset_id);
}

/** 删除数据 */
function handleDelete(row: { [key: string]: any }) {
  ElMessageBox.confirm("确认删除数据集信息?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(function () {
    DataSetApi.deleteRaw(row.dataset_id).then((res) => {
      ElMessage.success("删除成功");
      resetQuery();
    });
  });
}

/** 新增原始数据集 */
function handleAdd() {
  router.push({ path: "/dataMgr/datasAdd" });
}

/** 查看/修改数据集 */
function handleDetail(row: { [key: string]: any }) {
  router.push({ path: "/dataMgr/datasEdit", query: { id: row.dataset_id } });
}

onMounted(() => {
  handleQuery();
});
</script>
