<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-group">
        <div class="filter-lable">试卷名称：</div>
        <el-input v-model="KEYWORDS" placeholder="请输入阶段试卷名称" clearable class="filter-item" style="width: 300px;" @clear="getQuery"/>
      </div>
      <div class="filter-group">
        <div class="filter-lable">考试时间：</div>
        <el-date-picker
          v-model="EXAMTIMES"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="filter-item"
          clearable
          @clear="getQuery"/>
      </div>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getQuery">
        搜索
      </el-button>
      <el-button v-waves class="filter-item" type="success" icon="el-icon-refresh" @click="goKeyReset">
        重置
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      ref="multipleTable"
      :data="varList"
      :row-key="getRowKey"
      :header-cell-style="{
        'font-weight': 'bold',
        'color': '#000'
      }"
      tooltip-effect="dark"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        :reserve-selection="true"
        type="selection"
        width="55"
        align="center"/>
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="EXAMNAME" label="阶段试卷" />
      <el-table-column prop="EXAMTIME" label="考试时间" align="center" />
      <el-table-column prop="ANSWERSHEETTIME" label="答卷时间" align="center" />
      <el-table-column prop="QUESTIONNUM" label="试题数量" align="center" />
      <el-table-column label="完成考试人数/参与考试人数" align="center" >
        <template slot-scope="{row}">
          <a @click="handleStudent(row.STAGEEXAMPAPER_ID)">
            <el-tag size="medium">{{ row.COMPLETENUM }}/{{ row.PARTICIPANTNUM }}</el-tag>
          </a>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div/>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves' // waves directive
export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      config: config,
      KEYWORDS: '',
      EXAMTIMES: ['', ''],
      varList: [],
      pd: [],
      form: {
        CURRICULUMNAME: '', // 课程名称
        CURRICULUMINTRODUCE: '', // 课程介绍
        CURRICULUMAIM: '', // 课程目的
        STARTTIME: '', // 开始时间
        ENDTIME: '', // 结束时间
        CURRICULUMUSERS: '', // 适用人员
        VIDEOCOURSEWARES: '', // 视频课件组
        DATACOURSEWARES: ''// 资料课件组
      },
      multipleSelectionAll: [], // 所有选中的数据包含跨页数据
      multipleSelection: [], // 当前页选中的数据
      LearnerCategoryList: [],
      rules: {
        CURRICULUMNAME: [{ required: true, message: '课程名称不能为空', trigger: 'blur' }],
        CURRICULUMINTRODUCE: [{ required: true, message: '课程介绍不能为空', trigger: 'blur' }],
        CURRICULUMAIM: [{ required: true, message: '课程目的不能为空', trigger: 'blur' }],
        STARTTIME: [{ required: true, message: '开始时间不能为空', trigger: 'blur' }],
        ENDTIME: [{ required: true, message: '结束时间不能为空', trigger: 'blur' }],
        CURRICULUMUSERS: [{ required: true, message: '适用人员不能为空', trigger: 'blur' }]
      }
    }
  },
  watch: {
    'form.CURRICULUMUSERS': {
      handler() {
        this.getDict()
      }
    },
    EXAMTIMES: function(newData, oldData) {
      if (!newData) {
        this.EXAMTIMES = ['', '']
        this.getQuery()
      }
    }
  },
  created() {
    this.getList(this.ROLE_ID)
    this.getDict()
  },
  methods: {
    getRowKey(row) {
      return row.STAGEEXAMPAPER_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    goKeyReset() {
      this.KEYWORDS = ''
      this.EXAMTIMES = ['', '']
      this.getList()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/stageexampaper/listSummary?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS,
          STARTTIME: this.EXAMTIMES[0],
          ENDTIME: this.EXAMTIMES[1]
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
        this.pd = data.pd
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 格式化适用人员名称
    formatterUsers(row, column) {
      var CURRICULUMUSERS_VAL = ''
      const CURRICULUMUSERS = row.CURRICULUMUSERS.split(',')
      for (let i = 0; i < CURRICULUMUSERS.length; i++) {
        for (let j = 0; j < this.LearnerCategoryList.length; j++) {
          const option = this.LearnerCategoryList[j]
          if (option.BIANMA === CURRICULUMUSERS[i]) {
            CURRICULUMUSERS_VAL += option.NAME + ','
          }
        }
      }
      return CURRICULUMUSERS_VAL.slice(0, CURRICULUMUSERS_VAL.length - 1)
    },
    handleStudent(STAGEEXAMPAPER_ID) {
      this.$parent.STAGEEXAMPAPER_ID = STAGEEXAMPAPER_ID
      this.$parent.activeName = 'Student'
    },
    // 判断按钮权限，用于是否显示按钮
    hasButton: function() {
      var keys = 'curriculum:add,curriculum:del,curriculum:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.curriculumfhadminadd		// 新增权限
        this.del = data.curriculumfhadmindel		// 删除权限
        this.edit = data.curriculumfhadminedit	// 修改权限
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 获取数据字典数据
    getDict: function() {
      requestFN(
        '/dictionaries/getLevels',
        {
          DICTIONARIES_ID: '532282f9c48346b8b646531909c739df'
        }
      ).then((data) => {
        this.LearnerCategoryList = data.list
      }).catch((e) => {

      })
    }
  }
}

</script>
