<template>
  <div class="inspect-college-score">
    <div class="page-head page-head-flex">
      <h2>
        <span>考查成绩查询</span>
        <el-popover
          popper-class="el-popper-dark"
          placement="right-start"
          width="300"
          trigger="hover"
        >
          <i class="el-icon-question" slot="reference"></i>
          <div>
            <p>
              1.开课学院成绩分布后，这里才能查询到相关课程的成绩明细，且“总分”为赋分后的成绩；
            </p>
            <p>
              2.可根据需要导出查询结果的EXCEL表，便于考查学院做个性化的成绩分析处理。
            </p>
          </div>
        </el-popover>
      </h2>
      <div v-if="orgName">
        <i class="icon icon-sign"></i>考查学院：{{ orgName }}
      </div>
    </div>
    <div class="part-box part-box-filter">
      <el-form ref="FilterForm" label-position="left" label-width="60px" inline>
        <el-form-item label="学期：">
          <semester-select
            v-model="filter.semesterId"
            select-default
            :clearable="false"
            @change="semesterChange"
          ></semester-select>
        </el-form-item>
        <el-form-item label="考试：">
          <exam-select
            v-model="filter.examId"
            select-default
            :clearable="false"
            :semester-id="filter.semesterId"
            @change="examChange"
          ></exam-select>
        </el-form-item>
        <el-form-item label="课程：">
          <course-select
            v-model="filter.courseCode"
            :exam-id="filter.examId"
            inspect
            @change="courseChange"
          ></course-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="filter.current"
            style="width: 142px;"
            placeholder="请选择"
            clearable
            @change="currentChange"
          >
            <el-option
              v-for="(val, key) in CURRENT_TYPE"
              :key="key"
              :value="key"
              :label="val"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="任课教师：">
          <teach-select
            v-model="filter.teacherId"
            :filter="filter"
          ></teach-select>
        </el-form-item>
        <el-form-item label="班级：">
          <class-select
            v-model="filter.clazzId"
            :filter="filter"
          ></class-select>
        </el-form-item>
        <el-form-item label="模糊查询：">
          <el-input
            v-model="filter.studentParam"
            placeholder="准考证号/学号/姓名"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" round @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="part-box part-box-pad">
      <div class="mb-2">
        <el-button type="info" :disabled="loading" @click="toExport"
          ><i class="icon icon-export-white"></i> 导出查询结果</el-button
        >
      </div>
      <el-table
        ref="TableList"
        :data="dataList"
        border
        @sort-change="sortChange"
      >
        <el-table-column
          prop="semester"
          label="学期"
          min-width="160"
        ></el-table-column>
        <el-table-column prop="examName" label="考试名称"></el-table-column>
        <el-table-column prop="courseCode" label="课程(代码)" min-width="100">
          <span slot-scope="scope">
            {{ scope.row.courseName }}（{{ scope.row.courseCode }}）
          </span>
        </el-table-column>
        <el-table-column
          prop="ticketNumber"
          label="准考证号"
          min-width="120"
        ></el-table-column>
        <el-table-column
          prop="studentCode"
          label="学号"
          min-width="120"
        ></el-table-column>
        <el-table-column prop="name" label="姓名" width="80"></el-table-column>
        <el-table-column
          prop="current"
          label="考试类型"
          width="100"
          sortable="custom"
        >
          <span slot-scope="scope">{{
            scope.row.current | studentExamTypeFilter
          }}</span>
        </el-table-column>
        <el-table-column
          prop="assignedScore"
          label="总分"
          width="80"
          sortable="custom"
        ></el-table-column>
        <el-table-column
          prop="clazzName"
          label="班级"
          min-width="100"
          sortable="custom"
        ></el-table-column>
        <el-table-column
          prop="teachCollegeName"
          label="开课学院"
          min-width="100"
          sortable="custom"
        ></el-table-column>
        <el-table-column
          prop="inspectCollegeName"
          label="考查学院"
          min-width="100"
          sortable="custom"
        ></el-table-column>
        <el-table-column
          prop="teacherName"
          label="任课老师"
          width="80"
          sortable="custom"
        ></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>
  </div>
</template>

<script>
import {
  schoolStudentInspectCollegeResult,
  schoolStudentExportInspectCollegeResult
} from "../api";
import { CURRENT_TYPE } from "../../../constants/enumerate";
import { downloadByApi } from "@/plugins/download";

export default {
  name: "inspect-college-score",
  data() {
    return {
      isInit: false,
      filter: {
        semesterId: "",
        examId: "",
        courseCode: "",
        current: null,
        teacherId: "",
        clazzId: "",
        studentParam: "",
        column: null,
        order: null,
        inspectCollegeId: this.$ls.get("orgId")
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      orgName: "",
      orgId: "",
      dataList: [],
      CURRENT_TYPE,
      loading: false
    };
  },
  created() {
    const orgInfo = this.$ls.get("user", { orgInfo: { name: "" } }).orgInfo;
    if (orgInfo) {
      this.orgId = orgInfo.id;
      this.orgName = orgInfo.name;
    }
  },
  methods: {
    search() {
      this.toPage(1);
    },
    async getList() {
      if (!this.filter.semesterId || !this.filter.examId) {
        this.$message.error("请选择学期和考试！");
        return;
      }
      let datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size
      };
      if (datas.current) datas.current = !!(datas.current * 1);
      const data = await schoolStudentInspectCollegeResult(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    semesterChange() {
      this.filter.examId = null;
      this.examChange();
    },
    examChange() {
      this.filter.teachCollegeId = null;
      this.filter.courseCode = null;
      this.filter.teacherId = null;
      this.filter.clazzId = null;

      // 初次载入页面加载默认数据
      if (!this.isInit && this.filter.examId && this.filter.semesterId) {
        this.isInit = true;
        this.search();
      }
    },
    courseChange() {
      this.filter.clazzId = null;
    },
    currentChange() {
      this.filter.teacherId = null;
    },
    sortChange({ prop, order }) {
      if (order) {
        this.filter.column = prop;
        this.filter.order = order.replace("ending", "").toUpperCase();
      } else {
        this.filter.column = null;
        this.filter.order = null;
      }
      this.getList();
    },
    async toExport() {
      if (this.loading) return;

      if (!this.filter.semesterId || !this.filter.examId) {
        this.$message.error("请选择学期和考试！");
        return;
      }

      this.loading = true;
      const res = await downloadByApi(() => {
        return schoolStudentExportInspectCollegeResult(this.filter);
      }, `考查成绩明细.xlsx`).catch(() => {});

      this.loading = false;

      if (res) {
        this.$message.success("文件下载成功！");
      } else {
        this.$message.error("文件下载失败，请重新尝试！");
      }
    },
    // import
    uplaodError(errorData) {
      this.$notify.error({ title: "错误提示", message: errorData.message });
    },
    uploadSuccess(data) {
      this.$message.success("上传成功！");
    }
  }
};
</script>
