<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="60px"
        @submit.prevent="fnResetPagination"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="姓名" prop="KEYWORDS">
              <el-input
                v-model="searchForm.KEYWORDS"
                placeholder="请输入姓名"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label-width="10px">
              <el-button type="primary" native-type="submit">搜索</el-button>
              <el-button native-type="reset" @click="fnResetPagination">
                重置
              </el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="10px" class="end">
              <el-button type="primary" @click="fnBatchDownload">
                下载补充档案
              </el-button>
              <el-button type="primary" @click="classFileDialogVisible = true">
                班级档案
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        ref="tableRef"
        v-model:pagination="pagination"
        :data="list"
        row-key="USER_ID"
        @get-data="fnGetData"
      >
        <el-table-column reserve-selection type="selection" width="55" />
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column prop="USERNAME" label="用户名" />
        <el-table-column prop="NAME" label="姓名" />
        <el-table-column prop="CLASS_COUNT" label="班级数" />
        <el-table-column prop="COMPLETE_COUNT" label="完成班级数" />
        <el-table-column prop="CLIENT" label="是否补充">
          <template #default="{ row }">
            {{
              row.ARCHIVES_AWARD_PUN_LOG_ID || row.ARCHIVES_SPECIAL_WORK_ID
                ? "是"
                : "否"
            }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <el-button
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/archives_management/user/class',
                  query: {
                    USER_ID: row.USER_ID,
                  },
                })
              "
            >
              班级详情
            </el-button>
          </template>
        </el-table-column>
      </layout-table>
    </layout-card>
    <class-file v-model:visible="classFileDialogVisible" />
  </div>
</template>

<script setup>
import { serialNumber } from "@/assets/js/utils.js";
import useListData from "@/assets/js/useListData.js";
import {
  downloadAward,
  getStudentsList,
} from "@/request/training_archive_management.js";
import { ElMessage, ElMessageBox } from "element-plus";
import { debounce } from "throttle-debounce";
import { useRouter } from "vue-router";
import { ref } from "vue";
import ClassFile from "./components/class_file.vue";

const router = useRouter();
const { list, pagination, searchForm, fnGetData, fnResetPagination, tableRef } =
  useListData(getStudentsList, {
    otherParams: { TRAINTYPE_ID: "c70bf859512241579a8a30fc5d1ae153" },
  });
const classFileDialogVisible = ref(false);
const fnBatchDownload = debounce(
  1000,
  async () => {
    const selectionData = tableRef.value.getSelectionRows();
    if (selectionData.length === 0) {
      ElMessage.warning("请选中要下载的学员补充档案...");
      return;
    }
    const ids = selectionData
      .map((item) => {
        return item.USER_ID;
      })
      .join(",");
    await downloadAward({
      ids,
    });
    await ElMessageBox.confirm(
      "导出后请前往档案下载中下载该档案！",
      "温馨提示",
      { type: "info" }
    );
    tableRef.value.clearSelection();
  },
  { atBegin: true }
);
</script>
<style scoped></style>
