<template>
  <div class="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 label="档案：">
          <el-select
            v-model="filter.recordId"
            placeholder="请选择档案"
            clearable
          >
            <el-option
              v-for="item in recordList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程：">
          <el-select
            v-model="filter.courseCode"
            placeholder="请选择课程"
            clearable
          >
            <el-option
              v-for="item in courseList"
              :key="item.courseCode"
              :value="item.courseCode"
              :label="item.courseName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="教学班：">
          <el-select
            v-model="filter.teachingClassId"
            placeholder="请选择教学班"
            clearable
          >
            <el-option
              v-for="item in teachingClassList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label-width="0px">
          <el-button type="primary" @click="toPage(1)">查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="mb-4 tab-btns">
      <el-button
        v-for="tab in tabs"
        :key="tab.val"
        size="medium"
        :type="curTab == tab.val ? 'primary' : 'default'"
        @click="selectMenu(tab.val)"
        >{{ tab.name }}
      </el-button>
    </div>

    <div class="part-box part-box-pad">
      <el-table ref="TableList" size="medium" :data="dataList">
        <el-table-column
          prop="taskId"
          label="任务ID"
          min-width="160"
        ></el-table-column>
        <el-table-column
          prop="taskName"
          label="任务名称"
          min-width="160"
        ></el-table-column>
        <el-table-column prop="courseName" label="课程（代码）" min-width="200">
          <template slot-scope="scope">
            {{ scope.row.courseName }}（{{ scope.row.courseCode }}）
          </template>
        </el-table-column>
        <el-table-column
          prop="userName"
          label="教学班"
          min-width="100"
        ></el-table-column>
        <el-table-column
          prop="userName"
          label="学生数"
          min-width="100"
        ></el-table-column>
        <el-table-column
          prop="userName"
          label="已扫描数量"
          min-width="100"
        ></el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="80"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              type="text"
              @click="toScan(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"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { taskListPage } from "../api";

export default {
  name: "task-manage",
  data() {
    return {
      curTab: "all",
      tabs: [
        {
          name: "全部",
          val: "all"
        },
        {
          name: "我的",
          val: "my"
        }
      ],
      cacheData: {
        all: {},
        my: {}
      },
      filter: {
        recordId: "",
        courseCode: "",
        teachingClassId: ""
      },
      current: 1,
      size: 10,
      total: 0,
      dataList: [],
      curRow: {},
      recordList: [],
      courseList: [],
      teachingClassList: []
    };
  },
  methods: {
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size
      };
      const data = await taskListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    selectMenu(curTab) {
      this.cacheData[this.curTab] = {
        current: this.current,
        total: this.total,
        dataList: this.dataList
      };

      this.curTab = curTab;

      if (this.cacheData[this.curTab].total) {
        const { current, total, dataList } = this.cacheData[this.curTab];
        this.current = current;
        this.total = total;
        this.dataList = dataList;
      } else {
        this.toPage(1);
      }
    }
  },
  toScan(row) {
    this.curRow = row;
  }
};
</script>
