<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="100px"
        @submit.prevent="fnResetPagination"
      >
        <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="4">
            <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-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        :data="list"
        @get-data="fnGetData"
        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 prop="PEIXUE_START_TIME" label="开始时间" />
        <el-table-column prop="PEIXUE_END_TIME" label="结束时间" />
        <el-table-column prop="CURRICULUMUSERS" label="状态">
          <template v-slot="{ row }">
            {{ fnCompareDate(row) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="140">
          <template v-slot>
            <el-button
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/archives/semester/catalogue',
                  query: {
                    source: 'semester',
                  },
                })
              "
            >
              档案目录
            </el-button>
          </template>
        </el-table-column>
      </layout-table>
    </layout-card>
  </div>
</template>
<script setup>
import { reactive } from "vue";
import useListData from "@/assets/js/useListData.js";
import { getTaskList } from "@/request/archives.js";
import LayoutCard from "@/components/card/index.vue";
import LayoutTable from "@/components/table/index.vue";
import { serialNumber } from "@/assets/js/utils.js";
import { useRouter } from "vue-router";

const router = useRouter();

const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getTaskList);

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

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>
