<template>
  <div>
    <section class="content">
      <div class="box box-info">
        <div class="box-body">
          <el-form
            :inline="true"
            :model="formSearch"
            ref="formSearch"
            label-width="70px"
          >
            <el-form-item label="评卷名称" prop="markId">
              <el-select
                clearable
                filterable
                class="input"
                v-model="formSearch.markId"
                placeholder="请选择"
                style="width:150px;"
                @change="getCourses"
              >
                <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="学习中心" prop="orgCode">
              <el-select
                :remote-method="getOrgs"
                remote
                :loading="getOrgsSearchLoading"
                class="input"
                clearable
                v-model="formSearch.orgCode"
                placeholder="请选择"
                filterable
                style="width:150px;"
                @focus="e => getOrgs(e.target.value)"
              >
                <el-option label="请选择" value></el-option>
                <el-option
                  v-for="item in orgList"
                  :label="item.name"
                  :value="item.code"
                  :key="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="姓名" prop="studentName">
              <el-input
                placeholder="请输入姓名"
                v-model="formSearch.studentName"
                style="width:150px;"
              ></el-input>
            </el-form-item>
            <el-form-item label="学号" prop="studentCode">
              <el-input
                placeholder="请输入学号"
                v-model="formSearch.studentCode"
                style="width:150px;"
              ></el-input>
            </el-form-item>
            <el-form-item label="标记卷" prop="tag">
              <el-select
                clearable
                v-model="formSearch.tag"
                placeholder="请选择"
                filterable
                style="width:150px;"
              >
                <el-option label="请选择" value></el-option>
                <el-option
                  v-for="item in tags"
                  :label="item.name"
                  :value="item.code"
                  :key="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="课程" prop="courseCode">
              <el-select
                clearable
                @change="getUserList"
                filterable
                class="input"
                v-model="formSearch.courseCode"
                placeholder="请选择"
                style="width:150px;"
              >
                <el-option label="请选择" value></el-option>
                <el-option
                  v-for="item in courseAllListSelect"
                  :label="item.courseInfo"
                  :value="item.code"
                  :key="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="评委" prop="userId">
              <el-select
                clearable
                class="input"
                filterable
                v-model="formSearch.userId"
                placeholder="请选择"
                style="width:150px;"
              >
                <el-option label="请选择" value></el-option>
                <el-option
                  v-for="item in userList"
                  :label="item.name"
                  :value="item.userId"
                  :key="item.userId"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="评卷备注" prop="markRemark">
              <el-input
                placeholder="请输入备注"
                v-model="formSearch.markRemark"
                style="width:150px;"
              ></el-input>
            </el-form-item>
            <div></div>
            <el-form-item>
              <el-button
                size="small"
                type="primary"
                icon="el-icon-search"
                @click="searchMarkPaperCheck"
                >查询</el-button
              >
              <el-button
                @click="resetForm('formSearch')"
                size="small"
                icon="el-icon-refresh"
                >重置</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="150" prop="orgName" />
            <el-table-column
              label="学号"
              width="120"
              prop="studentCode"
              sortable
            />
            <el-table-column label="姓名" width="120" prop="studentName" />
            <el-table-column
              label="课程代码"
              width="120"
              prop="courseCode"
              sortable
            />
            <el-table-column label="课程" width="120" prop="courseName" />
            <el-table-column label="试卷类型" width="120" prop="paperType" />
            <el-table-column label="评卷员" width="120" prop="userName" />
            <el-table-column label="主观总分" width="120" prop="score" />
            <el-table-column
              label="客观总分"
              width="120"
              prop="objectiveScore"
              sortable
            />
            <el-table-column
              label="卷面总分"
              width="120"
              prop="totalScore"
              sortable
            />
            <el-table-column label="标记卷" width="120">
              <template slot-scope="scope">
                <div>
                  <span>{{ scope.row.tag | tagFilter }}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="评卷备注" width="120">
              <template slot-scope="scope">
                <el-popover trigger="hover" name="text" placement="top">
                  <div style="width:200px;">{{ scope.row.markRemark }}</div>
                  <div slot="reference">
                    <span>{{ interceptStr(scope.row.markRemark) }}</span>
                  </div>
                </el-popover>
              </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="180">
              <template slot-scope="scope">
                <el-button
                  @click="viewPaper(scope.row)"
                  type="primary"
                  size="mini"
                  icon="el-icon-view"
                  plain
                  >原卷</el-button
                >
                <el-button
                  @click="backRefresh(scope.row)"
                  type="danger"
                  icon="el-icon-arrow-left"
                  size="mini"
                  >打回</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <div class="page pull-right">
            <el-pagination
              background
              @current-change="handleCurrentChange"
              @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 { mapState } from "vuex";
