<template>
  <div>

    <div class="app-container">
      <div class="filter-container">
        <el-input v-model="KEYWORDS" placeholder="搜索学员姓名" class="filter-item" style="width: 200px;" />
        <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="USERNAME" label="学员姓名" align="center" />
        <el-table-column prop="STUDYCOUNT" label="学习次数" align="center" />
        <el-table-column prop="COURSEEXAMCOUNT" label="课程考试次数" align="center" />
        <el-table-column prop="COURSEEXAMSCORE" label="课程考试成绩" align="center">
          <template slot-scope="{row}">
            <a v-if="row.COURSEEXAM_ID != null && row.COURSEEXAMSTATE !== '1'" @click="handleCourseExam(row.COURSEEXAM_ID)">
              <el-tag size="medium">{{ row.COURSEEXAMSCORE }}</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>

      <el-dialog v-if="dialogCourseExamShow" :visible.sync="dialogCourseExamShow" title="课考试卷" width="90%">
        <CourseExamInfo :examid="COURSEEXAM_ID" />
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogCourseExamShow = false">关 闭</el-button>
        </div>
      </el-dialog>

      <el-dialog v-if="dialogStageExamShow" :visible.sync="dialogStageExamShow" title="阶考试卷" width="90%">
        <StageExamInfo :examid="STAGEEXAM_ID" />
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogStageExamShow = false">关 闭</el-button>
        </div>
      </el-dialog>

      <div class="ui-height" />
    </div>

    <div class="ui-foot">
      <el-button plain type="info" @click="goBack">返 回</el-button>
    </div>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves' // waves directive
import CourseExamInfo from '@/views/onlinexxks/course/exam/components/info'
import StageExamInfo from '@/views/onlinexxks/stage/exam/components/info'
export default {
  components: { Pagination, CourseExamInfo, StageExamInfo },
  directives: { waves },
  data() {
    return {
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      config: config,
      KEYWORDS: '',
      varList: [],
      pd: [],
      form: {
        CURRICULUMNAME: '', // 课程名称
        CURRICULUMINTRODUCE: '', // 课程介绍
        CURRICULUMAIM: '', // 课程目的
        STARTTIME: '', // 开始时间
        ENDTIME: '', // 结束时间
        CURRICULUMUSERS: '', // 适用人员
        VIDEOCOURSEWARES: '', // 视频课件组
        DATACOURSEWARES: ''// 资料课件组
      },
      multipleSelectionAll: [], // 所有选中的数据包含跨页数据
      multipleSelection: [], // 当前页选中的数据
      LearnerCategoryList: [],
      dialogCourseExamShow: false,
      COURSEEXAM_ID: '',
      dialogStageExamShow: false,
      STAGEEXAM_ID: '',
      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()
      }
    }
  },
  created() {
    this.getList()
    this.getDict()
  },
  methods: {
    getRowKey(row) {
      return row.CURRICULUM_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    goKeyReset() {
      this.KEYWORDS = ''
      this.getList()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/coursestudentrelation/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS,
          CURRICULUM_ID: this.$parent.CURRICULUM_ID
        }
      ).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)
    },
    // 查看课程试卷答题结果
    handleCourseExam(COURSEEXAM_ID) {
      // this.COURSEEXAM_ID = COURSEEXAM_ID
      // this.dialogCourseExamShow = true
      this.$parent.COURSEEXAM_ID = COURSEEXAM_ID
      this.$parent.activeName = 'CourseExamResult'
    },
    // 判断按钮权限，用于是否显示按钮
    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) => {

      })
    },
    goBack() {
      this.$parent.activeName = 'Course'
    }
  }
}

</script>
