<template>
    <div class="testStat">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="68px">

        <el-form-item label="考试名称" prop="courseName">
          <el-input
            v-model="queryParams.courseName"
            placeholder="请输入课程名称"
            clearable
            size="small"
          />
        </el-form-item>

        <el-form-item label="类型" prop="dataKind">
          <el-select v-model="queryParams.dataKind">
            <el-option label="培训考试" value="0" />
            <el-option label="随机考试" value="1" />
          </el-select>
        </el-form-item>

        <el-form-item label="发布时间" prop="releaseTimeBegin">
          <el-date-picker
            v-model="releaseTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="dateFormat">
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="search"
          >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetClick"
          >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="testStatData">
        <el-table-column label="序号" width='100' align="center" prop="courseNum"/>
        <el-table-column label="考试名称" align="center" prop="courseName"/>
        <el-table-column label="发布时间" align="center" prop="releaseTime"/>
        <el-table-column label="类型" align="center" prop="dataKind">
          <template slot-scope="scope">
            <span v-if="scope.row.dataKind == '0'">培训考试</span>
            <span v-else>随机考试</span>
          </template>
        </el-table-column>
        <el-table-column label="应参加人数" align="center" prop="count"/>
        <el-table-column label="实际考试人数" align="center" prop="test"/>
        <el-table-column label="合格人数" align="center" prop="pass"/>
        <el-table-column label="合格率" align="center" prop="rate">
          <template slot-scope="scope">
            {{scope.row.rate}}%
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template v-slot="{ row: { courseId } }">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="courseDetail(courseId)"
            >查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getTestList"
      />

      <!--考试详情 -->
      <el-dialog title="考试详情" :visible.sync="testStatDetailOpen" append-to-body :close-on-click-modal="false">
        <el-table v-loading="loading" :data="testStatDetailData">
          <el-table-column label="序号" width='100' align="center" prop="detailNum"/>
          <el-table-column label="考试人员" align="center" prop="staffName"/>
          <el-table-column label="所属部门" align="center" prop="deptName"/>
          <el-table-column label="答对个数" align="center" prop="examinationResult"/>
          <el-table-column label="考试结果" align="center" prop="state">
            <template slot-scope="scope">
              <span v-if="scope.row.state == 0">未考试</span>
              <span v-if="scope.row.state == 1">不合格</span>
              <span v-if="scope.row.state == 2">合格</span>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="totalDetail > 0"
          :total="totalDetail"
          :page.sync="queryDetailParams.pageNum"
          :limit.sync="queryDetailParams.pageSize"
          @pagination="getTestStatDetails"
        />
      </el-dialog>
    </div>
</template>

<script>
  import {statisticsTrainCourse,testPersonDetailByCourseId} from "@/api/educationPlanExam/lessonsProgram.js";
  export default {
    name: "testStat",
    data(){
        return{
          // 遮罩层
          loading: false,
          // 总条数
          total: 0,
          releaseTime: "",
          queryParams:{
            pageNum: 1,
            pageSize: 10,
            courseName: "",
            dataKind: "",
            releaseTimeBegin: "",
            releaseTimeEnd: ""
          },
          testStatData:[],
          testStatDetailOpen: false,
          testStatDetailData: [],
          totalDetail: 0,
          queryDetailParams:{
            pageNum: 1,
            pageSize: 10,
            courseId: ""
          }
        }
    },
    created(){
      this.getTestList();
    },
    methods: {
      getTestList(){
        console.log("this.queryParams:",this.queryParams)
        statisticsTrainCourse(this.queryParams).then(res=>{
          this.testStatData = res.rows.map((item, index) => {
            return {
              courseNum:
                index +
                1 +
                (this.queryParams.pageNum - 1) * this.queryParams.pageSize,
                ...item,
            };
          });
          this.total = res.total;
        }) .finally(() => {
            this.loading = false;
        });
      },
      dateFormat(picker){
        this.queryParams.releaseTimeBegin = picker[0];
        this.queryParams.releaseTimeEnd = picker[1];
      },
      search(){
        this.getTestList();
      },
      resetClick(){
        this.releaseTime = "";
        this.queryParams={
            pageNum: 1,
            pageSize: 10,
            courseName: "",
            dataKind: "",
            releaseTimeBegin: "",
            releaseTimeEnd: ""
        }
      },
      courseDetail(courseId){
        this.testStatDetailOpen = true;
        this.getTestStatDetails(courseId);
      },
      getTestStatDetails(courseId){
        this.queryDetailParams.courseId = courseId;
        testPersonDetailByCourseId(this.queryDetailParams).then(res =>{
          this.testStatDetailData = res.rows.map((item, index) => {
            return {
              detailNum:
              index +
              1 +
              (this.queryParams.pageNum - 1) * this.queryParams.pageSize,
              ...item,
          };
        });
          this.totalDetail = res.total;
        }) .finally(() => {
            this.loading = false;
        });
      }
    }
  }
</script>

<style scoped lang="scss">
  .testStat{
    margin: 10px;
  }
</style>
