<!-- 
 * @Description: 教师点名
 * @Author: syy 
 * @Date: 2024-7-15
-->
<template>
  <div class="main-box">
    <div class="table-box">
      <ProTable ref="proTable" title="任务列表" :columns="columns" rowKey="id" :request-api="attendanceRoolcallApi.getTaskPageList">
        <!-- 表格 header 按钮 -->
        <template #tableHeader="scope">
          <s-button :opt="FormOptEnums.INITIATE" @click="onOpen(FormOptEnums.INITIATE, {})" />
          <s-button
            type="danger"
            :opt="FormOptEnum.DELETE"
            plain
            suffix="点名任务"
            :disabled="!scope.isSelected"
            @click="onDelete(scope.selectedListIds, '删除点名任务')"
          />
        </template>
        <!-- 表格操作栏 -->
        <template #operation="scope">
          <el-space>
            <s-button link :opt="FormOptEnum.VIEW" @click="onView(scope.row)" />
            <s-button link :opt="FormOptEnum.DELETE" @click="onDelete([scope.row.personId], `删除点名任务`)" />
          </el-space>
        </template>
      </ProTable>
    </div>
    <!-- 发起新增/编辑表单 -->
    <Form ref="formRef"></Form>
  </div>
</template>
<script setup lang="tsx" name="SysUserPersonnel">
import { attendanceRoolcallApi} from "@/api";
import { getMajorList, getDepartmentList, getClassList } from "@/mixin";
import { useHandleData } from "@/hooks/useHandleData";
import { FormOptEnum } from "@/enums";
import Form from "./components/form/index.vue";
import { ColumnProps, ProTableInstance } from "@/components/ProTable/interface";
import { useRouter } from "vue-router";
const router = useRouter();
// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref<ProTableInstance>();
const departmentOptions = ref<any>([])
const majorOptions = ref<any>([])
const classOptions = ref<any>([])
//监听depId/majorId变化
watch(
  () => [proTable.value!?.searchParam?.depId,proTable.value!?.searchParam?.majorId],
  async ([newDepId,newMajorId],[oldDepId,oldMajorId]) => {
    if(newDepId !== oldDepId){
      majorOptions.value =await getMajorList(newDepId)
      classOptions.value = [];
      proTable.value!.searchParam.majorId = "";
      proTable.value!.searchParam.personSetId = ""
    }
    if(newMajorId !== oldMajorId){
      classOptions.value = await getClassList(newMajorId)
    }
    
    
  } ,{ deep: true, immediate: false }
);
/** 表单操作类型枚举 */
enum FormOptEnums {
  /** 发起 */
  INITIATE = "发起",
  /* 查看点名任务 */
  VIEWTASK = "查看",
  /* 修改点名记录 */
  EDITTASK = "修改点名记录"
}

// 表格配置项
const columns: ColumnProps[] = [
  { type: "selection", fixed: "left", width: 50 },
  {
    prop: "depId",
    label: "所属系部",
    enum: departmentOptions,
    fieldNames: { label: "name", value: "id" },
    search: {
      el: "select",
      span:1,
      props: {
        clearable: false,
        placeholder: "请选择系部",
      },
    },
    isShow: false
  },
  {
    prop: "majorId",
    label: "所属专业",
    enum: majorOptions,
    fieldNames: { label: "name", value: "id" },
    search: {
      el: "select",
      span:1,
      props: {
        clearable: false,
        placeholder: "请选择专业",
      },
    },
    isShow: false
  },
  {
    prop: "personSetId",
    label: "所属班级",
    enum: classOptions,
    fieldNames: { label: "personSetName", value: "personSetId" },
    search: {
      el: "select",
      span:1,
      props: {
        clearable: false,
        placeholder: "请选择所属班级",
      },
    },
    isShow: false
  },
  {
    prop: "cameraName",
    label: "摄像头名称"
  },
  {
    prop: "continueTime",
    label: "持续时间"
  },
  {
    prop: "similarity",
    label: "相似度"
  },
  { prop: "operation", label: "操作", width: 250, fixed: "right" }
];
const onView = (row: any) => {
  router.push( {
    path:'/attendance/roolcall/detail',
    query: {
      taskId: row.taskId,
      personSetId:row.personSetId
    }
  });
};
// 发起任务表单引用
const formRef = ref<InstanceType<typeof Form> | null>(null);

/**
 * 打开表单
 * @param opt  操作类型
 * @param record  记录
 */
 function onOpen(opt: FormOptEnums, record: {}) {
  formRef.value?.onOpen({ opt: opt, record: record, successful: RefreshTable });
}



/**
 * 任务删除
 * @param ids  id数组
 */
 async function onDelete(ids: string[], msg: string) {
  if(ids.length === 0){
    ElMessage({
      message: '请选择要删除的人员',
      type: 'warning'
    });
    return
  }
  // 二次确认 => 请求api => 刷新表格
  await useHandleData(attendanceRoolcallApi.delete, {id: ids.join(",") }, msg);
  RefreshTable(); //刷新表格
}

// 刷新表格
const RefreshTable = () => {
  proTable.value?.refresh();
}
onMounted(async () => {
  // 获取系部下拉数据
  departmentOptions.value = await getDepartmentList();
  // 获取班级
  classOptions.value = await getClassList('')
});
</script>
<style scoped lang="scss">
.table-box {
  width: 100%;
  height: 100%;
}
.custom-tree-node {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: space-between;
  padding-right: 8px;
  font-size: 14px;
}
</style>
