<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="100px"
        @submit.prevent="fnResetPagination"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="班级名称" prop="KEYWORDS">
              <el-input
                v-model="searchForm.KEYWORDS"
                placeholder="请输入班级名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="开始时间" prop="STARTTIME">
              <el-date-picker
                v-model="searchForm.STARTTIME"
                type="date"
                value-format="YYYY-MM-DD"
                format="YYYY-MM-DD"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="结束时间" prop="ENDTIME">
              <el-date-picker
                v-model="searchForm.ENDTIME"
                type="date"
                value-format="YYYY-MM-DD"
                format="YYYY-MM-DD"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="状态" prop="STATUS">
              <el-select v-model="searchForm.STATUS">
                <el-option
                  v-for="item in statusList"
                  :key="item.ID"
                  :label="item.NAME"
                  :value="item.ID"
                />
              </el-select>
            </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-button type="primary" @click="fnExport"> 导出 </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"
        @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="NAME" label="班级名称" />
        <el-table-column prop="DEPARTMENT_NAME" label="部门" />
        <el-table-column prop="POST_NAME" label="工种" />
        <el-table-column label="班级状态">
          <template #default="{ row }">
            {{ translationStatus(row.STATE, statusList) }}
          </template>
        </el-table-column>
        <el-table-column prop="TRAININGTYPE_NAME" label="培训类型" />
        <el-table-column prop="POSTTYPE_NAME" label="岗位类型" />
        <el-table-column prop="TRAINLEVEL_NAME" label="培训级别" />
        <el-table-column prop="SUM_CLASSHOUR" label="任务学时" />
        <el-table-column prop="COMPLETE_CLASSHOUR" label="已学学时" />
        <el-table-column label="考试结果">
          <template #default="{ row }">
            {{ translationStatus(row.STAGEEXAMSTATE, examStatus) }}
          </template>
        </el-table-column>
        <el-table-column prop="START_TIME" label="开始时间" />
        <el-table-column prop="END_TIME" label="结束时间" />
        <el-table-column label="操作" width="170">
          <template #default="{ row }">
            <el-button
              type="primary"
              text
              link
              @click="fnPersonnelRegistrationForm(row.STUDENT_ID, row.USER_ID)"
            >
              人员登记表
            </el-button>
            <el-button
              :disabled="
                !(
                  row.STATE === '6' ||
                  (row.EXAMINATION === 1 &&
                    parseInt(row.STAGEEXAMSCORE) > -1 &&
                    parseInt(row.STUDYSTATE) !== 5) ||
                  (row.EXAMINATION === 0 && parseInt(row.STUDYSTATE) > 1)
                )
              "
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/archives_management/user/class/archives',
                  query: {
                    CLASS_ID: row.CLASS_ID,
                    CLASS_NAME: row.NAME,
                    STUDENT_ID: row.STUDENT_ID,
                    STUDENT_NAME: row.STUDENT_NAME,
                    CORP_NAME: row.CORP_NAME,
                    RECORDOR_SIGN: row.RECORDOR_SIGN,
                    ASSESSOR_SIGN: row.ASSESSOR_SIGN,
                    SAFETYDEPTOR_SIGN: row.SAFETYDEPTOR_SIGN,
                    POSTTYPE: row.POSTTYPE,
                    POSTTYPE_NAME: row.POSTTYPE_NAME,
                    INDUSTRY_ALL_NAME: row.INDUSTRY_ALL_NAME,
                    INDUSTRY_ALL_TYPE: row.INDUSTRY_ALL_TYPE,
                    TRAINLEVEL: row.TRAINLEVEL,
                    TRAINLEVEL_NAME: row.TRAINLEVEL_NAME,
                    PERSONNEL_TYPE: row.PERSONNEL_TYPE,
                  },
                })
              "
            >
              档案详情
            </el-button>
          </template>
        </el-table-column>
      </layout-table>
      <personnel-registration-form
        v-model:visible="data.personnelRegistrationFormDialog.visible"
        :student-id="data.personnelRegistrationFormDialog.STUDENT_ID"
        :user-id="data.personnelRegistrationFormDialog.USER_ID"
      />
    </layout-card>
    <class-file-export
      v-model:visible="classFileExportDialogVisible"
      @submit="fnSubmit"
    />
  </div>
