<template>
  <div class="app-warp">
    <div class="detail-structure">
      <el-page-header content="证书管理" @back="goBack"/>
      <!--      <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>-->
      <!--      </div>-->
      <div class="detail-container">
        <div class="app-tools">
          <div class="app-function">
            <el-button v-show="add" type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
            <el-button v-show="del" type="danger" icon="el-icon-delete" plain @click="batchDel">批量删除</el-button>
          </div>
        </div>
        <el-table
          v-loading="listLoading"
          ref="multipleTable"
          :data="varList"
          :row-key="getRowKey"
          :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
          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="QUALIFICATION_TYPE_NAME" label="资质类型" align="center" />
          <el-table-column prop="VOCATIONAL_CERTIFICATE_NUMBER" label="职业证书编号" />
          <el-table-column prop="IS_PERMANENT_VALID" label="是否永久有效" align="center" >
            <template slot-scope="{row}">
              <span v-if="row.IS_PERMANENT_VALID=='1'">是</span>
              <span v-else>否</span>
            </template>
          </el-table-column>
          <el-table-column prop="CERTIFICATE_DATE_BEGIN" label="证书有效期（开始）" align="center" />
          <el-table-column prop="CERTIFICATE_DATE_END" label="证书有效期（结束）" align="center" >
            <template slot-scope="{row}">
              <span v-if="row.IS_PERMANENT_VALID=='1'">永久</span>
              <span v-else>{{ row.CERTIFICATE_DATE_END }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="180">
            <template slot-scope="{row}">
              <el-button type="text" @click="handleInfo(row.TEACHERCERTIFICATE_ID)">查看</el-button>
              <span class="app-divider"/>
              <el-button v-show="edit" type="text" @click="handleEdit(row.TEACHERCERTIFICATE_ID)">编辑</el-button>
              <span class="app-divider"/>
              <el-button v-show="del" type="text" @click="handleDelete(row.TEACHERCERTIFICATE_ID)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </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,
      KEYWORDS: '',
      varList: [],
      QUALIFICATION_TYPE_List: [], // 数据字典
      form: {
        TEACHER_ID: '', // 教师ID
        QUALIFICATION_TYPE: '', // 资质类型
        VOCATIONAL_CERTIFICATE_NUMBER: '', // 职业证书编号
        IS_PERMANENT_VALID: '', // 是否永久有效
        CERTIFICATE_DATE_BEGIN: '', // 证书有效期（开始）
        CERTIFICATE_DATE_END: '', // 证书有效期（结束）
        CERTIFICATE_FILEPATH: ''// 证书照片
      },
      multipleSelectionAll: [], // 所有选中的数据包含跨页数据
      multipleSelection: [] // 当前页选中的数据
    }
  },
  created() {
    this.getList(this.ROLE_ID)
  },
  methods: {
    getRowKey(row) {
      return row.TEACHERCERTIFICATE_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/teachercertificate/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS,
          TEACHER_ID: this.$parent.TEACHER_ID
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 添加
    handleAdd() {
      this.$parent.activeName = 'CertificateEdit'
      this.$parent.TEACHERCERTIFICATE_ID = ''
    },
    // 修改
    handleEdit(ID) {
      this.$parent.activeName = 'CertificateEdit'
      this.$parent.TEACHERCERTIFICATE_ID = ID
    },
    handleInfo(ID) {
      this.$parent.activeName = 'CertificateInfo'
      this.$parent.TEACHERCERTIFICATE_ID = ID
    },

    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/teachercertificate/delete',
          {
            TEACHERCERTIFICATE_ID: id
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },

    batchDel() {
      const _selectData = this.$refs.multipleTable.selection
      if (_selectData == null || _selectData.length == 0) {
        this.$message({
          message: '请选中要删除的项...',
          type: 'error'
        })
        return false
      }
      const ids = _selectData.map((item, index) => {
        return item.TEACHERCERTIFICATE_ID
      }).join(',')

      this.$confirm('确定要删除选中的数据吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/teachercertificate/deleteAll',
          {
            DATA_IDS: ids
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.$refs.multipleTable.clearSelection()
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    // 判断按钮权限，用于是否显示按钮
    hasButton: function() {
      var keys = 'teacher:add,teacher:del,teacher:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.teacherfhadminadd		// 新增权限
        this.del = data.teacherfhadmindel		// 删除权限
        this.edit = data.teacherfhadminedit	// 修改权限
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 获取数据字典数据
    getDict: function() {
      requestFN(
        '/dictionaries/getLevels',
        {
          DICTIONARIES_ID: '82d41f1cab2a4ed394ddbe69a15bb8f1'
        }
      ).then((data) => {
        this.QUALIFICATION_TYPE_List = data.list
      }).catch((e) => {

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

</script>
