<!-- 
 * @Description: 专业管理
 * @Author: wwp
 * @Date: 2024-7-15
-->
<template>
  <div class="main-box">
    <div class="table-box">
      <ProTable ref="proTable" title="专业管理" :columns="columns" rowKey="id" :request-api="userManageMajorApi.page">
        <!-- 表格 header 按钮 -->
        <template #tableHeader="scope">
          <s-button suffix="专业" @click="onOpen(FormOptEnum.ADD)" />
          <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.EDIT" @click="onOpen(FormOptEnum.EDIT, scope.row)" />
            <s-button link :opt="FormOptEnum.DELETE" @click="onDelete([scope.row.id], `删除专业`)" />
          </el-space>
        </template>
      </ProTable>
    </div>
    <!-- 专业新增/编辑表单 -->
    <Form ref="formRef"></Form>
  </div>
</template>
<script setup lang="tsx" name="SysUserKeyPersonnel">
import { userManageMajorApi,SysMajor,userManageDepartmentApi } from "@/api";
import { useHandleData } from "@/hooks/useHandleData";
import { FormOptEnum } from "@/enums";
import Form from "./components/form.vue";
import { ArrowDown } from "@element-plus/icons-vue";
import { ColumnProps, ProTableInstance } from "@/components/ProTable/interface";
import { ElMessage } from "element-plus";
import { useUserStore } from "@/stores/modules";
import { TokenEnum } from "@/enums";
import type { UploadProps } from "element-plus";
// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const faceUrl = ref('');
const visible = ref(false); //是否显示专业表单
const proTable = ref<ProTableInstance>();
const userStore = useUserStore();
const { accessToken } = userStore;
let departmentOptions = ref<any>([]);
onMounted(() => {
  getDepartmentList();
});

const getDepartmentList = async () => {
  const { data } = await userManageDepartmentApi.list({});
  departmentOptions.value = data;
};

// 表格配置项
const columns: ColumnProps<SysMajor.MajorInfo>[] = [
  { type: "selection", fixed: "left", width: 50 },
  
  {
    prop: "code",
    label: "专业编号"
  },
  {
    prop: "name",
    label: "专业名称"
  },
  {
    prop: "depId",
    label: "所属院系",
    enum: departmentOptions, 
    search: { 
      el: "select",
      // span: 1
    },
    fieldNames: { label: "name", value: "id" } 
  },
  {
    prop: "introduce",
    label: "专业简介"
  },
  { prop: "operation", label: "操作", width: 250, fixed: "right" }
];

// 专业表单引用
const formRef = ref<InstanceType<typeof Form> | null>(null);

/**
 * 打开表单
 * @param opt  操作类型
 * @param record  记录
 */
function onOpen(opt: FormOptEnum, record: {} | SysMajor.MajorInfo = {}) {
  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(userManageMajorApi.delete, {ids}, msg);
  RefreshTable(); //刷新表格
}

// 刷新表格
const RefreshTable = () => {
  proTable.value?.refresh();
}

</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>
