<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-group">
        <div class="filter-lable">关键字：</div>
        <el-input v-model="KEYWORDS" placeholder="请输入学习任务名称" style="width: 200px;"/>
      </div>
      <!--      <div class="filter-group">-->
      <!--        <div class="filter-lable">培训行业类型：</div>-->
      <!--        <SelectTree ref="keyHyRef" :options="dicListMap.trainDicList" :props="defaultProps" v-model="TRAINTYPE" placeholder="请选择培训行业类型" />-->
      <!--      </div>-->
      <div class="filter-group">
        <div class="filter-lable">岗位培训类型：</div>
        <SelectTree ref="keyGwRef" :options="dicListMap.postDicList" :props="defaultProps" v-model="POSTTYPE" placeholder="请选择岗位培训类型" />
      </div>
      <div class="filter-group">
        <div class="filter-lable">任务状态：</div>
        <el-select v-model="STATE" placeholder="请选择">
          <el-option v-for="item in dicListMap.vidStateList" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </div>
      <el-button v-waves class="ml-10" type="primary" icon="el-icon-search" @click="getQuery">
        搜索
      </el-button>
      <el-button v-waves 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="STUDY_NAME" label="学习任务名称" />
      <el-table-column prop="train_type_name" label="培训行业类型" />
      <el-table-column prop="post_type_name" label="岗位培训类型" />
      <el-table-column prop="PEIXUE_START_TIME" label="培训开始时间" align="center"/>
      <el-table-column prop="PEIXUE_END_TIME" label="培训结束时间" align="center"/>
      <el-table-column prop="SUM_CLASSHOUR" label="任务课时" align="center" width="100"/>
      <el-table-column prop="userCount" label="应参考人数" align="center" width="100"/>
      <el-table-column prop="CURRICULUMNAME" label="参考人数/合格人数" align="center">
        <template slot-scope="{row}">
          <div>{{ row.cj }}/{{ row.hg }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="STATE" label="任务状态" align="center" width="100">
        <template slot-scope="{row}">
          <template v-if="resero(row)=='1'"><div>进行中</div></template>
          <template v-else-if="resero(row)=='2'"><div>已结束</div></template>
          <template v-else-if="resero(row)=='3'"><div>未开始</div></template>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="440">
        <template slot-scope="{row}">
          <el-button v-show="edit" class="light-blue-btn" icon="el-icon-view" size="mini" @click="handleEdit(row.STUDYTASK_ID)">查看</el-button>
          <!--          <el-button class="light-blue-btn" icon="el-icon-view" size="mini" @click="handleEdit(row.STUDYTASK_ID)">编辑</el-button>-->
          <el-button v-show="edit" type="primary" icon="el-icon-edit" size="mini" @click="goEdit(row.STUDYTASK_ID)">编辑</el-button>
          <el-button v-show="edit" class="blue-btn" icon="el-icon-user" size="mini" @click="handleGouserInfo(row)">学员详情</el-button>
          <el-button v-show="edit" type="primary" icon="el-icon-medal" size="mini" @click="handleKaoshi(row)">考试详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div>
        <el-button v-show="add" type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
      </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
import SelectTree from '@/components/SelectTree'
import dateformat from '@/utils/dateformat'
export default {
  components: { Pagination, SelectTree },
  directives: { waves },
  data() {
    return {
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 10
      },
      defaultProps: {
        value: 'id',
        children: 'nodes',
        label: 'name'
      },
      total: 0,
      config: config,
      KEYWORDS: '',
      LEARNERCATEGORY: '',
      STARTTIMES: [],
      ENDTIMES: [],
      STATE: '',
      TRAINTYPE: '',
      POSTTYPE: '',
      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' }]
      },
      dicListMap: {
        outlineDicList: [],
        trainDicList: [],
        vidStateList: [
          { value: '1', label: '未开始' },
          { value: '2', label: '进行中' },
          { value: '3', label: '已结束' }
        ]
      }
    }
  },
  watch: {
    'form.CURRICULUMUSERS': {
      handler() {
        this.getDict()
      }
    }
  },
  created() {
    this.getList(this.ROLE_ID)
    this.getDict()
    this.getDicListByPID('052369aa22d242118236cde52d0c67ea', 'trainDicList')
    this.getDicListByPID('f6a7c4f5602f46e291d06b1390a3f820', 'postDicList')
  },
  methods: {
    resero(row) {
      if (row.PEIXUE_START_TIME > dateformat(new Date(), 'YYYY-MM-DD')) {
        return '3'
      } else if (row.PEIXUE_START_TIME <= dateformat(new Date(), 'YYYY-MM-DD') && row.PEIXUE_END_TIME >= dateformat(new Date(), 'YYYY-MM-DD')) {
        return '1'
      } else if (row.PEIXUE_END_TIME <= dateformat(new Date(), 'YYYY-MM-DD')) {
        return '2'
      }
    },
    getRowKey(row) {
      return row.CURRICULUM_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    goKeyReset() {
      this.KEYWORDS = ''
      this.LEARNERCATEGORY = ''
      this.STARTTIMES = []
      this.ENDTIMES = []
      this.TRAINTYPE = ''
      this.POSTTYPE = ''
      this.STATE = ''
      this.$refs.keyGwRef.clearHandle()
      // TRAINTYPE
      // POSTTYPE
      // STATE
      this.getList()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/studytask/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS,
          STATE: this.STATE,
          TRAINTYPE: this.TRAINTYPE,
          POSTTYPE: this.POSTTYPE
        }
      ).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
      })
    },
    handleStudent(CURRICULUM_ID) {
      this.$parent.CURRICULUM_ID = CURRICULUM_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) => {

      })
    },
    handleAdd() {
      this.$parent.activeName = 'CourseAdd'
    },
    getDicListByPID(pId, listName) {
      requestFN(
        '/dictionaries/listAllDictToParId',
        {
          parentId: pId
        }
      ).then((data) => {
        this.dicListMap[listName] = JSON.parse(data.zTreeNodes)
      }).catch((e) => {

      })
    },
    handleEdit(id) {
      this.$parent.activeName = 'CouresInfo'
      this.$parent.STUDYTASK_ID = id
    },
    goEdit(id) {
      this.$parent.activeName = 'CourseEdit'
      this.$parent.STUDYTASK_ID = id
    },
    handleGouserInfo(row) {
      this.$parent.activeName = 'CouresUserInfo'
      this.$parent.STUDYTASK_ID = row.STUDYTASK_ID
    },
    handleKaoshi(row) {
      this.$parent.activeName = 'CourseKaoshiInfo'
      console.info(row)
      this.$parent.STUDYTASK_ID = row.STUDYTASK_ID
      this.$parent.STAGEEXAMPAPER_ID = row.STAGEEXAMPAPER_ID
      // this.$parent.STUDYTASK_ID = id
    }
  }
}

</script>
