<template>
  <div>
    <section class="content">
      <div class="box box-info">
        <div class="box-body">
          <el-form
            :inline="true"
            :model="formSearch"
            label-position="right"
            label-width="70px"
          >
            <el-form-item label="评卷名称" class="pull-left">
              <el-select
                clearable
                filterable
                class="input"
                v-model="formSearch.markId"
                placeholder="请选择"
              >
                <el-option value>请选择</el-option>
                <el-option
                  v-for="item in markWorkSelect"
                  :label="item.markName"
                  :value="item.markId"
                  :key="item.markId"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="登录名" class="pull-left">
              <el-input
                placeholder="登录名"
                v-model="formSearch.userLoginName"
              ></el-input>
            </el-form-item>
            <el-form-item label="姓名" class="pull-left">
              <el-input
                placeholder="姓名"
                v-model="formSearch.userName"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button
                size="small"
                type="primary"
                icon="el-icon-search"
                @click="searchSetting"
                >查询</el-button
              >
            </el-form-item>
          </el-form>
          <div class="block-seperator"></div>
          <!-- 页面列表 -->
          <el-table
            stripe
            v-loading="loading"
            element-loading-text="拼命加载中"
            :data="tableData"
            border
            style="width: 100%"
          >
            <el-table-column label="姓名" width="200" prop="userName" />
            <el-table-column
              label="登录名"
              min-width="100"
              prop="userLoginName"
            />
            <el-table-column
              label="分配课程"
              min-width="200"
              prop="courseCount"
              sortable
            />
            <el-table-column
              label="完成课程"
              min-width="120"
              prop="finishedCount"
              sortable
            />
            <el-table-column
              label="完成数量"
              min-width="120"
              prop="markedCount"
              sortable
            />
            <el-table-column
              label="最低分"
              min-width="100"
              prop="minScore"
              sortable
            />
            <el-table-column
              label="最高分"
              min-width="100"
              prop="maxScore"
              sortable
            />
            <el-table-column
              label="平均分"
              min-width="100"
              prop="avgScore"
              sortable
            />
            <el-table-column
              label="标准方差"
              min-width="120"
              prop="stdDev"
              sortable
            />
            <el-table-column :context="_self" label="操作" fixed="right">
              <template slot-scope="scope">
                <el-button
                  @click="markerDetail(scope.row)"
                  type="primary"
                  size="mini"
                  plain
                  >详情</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <div class="page pull-right">
            <el-pagination
              background
              @current-change="handleSettingCurrentChange"
              @size-change="handleSizeChange"
              :current-page="currentPage"
              :page-size="pageSize"
              :page-sizes="[10, 20, 50, 100]"
              layout="total, sizes, 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: {
        userLoginName: "",
        userName: ""
      },
      tableData: [],
      oldData: [],
      currentPage: 1,
      pageSize: 10,
      total: 10,
      loading: false,
      workId: "",
      examId: "",
      markWorkName: "",
      markWorkList: []
    };
  },
  methods: {
    handleSettingCurrentChange(val) {
      this.currentPage = val;
      this.searchSetting();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.searchSetting();
    },
    pagingSetting() {
      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]);
      }
      this.tableData = tempData;
    },
    searchSetting() {
      if (!this.formSearch.markId) {
        this.$notify({
          title: "警告",
          message: "请选择评卷名称",
          type: "warning",
          duration: 1000
        });
        return false;
      }
      let obj = {};
      for (let item of this.markWorkList) {
        if (item.id === this.formSearch.markId) {
          obj = item;
          break;
        }
      }
      this.examId = obj.examId;
      this.markWorkName = obj.name;
      this.workId = this.formSearch.markId;
      this.loading = true;
      this.$http
        .get(
          DATA_PROCESS_API +
            "/markers/all/" +
            (this.currentPage - 1) +
            "/" +
            this.pageSize +
            "?workId=" +
            this.workId,
          { params: this.formSearch }
        )
        .then(response => {
          console.log(response);
          this.tableData = response.data.content;
          this.total = response.data.totalElements;
          this.loading = false;
          this.$router.push({
            path:
              "/marking/marker?" +
              new URLSearchParams(JSON.parse(JSON.stringify(this.formSearch)))
          });
        });
    },
    markerDetail(row) {
      var url =
        "/marking/marker_detail/" +
        this.workId +
        "/" +
        this.examId +
        "/" +
        this.markWorkName +
        "/" +
        row.userId +
        "/" +
        row.userName;
      this.$router.push({
        path: url
      });
    },
    //获取创建成功的markWork列表
    getMarkWorks() {
      this.$http
        .get(DATA_PROCESS_API + "/markWorks?status=1")
        .then(response => {
          this.markWorkList = response.data;
        });
    },
    backFill() {
      var formData = this.$route.query;
      if (formData && formData.markId) {
        for (var attr in formData) {
          var value = formData[attr];
          if (value && value != "null") {
            if (!isNaN(value)) {
              if (~~value == value) {
                value = parseInt(value);
              } else {
                value = parseFloat(value);
              }
            }
            this.formSearch[attr] = value;
          }
        }
        this.searchSetting();
      }
    }
  },
  computed: {
    ...mapState({ user: state => state.user }),
    markWorkSelect() {
      let markWorkNames = [];
      for (let markWork of this.markWorkList) {
        markWorkNames.push({
          markId: markWork.id,
          markName: markWork.name,
          examId: markWork.examId
        });
      }
      return markWorkNames;
    }
  },
  created() {
    this.getMarkWorks();
    this.backFill();
  }
};
</script>
<style lang="css" scoped>

li {
    list-style-type: none;
}

.searchFrame {
    margin-right: 10px;
    margin-bottom: 10px;
}

.page{
  margin-top: 10px;
}
.f_button{
    display:block;
    width:57px;
    height:20px;
    border:1px solid #CCC;
    background:#FFF;
    font-size: small;
}
</style>
