<template>
  <div class="owner-course-exam-overview">
    <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.有效人数：成绩有效的实考人数。</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"
            :semester-id="filter.semesterId"
            select-default
            :clearable="false"
            @change="examChange"
          ></exam-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" round @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="school-exam-overview">
      <div class="exam-summary">
        <div class="summary-item summary-time">
          <div class="summary-tag">
            <div class="tag-body">
              <i class="tag-icon"><i class="icon icon-start"></i></i>
            </div>
            <div class="tag-body">
              <p>考试开始时间</p>
              <p>{{ summary.examTime | timestampFilter }}</p>
            </div>
          </div>
        </div>
        <div class="summary-item summary-college">
          <div class="summary-tag">
            <div class="tag-body">
              <i class="tag-icon"><i class="icon icon-college"></i></i>
            </div>
            <div class="tag-body">
              <p>考查学院</p>
              <p>{{ summary.collegeCount }}</p>
            </div>
          </div>
        </div>
        <div class="summary-item summary-grid">
          <div class="summary-tag">
            <div class="tag-body">
              <i class="tag-icon"><i class="icon icon-course"></i></i>
            </div>
            <div class="tag-body">
              <p>课程数量</p>
              <p>{{ summary.courseCount }}</p>
            </div>
          </div>
          <div class="summary-tag">
            <div class="tag-body">
              <p>公共课</p>
              <p>{{ summary.publicCourseCount }}</p>
            </div>
            <div class="tag-body">
              <p>专业课</p>
              <p>{{ summary.majorCourseCount }}</p>
            </div>
          </div>
        </div>
        <div class="summary-item summary-grid summary-reality-course">
          <div class="summary-tag">
            <div class="tag-body">
              <i class="tag-icon"><i class="icon icon-book"></i></i>
            </div>
            <div class="tag-body">
              <p>应考课次</p>
              <p>{{ summary.totalCourseTimes }}</p>
            </div>
          </div>
          <div class="summary-tag">
            <div class="tag-body">
              <p>有效(实考)课次</p>
              <p>{{ summary.realityCourseTimes }}</p>
            </div>
            <div class="tag-body">
              <p>缺考/违纪课次</p>
              <p>{{ summary.absentCourseTimes }}</p>
            </div>
          </div>
        </div>
        <div class="summary-item summary-grid summary-reality-person">
          <div class="summary-tag">
            <div class="tag-body">
              <i class="tag-icon"><i class="icon icon-users"></i></i>
            </div>
            <div class="tag-body">
              <p>应考人数</p>
              <p>{{ summary.totalCount }}</p>
            </div>
          </div>
          <div class="summary-tag">
            <div class="tag-body">
              <p>有效(实考)人数</p>
              <p>{{ summary.realityCount }}</p>
            </div>
            <div class="tag-body">
              <p>缺考/违纪人数</p>
              <p>{{ summary.absentCount }}</p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="part-box part-box-pad">
      <div class="part-box-head">
        <h3>
          <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>点击课程名称，可以下钻查看该课程的详细分析。</p>
            </div>
          </el-popover>
        </h3>
        <el-button type="text" :disabled="loading" @click="toExport">
          <i class="el-icon-download"></i> 导出EXCEL报表
        </el-button>
      </div>
      <el-table
        ref="TableList"
        :data="dataList"
        border
        @sort-change="sortChange"
      >
        <el-table-column prop="courseCode" label="课程(代码)" min-width="120">
          <span
            class="text-link"
            slot-scope="scope"
            @click="toDetail(scope.row)"
          >
            {{ scope.row.courseName }}（{{ scope.row.courseCode }}）
          </span>
        </el-table-column>
        <el-table-column
          prop="teachCollegeName"
          label="开课学院"
          min-width="80"
        ></el-table-column>
        <el-table-column prop="inspectCollegeCount" width="70">
          <span class="cell-head" slot="header">考查<br />学院</span>
        </el-table-column>
        <el-table-column prop="realityCount" width="70">
          <span class="cell-head" slot="header">有效<br />人数</span>
        </el-table-column>
        <el-table-column prop="currentRealityRate" width="90">
          <span class="cell-head" slot="header">应届占比<br />(人数)</span>
          <span slot-scope="scope">
            {{ scope.row.currentRealityRate }}% <br /><i class="color-gray"
              >({{ scope.row.currentRealityCount }})</i
            >
          </span>
        </el-table-column>
        <el-table-column prop="pastRealityRate" width="100">
          <span class="cell-head" slot="header">非应届占比<br />(人数)</span>
          <span slot-scope="scope">
            {{ scope.row.pastRealityRate }}%
            <i class="color-gray">({{ scope.row.pastRealityCount }})</i>
          </span>
        </el-table-column>
        <el-table-column
          prop="absentCount"
          label="缺考"
          width="60"
        ></el-table-column>
        <el-table-column label="赋分前" align="center">
          <el-table-column prop="avgScoreBefore" width="80">
            <span class="cell-head" slot="header">总体<br />平均分</span>
          </el-table-column>
          <el-table-column prop="notPassRateBefore" width="80">
            <span class="cell-head" slot="header">总体不<br />及格率</span>
            <span slot-scope="scope">
              {{ scope.row.notPassRateBefore }}% <br /><i class="color-gray"
                >({{ scope.row.notPassCountBefore }})</i
              >
            </span>
          </el-table-column>
          <el-table-column
            prop="currentAvgScoreBefore"
            sortable="custom"
            width="100"
          >
            <span class="cell-head" slot="header">应届<br />平均分</span>
          </el-table-column>
          <el-table-column
            prop="currentNotPassRateBefore"
            sortable="custom"
            width="100"
          >
            <span class="cell-head" slot="header">应届不<br />及格率</span>
            <span slot-scope="scope">
              {{ scope.row.currentNotPassRateBefore }}% <br /><i
                class="color-gray"
                >({{ scope.row.currentNotPassCountBefore }})</i
              >
            </span>
          </el-table-column>
        </el-table-column>
        <el-table-column prop="coefficient" width="60" align="center">
          <span class="cell-head" slot="header">赋分<br />系数</span>
        </el-table-column>
        <el-table-column label="赋分后" align="center">
          <el-table-column prop="avgScoreAfter" width="80">
            <span class="cell-head" slot="header">总体<br />平均分</span>
          </el-table-column>
          <el-table-column prop="notPassRateAfter" width="80">
            <span class="cell-head" slot="header">总体不<br />及格率</span>
            <span slot-scope="scope">
              {{ scope.row.notPassRateAfter }}% <br /><i class="color-gray"
                >({{ scope.row.notPassCountAfter }})</i
              >
            </span>
          </el-table-column>
          <el-table-column
            prop="currentAvgScoreAfter"
            sortable="custom"
            width="100"
          >
            <span class="cell-head" slot="header">应届<br />平均分</span>
          </el-table-column>
          <el-table-column
            prop="currentNotPassRateAfter"
            sortable="custom"
            width="100"
          >
            <span class="cell-head" slot="header">应届不<br />及格率</span>
            <span slot-scope="scope">
              {{ scope.row.currentNotPassRateAfter }}% <br /><i
                class="color-gray"
                >({{ scope.row.currentNotPassCountAfter }})</i
              >
            </span>
          </el-table-column>
        </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 {
  schoolExamOverview,
  courseSurveyTeacherList,
  courseSurveyTeacherExport
} from "../api";
import { downloadByApi } from "@/plugins/download";

