<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.courseCode"
            :filter-data="filter"
            placeholder="请选择课程"
          >
          </course-select>
        </el-form-item>
        <el-form-item label-width="0px">
          <el-button type="primary" @click="search">查询</el-button>
          <el-button v-if="openBarCode" type="primary" @click="toSetOrcArea"
            >设置条码识别区</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="archivesName"
          label="档案名称"
          min-width="160"
        ></el-table-column>
        <el-table-column prop="courseName" label="课程" min-width="200">
          <span slot-scope="scope">
            {{ scope.row.courseName }}（{{ scope.row.courseCode }}）
          </span>
        </el-table-column>
        <el-table-column
          prop="studentCount"
          label="学生数"
          min-width="100"
        ></el-table-column>
        <el-table-column
          prop="studentCount"
          label="实扫/已上传（采集）"
          min-width="100"
        >
          <span slot-scope="scope">
            {{ scope.row.scanCount }} / {{ scope.row.uploadCount }}
          </span>
        </el-table-column>
        <el-table-column
          prop="scanCount"
          label="已上传（后台）"
          min-width="100"
        ></el-table-column>
        <el-table-column
          v-if="curTab === 'my'"
          class-name="action-column"
          prop="enable"
          label="状态"
          width="80"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              :class="scope.row.enable ? 'btn-danger' : 'btn-primary'"
              type="text"
              @click="toEnable(scope.row)"
            >
              {{ scope.row.enable ? "已完成" : "未完成" }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="80"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              v-if="!scope.row.enable"
              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>
    <!-- OcrAreaSetDialog -->
    <ocr-area-set-dialog
      v-if="openBarCode"
      ref="OcrAreaSetDialog"
      @modified="getOcrArea"
    >
    </ocr-area-set-dialog>
  </div>
</template>

<script>
import db from "../../../plugins/db";
import { taskListPage, enableScanTask } from "../api";
import ScanTaskProcessDialog from "../components/ScanTaskProcessDialog.vue";
import OcrAreaSetDialog from "../components/OcrAreaSetDialog.vue";
// import ScanTaskProcessDialog from "../components/ScanTaskDialog.vue";

export default {
  name: "task-manage",
  components: {
    ScanTaskProcessDialog,
    OcrAreaSetDialog
  },
  data() {
    return {
      curTab: "all",
      tabs: [
        {
          name: "全部",
          val: "all"
        },
        {
          name: "我的",
          val: "my"
        }
      ],
      cacheData: {
        all: {},
        my: {}
      },
      filter: {
        paperArchivesId: "",
        courseCode: "",
        isMine: false
      },
      current: 1,
      size: 10,
      total: 0,
      dataList: [],
      curRow: {},
      recordList: [],
      courseList: [],
      teachingClassList: [],
      ocrArea: null
    };
  },
  computed: {
    openBarCode() {
      return this.$store.state.user.openBarCode;
    }
  },
  mounted() {
    this.getOcrArea();
    this.search();
  },
  methods: {
    search() {
      this.cacheData[this.curTab] = {};
      this.toPage(1);
    },
    async getOcrArea() {
      const ocrArea = await db.getDict("ocrArea", "").catch(() => {});
      this.ocrArea = ocrArea || null;
      this.$store.commit(
        "client/setOcrArea",
        ocrArea ? JSON.parse(ocrArea) : {}
      );
    },
    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;
      this.updateTaskCount();
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    async updateTaskCount() {
      for (let i = 0; i < this.dataList.length; i++) {
        const task = this.dataList[i];
        const scanCount = await db.countScanList({ taskId: task.id });
        this.$set(task, "scanCount", scanCount);
        const uploadCount = await db.countScanList({
          taskId: task.id,
          isUpload: 1
        });
        this.$set(task, "uploadCount", uploadCount);
      }
    },
    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) {
      if (row.enable) return;
      if (this.openBarCode && !this.ocrArea) {
        this.$message.error("请先设置条形码识别区！");
        return;
      }
      this.curRow = row;
      this.$refs.ScanTaskProcessDialog.open();
    },
    toSetOrcArea() {
      this.$refs.OcrAreaSetDialog.open();
    },
    async toEnable(row) {
      await enableScanTask({
        paperScanTaskId: row.id,
        enable: !row.enable
      });
      row.enable = !row.enable;
    }
  }
};
</script>