</template>

<script setup>
import { serialNumber, translationStatus } from "@/assets/js/utils.js";
import useListData from "@/assets/js/useListData.js";
import { reactive, ref } from "vue";
import PersonnelRegistrationForm from "./components/personnel_registration_form.vue";
import {
  classDetailsDownload,
  getUserClassesList,
} from "@/request/training_archive_management.js";
import { useRoute, useRouter } from "vue-router";
import ClassFileExport from "@/views/archives_management/user/components/class_file_export.vue";
import { ElMessage } from "element-plus";

const route = useRoute();
const router = useRouter();
const { USER_ID } = route.query;
const statusList = [
  { ID: "1", NAME: "未申请" },
  { ID: "4", NAME: "待开班" },
  { ID: "5", NAME: "培训中" },
  { ID: "6", NAME: "培训结束" },
];
const examStatus = [
  { ID: "0", NAME: "不考试" },
  { ID: "1", NAME: "待考试" },
  { ID: "2", NAME: "考试未通过" },
  { ID: "3", NAME: "考试通过" },
  { ID: "4", NAME: "未参加" },
];
const data = reactive({
  personnelRegistrationFormDialog: {
    visible: false,
    STUDENT_ID: "",
    USER_ID: "",
  },
});
const classFileExportDialogVisible = ref(false);
const { list, pagination, searchForm, fnGetData, fnResetPagination, tableRef } =
  useListData(getUserClassesList, {
    otherParams: { USER_ID, TRAINTYPE_ID: "c70bf859512241579a8a30fc5d1ae153" },
  });
const fnPersonnelRegistrationForm = (STUDENT_ID, USER_ID) => {
  data.personnelRegistrationFormDialog.STUDENT_ID = STUDENT_ID;
  data.personnelRegistrationFormDialog.USER_ID = USER_ID;
  data.personnelRegistrationFormDialog.visible = true;
};
const fnExport = () => {
  const selectionData = tableRef.value.getSelectionRows();
  if (selectionData.length === 0) {
    ElMessage.warning("请选中要导出的项");
    return;
  }
  classFileExportDialogVisible.value = true;
};
const fnSubmit = async (checkArchivesList) => {
  const selectionData = tableRef.value.getSelectionRows();
  let cnt = 0;
  if (checkArchivesList.indexOf("人员登记表") !== -1) {
    selectionData.forEach((item) => {
      if (
        !(
          item.STATE === "6" ||
          (item.EXAMINATION === "1" &&
            parseInt(item.STAGEEXAMSCORE) > -1 &&
            parseInt(item.STUDYSTATE) !== "5") ||
          (item.EXAMINATION === 0 && parseInt(item.STUDYSTATE) > 1)
        )
      ) {
        cnt = cnt + 1;
      }
    });
  }
  if (cnt === selectionData.length) {
    ElMessage.warning("所选中的内容不符合导出条件");
  } else {
    const curriculum = [];
    for (let i = 0; i < selectionData.length; i++) {
      curriculum.push({
        STUDENT_ID: selectionData[i].STUDENT_ID,
        CLASS_ID: selectionData[i].CLASS_ID,
        STUDENT_NAME: selectionData[i].STUDENT_NAME,
        ENTERPRISE_ID: selectionData[i].ENTERPRISE_ID,
        USER_ID: selectionData[i].USER_ID,
      });
    }
    await classDetailsDownload({
      curriculum: JSON.stringify(curriculum),
      checkArchivesList,
    });
  }
};
</script>

<style scoped></style>