export default {
  name: "owner-course-exam-overview",
  data() {
    return {
      isInit: false,
      filter: {
        semesterId: "",
        examId: "",
        column: null,
        order: null
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      summary: {},
      dataList: [],
      orgName: "",
      loading: false
    };
  },
  created() {
    const orgInfo = this.$ls.get("user", { orgInfo: { name: "" } }).orgInfo;
    if (orgInfo) this.orgName = orgInfo.name;
  },
  methods: {
    search() {
      this.getSummary();
      this.toPage(1);
    },
    async getSummary() {
      const data = await schoolExamOverview(this.filter);
      this.summary = data || {};
    },
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size
      };
      const data = await courseSurveyTeacherList(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    semesterChange() {
      this.filter.examId = null;
    },
    examChange() {
      // 初次载入页面加载默认数据
      if (!this.isInit && this.filter.examId && this.filter.semesterId) {
        this.isInit = true;
        this.search();
      }
    },
    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();
    },
    toDetail(row) {
      this.$ls.set("ownerCourseSet", {
        semesterId: this.filter.semesterId,
        examId: this.filter.examId,
        courseCode: row.courseCode
      });
      this.$router.push({ name: "OwnerCourseExamAnalysis" });
    },
    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 courseSurveyTeacherExport(this.filter);
      }, `开课课程考试分析.xlsx`).catch(() => {});

      this.loading = false;

      if (res) {
        this.$message.success("文件下载成功！");
      } else {
        this.$message.error("文件下载失败，请重新尝试！");
      }
    }
  }
};
</script>
