<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="档案：">
          <archives-select
            v-model="filter.paperArchivesId"
            placeholder="请选择档案"
          >
          </archives-select>
        </el-form-item>
        <el-form-item label="课程：">
          <course-select
            v-model="filter.courseName"
            :filter-data="filter"
            placeholder="请选择课程"
          >
          </course-select>
        </el-form-item>
        <el-form-item label="教学班：">
          <teach-clazz-select
            v-model="filter.teachClazzName"
            placeholder="请选择教学班"
            :filter-data="filter"
          >
          </teach-clazz-select>
        </el-form-item>
        <el-form-item label-width="0px">
          <el-button type="primary" @click="search">查询</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="id"
          label="任务ID"
          min-width="160"
        ></el-table-column>
        <el-table-column
          prop="scanTaskName"
          label="任务名称"
          min-width="160"
        ></el-table-column>
        <el-table-column prop="courseName" label="课程" min-width="200">
        </el-table-column>
        <el-table-column
          prop="teachClazzName"
          label="教学班"
          min-width="100"
        ></el-table-column>
        <el-table-column
          prop="studentCount"
          label="学生数"
          min-width="100"
        ></el-table-column>
        <el-table-column
          prop="scanCount"
          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>

    <!-- ScanTaskProcessDialog -->
    <scan-task-process-dialog
      ref="ScanTaskProcessDialog"
      :task="curRow"
      @on-close="getList"
    ></scan-task-process-dialog>
  </div>
</template>

<script>
import { taskListPage } from "../api";
// import ScanTaskProcessDialog from "../components/ScanTaskProcessDialog.vue";
import ScanTaskProcessDialog from "../components/ScanTaskDialog.vue";

export default {
  name: "task-manage",
  components: {
    ScanTaskProcessDialog
  },
  data() {
    return {
      curTab: "all",
      tabs: [
        {
          name: "全部",
          val: "all"
        },
        {
          name: "我的",
          val: "my"
        }
      ],
      cacheData: {
        all: {},
        my: {}
      },
      filter: {
        paperArchivesId: "",
        courseName: "",
        teachClazzName: ""
      },
      current: 1,
      size: 10,
      total: 0,
      dataList: [],
      curRow: {},
      recordList: [],
      courseList: [],
      teachingClassList: []
    };
  },
  mounted() {
    this.search();
  },
  methods: {
    search() {
      this.cacheData[this.curTab] = {};
      this.toPage(1);
    },
    async getList() {
      let datas = {
        pageNumber: this.current,
        pageSize: this.size
      };
      if (this.curTab === "all") {
        datas = { ...datas, ...this.filter };
      } else {
        datas = { ...datas, isMine: true };
      }
      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;
      this.$refs.ScanTaskProcessDialog.open();
    }
  }
};
</script>
