<template lang="html">
  <div>
    <section class="content">
      <div class="box box-info">
        <div class="box-body">
          <el-form
            :inline="true"
            :model="formSearch"
            label-position="right"
            label-width="110px"
          >
            <el-form-item label="评卷工作名称" class="pull-left">
              <el-input
                placeholder="评卷工作名称"
                v-model="formSearch.name"
                @keyup.native="searchMarkWork"
              ></el-input>
            </el-form-item>
            <!-- <el-form-item class="pull-right"> -->
            <!--
              <el-button type="primary" icon="search" @click="searchMarkWork">查询</el-button>
            -->
            <!-- </el-form-item> -->
          </el-form>
          <el-table
            v-loading="loading"
            element-loading-text="拼命加载中"
            :data="tableData"
            border
            style="width: 100%"
          >
            <el-table-column label="评卷工作名称" width="250" prop="name">
            </el-table-column>
            <el-table-column label="考试批次" width="250" prop="examName">
            </el-table-column>
            <el-table-column label="考试类型" width="100">
              <template slot-scope="scope">
                <div>
                  <span>{{ scope.row.examType | examTypeFilter }}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="进度" width="100">
              <template slot-scope="scope">
                <div>
                  <span>{{ Number.parseFloat(scope.row.progress) }}%</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="备注" width="100" prop="remark">
            </el-table-column>
            <el-table-column :context="_self" label="操作">
              <template slot-scope="scope">
                <div class="pull-left">
                  <span v-if="isAdmin">
                    <span v-if="routeType == 'overview'">
                      <el-button
                        @click="markWorkOverview(scope.row);"
                        type="primary"
                        size="mini"
                        >评卷进度</el-button
                      >
                    </span>
                    <span v-if="routeType == 'marker'">
                      <el-button
                        @click="marker(scope.row);"
                        type="primary"
                        size="mini"
                        >评卷员一览</el-button
                      >
                    </span>
                    <span v-if="routeType == 'setting'">
                      <el-button
                        @click="settingMarkWork(scope.row);"
                        type="primary"
                        size="mini"
                        >评卷设置</el-button
                      >
                    </span>
                    <span v-if="routeType == 'checking'">
                      <el-button
                        @click="paperCheck(scope.row);"
                        type="primary"
                        size="mini"
                        >试卷检查</el-button
                      >
                    </span>
                    <span v-if="routeType == 'grade'">
                      <el-button
                        @click="viewGrade(scope.row);"
                        type="primary"
                        size="mini"
                        >成绩查询</el-button
                      >
                    </span>
                  </span>
                  <span v-if="isMarker && routeType == 'marking'">
                    <el-button
                      @click="marking(scope.row);"
                      type="primary"
                      size="mini"
                      >评阅试卷</el-button
                    >
                  </span>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div class="page pull-right">
            <el-pagination
              background
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-size="pageSize"
              layout="total, prev, pager, next, jumper"
              :total="total"
            >
            </el-pagination>
          </div>
        </div>
      </div>
    </section>
  </div>
</template>

<script>
import { DATA_PROCESS_API } from "@/constants/constants";
import { mapState } from "vuex";
export default {
  data() {
    return {
      formSearch: {
        name: ""
      },
      tableData: [],
      totalTableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      markWorkId: "",
      loading: false,
      isMarkingView: true
    };
  },
  methods: {
    initMarkWork() {
      this.loading = true;
      if (this.isAdmin) {
        this.$http.get(DATA_PROCESS_API + "/markWorks").then(response => {
          this.totalTableData = response.data;
          this.total = response.data.length;
          this.filterMarkWork();
          this.paging();
          this.loading = false;
        });
      } else {
        this.$http
          .get(DATA_PROCESS_API + "/markWorks?userId=" + this.user.userId)
          .then(response => {
            this.totalTableData = response.data;
            this.total = response.data.length;
            this.filterMarkWork();
            this.paging();
            this.loading = false;
          });
      }
    },
    searchMarkWork() {
      this.filterMarkWork();
      this.paging();
    },
    filterMarkWork() {
      var tempData = this.totalTableData.filter(element => {
        if (this.formSearch.name) {
          return element.name.includes(this.formSearch.name);
        } else {
          return true;
        }
      });
      this.tableData = tempData;
      this.total = tempData.length;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.filterMarkWork();
      this.paging();
      //this.searchMarkWork()
    },
    paging() {
      var start = (this.currentPage - 1) * this.pageSize;
      var end =
        this.currentPage * this.pageSize < this.total
          ? this.currentPage * this.pageSize
          : this.total;
      var tempData = [];
      console.log(`当前页: ${this.currentPage},开始:${start},结束:${end}`);
      for (let i = start; i < end; i++) {
        tempData.push(this.tableData[i]);
      }
      console.log(tempData);
      this.tableData = tempData;
    },
    settingMarkWork(row) {
      var url =
        "/marking/mark_setting_main/" +
        row.id +
        "/" +
        row.examId +
        "/" +
        row.name;
      this.$router.push({
        path: url
      });
    },
    markWorkOverview(row) {
      var url =
        "/marking/mark_work_overview/" +
        row.id +
        "/" +
        row.examId +
        "/" +
        row.name;
      this.$router.push({
        path: url
      });
    },
    marker(row) {
      var url = "/marking/marker/" + row.id + "/" + row.examId + "/" + row.name;
      this.$router.push({
        path: url
      });
    },
    paperCheck(row) {
      var url =
        "/marking/mark_paper_check/" +
        row.id +
        "/" +
        row.examId +
        "/" +
        row.examType;
      this.$router.push({
        path: url
      });
    },
    viewGrade(row) {
      var url = "/marking/mark_grade_main/" + row.examId;
      this.$router.push({
        path: url
      });
    },
    marking(row) {
      var userId = this.user.userId;
      var self = this;
      var url = "/marking/" + row.id + "/" + row.examType;
      self.$http
        .get(
          DATA_PROCESS_API + "/markTasks?workId=" + row.id + "&userId=" + userId
        )
        .then(response => {
          console.log(response);
          if (response.data.length > 0) {
            self.$router.push({
              path: url
            });
          } else {
            self.$notify({
              message: "没有评卷任务",
              type: "warning"
            });
          }
        });
    }
  },
  computed: {
    routeType() {
      return this.$route.params.type;
    },
    ...mapState({ user: state => state.user }),
    isAdmin() {
      if (sessionStorage.getItem("AdminButtonShow")) {
        return true;
      }
      return false;
    },
    isMarker() {
      if (sessionStorage.getItem("MarkButtonShow")) {
        return true;
      }
      return false;
    }
  },
  created() {
    this.initMarkWork();
  }
};
</script>
