<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="80px"
        @submit.prevent="fnResetPaginationTransfer"
      >
        <el-row>
          <el-col :span="4">
            <el-form-item label="任务名称" prop="KEYWORDS">
              <el-input
                v-model="searchForm.KEYWORDS"
                placeholder="请输入任务名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="开始时间" prop="STARTTIME">
              <el-date-picker
                v-model="searchForm.STARTTIME"
                value-format="YYYY-MM-DD"
                format="YYYY-MM-DD"
                type="date"
                clearable
                placeholder="请选择开始时间"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="结束时间" prop="ENDTIME">
              <el-date-picker
                v-model="searchForm.ENDTIME"
                value-format="YYYY-MM-DD"
                format="YYYY-MM-DD"
                type="date"
                clearable
                placeholder="请选择结束时间"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="状态" prop="STATUS">
              <el-select
                v-model="searchForm.STATUS"
                clearable
                placeholder="请选择状态"
              >
                <el-option
                  v-for="item in data.statusList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label-width="10px">
              <el-button type="primary" native-type="submit">搜索</el-button>
              <el-button native-type="reset" @click="fnResetPaginationTransfer">
                重置
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        :data="list"
        @get-data="fnGetDataTransfer"
        v-model:pagination="pagination"
      >
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="STUDY_NAME"
          label="任务名称"
        />
        <el-table-column
          show-overflow-tooltip
          prop="CURRICULUMUSERS"
          label="任务状态"
        >
          <template v-slot="{ row }">
            {{ fnCompareDate(row) }}
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="post_type_name"
          label="岗位培训类型"
        />
        <el-table-column prop="SUM_CLASSHOUR" label="任务学时" />
        <el-table-column prop="yixue" label="已学学时" />
        <el-table-column prop="STAGEEXAMSCORE" label="考试结果">
          <template v-slot="{ row }">
            <template v-if="row.STAGEEXAMSCORE === '-1'">未参加</template>
            <template
              v-else-if="Number(row.STAGEEXAMSCORE) >= Number(row.PASSSCORE)"
            >
              已通过
            </template>
            <template
              v-else-if="Number(row.STAGEEXAMSCORE) < Number(row.PASSSCORE)"
            >
              未通过
            </template>
          </template>
        </el-table-column>
        <el-table-column prop="PEIXUE_START_TIME" label="开始时间" />
        <el-table-column prop="PEIXUE_END_TIME" label="结束时间" />
        <el-table-column label="操作" align="center" width="200">
          <template v-slot="{ row }">
            <el-button
              v-if="fnCompareDate(row) === '已结束'"
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/archives/user/detail',
                  query: {
                    STUDYTASK_ID: row.STUDYTASK_ID,
                    USER_ID: USER_ID,
                  },
                })
              "
            >
              档案详情
            </el-button>
            <el-button
              v-else
              type="primary"
              text
              link
              disabled
              @click="ElMessage.warning('任务未完成,无法生成档案')"
            >
              档案详情
            </el-button>
          </template>
        </el-table-column>
      </layout-table>
    </layout-card>
  </div>
</template>

<script setup>
import LayoutTable from "@/components/table/index.vue";
import LayoutCard from "@/components/card/index.vue";
import { getTaskListByUser } from "@/request/archives.js";
import useListData from "@/assets/js/useListData.js";
import { reactive } from "vue";
import { useRoute, useRouter } from "vue-router";
import { serialNumber } from "@/assets/js/utils.js";
import { ElMessage } from "element-plus";

const router = useRouter();
const route = useRoute();
const { USER_ID } = route.query;

const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getTaskListByUser, {
    otherParams: {
      STUDY_USER_ID: USER_ID,
    },
  });

const data = reactive({
  statusList: [
    { value: 1, label: "未开始" },
    { value: 2, label: "进行中" },
    { value: 3, label: "已结束" },
  ],
});

const fnGetDataTransfer = async () => {
  await fnGetData({
    STUDY_USER_ID: USER_ID,
  });
};

const fnResetPaginationTransfer = async () => {
  await fnResetPagination({
    STUDY_USER_ID: USER_ID,
  });
};

const fnCompareDate = (row) => {
  const currentDatetime = new Date();
  const year = currentDatetime.getFullYear();
  const month = String(currentDatetime.getMonth() + 1).padStart(2, "0");
  const day = String(currentDatetime.getDate()).padStart(2, "0");
  const dateNow = new Date(`${year}-${month}-${day}`).getTime();
  if (new Date(row.PEIXUE_START_TIME).getTime() > dateNow) {
    return "未开始";
  } else if (
    new Date(row.PEIXUE_START_TIME).getTime() <= dateNow &&
    new Date(row.PEIXUE_END_TIME).getTime() >= dateNow
  ) {
    return "进行中";
  } else if (new Date(row.PEIXUE_END_TIME).getTime() <= dateNow) {
    return "已结束";
  }
};
</script>

<style scoped lang="scss"></style>