import { CORE_API, MARKING_API, DATA_PROCESS_API } from "@/constants/constants";

export default {
  data() {
    return {
      formSearch: {
        orgCode: "",
        studentName: "",
        studentCode: "",
        tag: "",
        courseCode: "",
        userId: "",
        workId: "",
        specialtyCode: "",
        examType: "",
        markRemark: ""
      },
      orgList: [],
      courseList: [],
      specialtyList: [],
      userList: [],
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      workId: "",
      examId: "",
      paper: "",
      tags: [],
      loading: false,
      examType: "",
      getOrgsSearchLoading: false,
      markWorkList: []
    };
  },
  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;
    },
    courseAllListSelect() {
      let courseSelect = [];
      for (let course of this.courseList) {
        let courseInfo = course.name + "(" + course.code + ")";
        courseSelect.push({ code: course.code, courseInfo: courseInfo });
      }
      return courseSelect;
    }
  },
  methods: {
    getMarkWorks() {
      this.$http.get(DATA_PROCESS_API + "/markWorks").then(response => {
        this.markWorkList = response.data;
      });
    },
    //查询学习中心
    getOrgs(name) {
      this.getOrgsSearchLoading = true;
      this.$http.get(CORE_API + "/org/query?name=" + name).then(response => {
        this.getOrgsSearchLoading = false;
        this.orgList = response.data;
      });
    },
    //查询课程
    getCourses(markId) {
      let workId = this.formSearch.markId;
      if (markId) {
        workId = markId;
      }
      this.$http
        .get(MARKING_API + "/markResults/queryExamCourseList?workId=" + workId)
        .then(response => {
          this.courseList = response.data;
        });
    },
    //查询评委
    getUserList() {
      this.formSearch.userId = "";
      this.$http
        .get(
          DATA_PROCESS_API +
            "/markUsers/assign?workId=" +
            this.workId +
            "&courseCode=" +
            this.formSearch.courseCode
        )
        .then(response => {
          this.userList = response.data;
        });
    },
    //查询标记卷
    getTags() {
      this.$http.get(MARKING_API + "/markResults/tag").then(response => {
        console.log("取到的标记卷：", response);
        this.tags = response.data;
        //移除科目错误
        for (let i = 0; i < this.tags.length; i++) {
          if (this.tags[i].code === "SUBJECT_ERROR") {
            this.tags.splice(i, 1);
          }
        }
      });
    },
    //查询专业
    getSpecialtys() {
      var orgId = this.user.rootOrgId;
      this.$http
        .get(CORE_API + "/specialty/all?orgId=" + orgId)
        .then(response => {
          this.specialtyList = response.data;
        });
    },
    //查询方法
    searchMarkPaperCheck() {
      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.formSearch.workId = this.formSearch.markId;
      this.examType = obj.examType;
      this.formSearch.examType = this.examType;
      this.loading = true;
      var url =
        DATA_PROCESS_API +
        "/markResults/all/" +
        (this.currentPage - 1) +
        "/" +
        this.pageSize;
      this.$http
        .get(url, { params: this.formSearch })
        .then(response => {
          console.log("查询的列表集合", response);
          this.tableData = response.data.list;
          this.total = response.data.total;
          this.loading = false;
          this.$router.push({
            path:
              "/marking/mark_paper_check?" +
              new URLSearchParams(JSON.parse(JSON.stringify(this.formSearch)))
          });
        })
        .catch(function(response) {
          console.log(response);
        });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.searchMarkPaperCheck();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.searchMarkPaperCheck();
    },
    //查看原卷
    viewPaper(row) {
      if (this.examType != "OFFLINE") {
        var studentPaperId = row.studentPaper.id;
        console.log("studentPaperId:", studentPaperId);
        var urls =
          "/marking/view_paper/" +
          this.formSearch.markId +
          "/" +
          this.examId +
          "/" +
          studentPaperId +
          "/" +
          this.examType;
        this.$router.push({
          path: urls
        });
      } else {
        var url = row.studentSubjectiveHtml;
        window.open(url);
      }
    },
    //打回
    backRefresh(row) {
      var userId = this.user.userId;
      var url =
        MARKING_API + "/markResults/" + row.id + "/reject?creatorId=" + userId;
      this.$http.post(url).then(response => {
        console.log(response);
        this.searchMarkPaperCheck();
      });
    },
    //字符串截取
    interceptStr(str) {
      if (str) {
        let l = str.length;
        if (l > 3) {
          return str.substring(0, 3) + "...";
        } else {
          return str;
        }
      }
    },
    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.getCourses();
        this.searchMarkPaperCheck();
      }
    }
  },
  created() {
    //查询标记卷
    this.getMarkWorks();
    this.getTags();
    this.backFill();
  }
};
</script>
