<template>
  <div class="time-task-manage">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <el-form-item prop="semesterId" label="学期：">
          <semester-select v-model="filter.semesterId"></semester-select>
        </el-form-item>
        <el-form-item prop="examTypeId" label="考试：">
          <exam-type-select
            v-model="filter.examTypeId"
            :semester-id="filter.semesterId"
          ></exam-type-select>
        </el-form-item>
        <el-form-item label="定时任务时间：">
          <el-date-picker
            v-model="createTime"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="定时任务开始时间"
            end-placeholder="定时任务结束时间"
            value-format="timestamp"
            align="right"
            unlink-panels
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :disabled="!filter.semesterId || !filter.examTypeId"
            @click="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button
          type="primary"
          icon="el-icon-circle-plus-outline"
          @click="toAdd"
          >新增</el-button
        >
      </div>
    </div>
    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="dataList">
        <el-table-column
          type="index"
          label="序号"
          width="70"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column prop="schoolName" label="学校"></el-table-column>
        <el-table-column prop="semesterName" label="学期"></el-table-column>
        <el-table-column prop="examTypeName" label="考试类型"></el-table-column>
        <el-table-column prop="startTime" label="定时任务开始时间" width="180">
          <span slot-scope="scope">{{
            scope.row.startTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column prop="endTime" label="定时任务截止时间" width="180">
          <span slot-scope="scope">{{
            scope.row.endTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column prop="userName" label="操作人"> </el-table-column>
        <el-table-column prop="createTime" label="操作时间">
          <span slot-scope="scope">{{
            scope.row.createTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column class-name="action-column" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              type="text"
              @click="toEdit(scope.row)"
              >修改</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total,prev, pager, next"
          :current-page="current"
          :total="total"
          :page-size="size"
          :page-sizes="[20, 50, 100]"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>

    <modify-task
      ref="ModifyTask"
      :instance="curRow"
      @modified="getList"
    ></modify-task>
  </div>
</template>

<script>
import pickerOptions from "@/constants/datePickerOptions";
import { timeTaskList } from "../api";
import ModifyTask from "../components/ModifyTask";

export default {
  name: "time-task-manage",
  components: {
    ModifyTask,
  },
  data() {
    return {
      filter: {
        semesterId: "",
        examTypeId: "",
        startTime: "",
        endTime: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curRow: {},
      // date-picker
      createTime: [],
      pickerOptions,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      if (this.createTime) {
        datas.startTime = this.createTime[0];
        datas.endTime = this.createTime[1];
      }

      const data = await timeTaskList(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toAdd() {
      this.curRow = {};
      this.$refs.ModifyTask.open();
    },
    toEdit(row) {
      this.curRow = { ...row };
      this.$refs.ModifyTask.open();
    },
  },
};
</script>
