<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="workId">
              <el-select
                clearable
                filterable
                class="input"
                v-model="formSearch.workId"
                placeholder="请选择"
                style="width:150px;"
                @change="changeMarkWork"
                @clear="resetForm"
              >
                <el-option
                  v-for="item in markWorkSelect"
                  :label="item.markName"
                  :value="item.workId"
                  :key="item.workId"
                ></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
                  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
                  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
                  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
                  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>
            <el-form-item label="试卷ID" prop="examRecordDataId">
              <el-input
                placeholder="请输入试卷ID"
                v-model="formSearch.examRecordDataId"
                style="width:150px;"
              ></el-input>
            </el-form-item>
            <el-form-item label="身份证号" prop="identityNumber">
              <el-input
                placeholder="请输入身份证号"
                v-model="formSearch.identityNumber"
                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()"
                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="试卷ID"
              width="150"
              prop="examRecordDataId"
            />
            <el-table-column
              label="身份证号"
              width="150"
              prop="identityNumber"
            />
            <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" class="interceptStr">
                    <span>{{ 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
              v-if="paginationShow"
              @current-change="handleCurrentChange"
              @size-change="handleSizeChange"
              :current-page="currentPage"
              :page-size="pageSize"
              :page-sizes="[10, 20, 50, 100, 200, 300]"
              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: "",
        examType: "",
        markRemark: "",
        examRecordDataId: "",
        identityNumber: ""
      },
      orgList: [],
      courseList: [],
      specialtyList: [],
      userList: [],
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      paginationShow: false,
      examId: "",
      tags: [],
      loading: false,
      examType: "",
      getOrgsSearchLoading: false,
      markWorkList: []
    };
  },
  computed: {
    ...mapState({ user: state => state.user }),
    markWorkSelect() {
      let markWorkNames = [];
      for (let markWork of this.markWorkList) {
        markWorkNames.push({
          workId: 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: {
    changeMarkWork() {
      if (this.$route.query) {
        this.$router.push({ path: "/marking/mark_paper_check" });
      }
      let obj = {};
      for (let item of this.markWorkList) {
        if (item.id === this.formSearch.workId) {
          obj = item;
          break;
        }
      }
      this.examId = obj.examId;
      this.examType = obj.examType;
      this.getCourses();
      this.formSearch = Object.assign(this.formSearch, {
        orgCode: "",
        studentName: "",
        studentCode: "",
        tag: "",
        courseCode: "",
        userId: "",
        examType: "",
        markRemark: "",
        examRecordDataId: "",
        identityNumber: ""
      });
    },
    getMarkWorks() {
      this.$http
        .get(DATA_PROCESS_API + "/markWorks?status=1")
        .then(response => {
          this.markWorkList = response.data;
          if (this.$route.query && this.$route.query.workId) {
            this.formSearch.workId = parseInt(this.$route.query.workId);
          }
        });
    },
    //查询学习中心
    getOrgs(name) {
      this.getOrgsSearchLoading = true;
      this.$http.get(CORE_API + "/org/query?name=" + name).then(response => {
        this.getOrgsSearchLoading = false;
        this.orgList = response.data;
      });
    },
    //查询课程
    getCourses() {
      this.$http
        .get(
          MARKING_API +
            "/markResults/queryExamCourseList?workId=" +
            this.formSearch.workId
        )
        .then(response => {
          this.courseList = response.data;
          if (this.$route.query && this.$route.query.courseCode) {
            this.formSearch.courseCode = this.$route.query.courseCode;
            this.getUserList();
          }
        });
    },
    //查询评委
    getUserList() {
      this.$http
        .get(
          DATA_PROCESS_API +
            "/markUsers/assign?workId=" +
            this.formSearch.workId +
            "&courseCode=" +
            this.formSearch.courseCode
        )
        .then(response => {
          this.userList = response.data;
          if (this.$route.query && this.$route.query.userId) {
            this.formSearch.userId = parseInt(this.$route.query.userId);
          }
        });
    },
    //查询标记卷
    getTags() {
      this.$http.get(MARKING_API + "/markResults/tag").then(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() {
      this.currentPage = 1;
      this.initMarkPaperCheck();
    },
    initMarkPaperCheck() {
      if (!this.formSearch.workId) {
        this.$notify({
          title: "警告",
          message: "请选择评卷名称",
          type: "warning"
        });
        return false;
      }
      this.loading = true;
      var url =
        DATA_PROCESS_API +
        "/markResults/all/" +
        (this.currentPage - 1) +
        "/" +
        this.pageSize;
      this.formSearch.examType = this.examType;
      this.$http.get(url, { params: this.formSearch }).then(response => {
        this.tableData = response.data.list;
        this.total = response.data.total;
        this.loading = false;
        this.$router.push({
          path: "/marking/mark_paper_check",
          query: {
            workId: this.formSearch.workId,
            orgCode: this.formSearch.orgCode,
            studentName: this.formSearch.studentName,
            studentCode: this.formSearch.studentCode,
            tag: this.formSearch.tag,
            courseCode: this.formSearch.courseCode,
            userId: this.formSearch.userId,
            markRemark: this.formSearch.markRemark,
            examId: this.examId,
            examType: this.examType,
            currentPage: this.currentPage,
            pageSize: this.pageSize,
            examRecordDataId: this.formSearch.examRecordDataId,
            identityNumber: this.formSearch.identityNumber
          }
        });
        this.$nextTick(function() {
          this.paginationShow = true;
        });
      });
    },
    resetForm() {
      this.formSearch = Object.assign(this.formSearch, {
        orgCode: "",
        studentName: "",
        studentCode: "",
        tag: "",
        courseCode: "",
        userId: "",
        workId: "",
        markRemark: "",
        examRecordDataId: "",
        identityNumber: ""
      });
      this.tableData = [];
      this.total = 0;
      this.$router.push({ path: "/marking/mark_paper_check" });
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.initMarkPaperCheck();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.initMarkPaperCheck();
    },
    //查看原卷
    viewPaper(row) {
      if (this.examType != "OFFLINE") {
        var studentPaperId = row.studentPaper.id;
        var urls =
          "/marking/view_paper/" +
          this.formSearch.workId +
          "/" +
          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(() => {
        this.searchMarkPaperCheck();
      });
    },
    operaQuery() {
      let formData = this.$route.query;
      if (formData && formData.workId) {
        this.formSearch.workId = formData.workId;
        this.formSearch.orgCode = formData.orgCode;
        this.formSearch.studentName = formData.studentName;
        this.formSearch.studentCode = formData.studentCode;
        this.formSearch.markRemark = formData.markRemark;
        this.formSearch.courseCode = formData.courseCode;
        this.formSearch.userId = formData.userId;
        this.formSearch.tag = formData.tag;
        this.formSearch.examRecordDataId = formData.examRecordDataId;
        this.formSearch.identityNumber = formData.identityNumber;
        this.examId = formData.examId;
        this.examType = formData.examType;
        this.currentPage = parseInt(formData.currentPage);
        this.pageSize = parseInt(formData.pageSize);
        this.getCourses();
        this.initMarkPaperCheck();
      }
    }
  },
  created() {
    //查询标记卷
    this.getTags();
    this.getMarkWorks();
    this.operaQuery();
  }
};
</script>
<style lang="css" scoped>
.interceptStr {
  text-overflow: ellipsis;
  overflow:hidden;white-space:nowrap;
}
</style>
