<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 style="width: 200px;" @clear="getQuery"/>
      </div>
      <div class="filter-group">
        <div class="filter-lable"> 资质类型：</div>
        <el-select ref="SELECT_QUALIFICATION_TYPE" v-model="QUALIFICATION_TYPE" clearable @clear="getQuery">
          <el-option v-for="item in QUALIFICATION_TYPE_List" :key="item.BIANMA" :label="item.NAME" :value="item.BIANMA" />
        </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="NAME" label="教师姓名" />
      <el-table-column prop="PHONE" label="手机号" />
      <el-table-column prop="IDENTITY_NUMBER" label="身份证号" />
      <el-table-column prop="QUALIFICATION_TYPE_NAMES" label="资质类型" />
      <el-table-column prop="EDUCATION_LEVEL_NAME" label="文化程度" />
      <el-table-column prop="PROFESSION_NAME" label="专业" />
      <el-table-column label="操作" align="center" width="360">
        <template slot-scope="{row}">
          <el-button class="light-blue-btn" icon="el-icon-postcard" size="mini" @click="handleCertificate(row.TEACHER_ID)">证书</el-button>
          <el-button icon="el-icon-view" size="mini" type="success" @click="handleInfo(row.TEACHER_ID)">查看</el-button>
          <el-button v-show="edit" type="primary" icon="el-icon-edit" size="mini" @click="handleEdit(row.TEACHER_ID)">编辑</el-button>
          <el-button v-show="del" type="danger" icon="el-icon-delete" size="mini" @click="handleDelete(row.TEACHER_ID)">删除</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>
        <el-button v-show="del" type="danger" icon="el-icon-delete" plain @click="batchDel">删除</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
export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      KEYWORDS: '',
      QUALIFICATION_TYPE: '',
      QUALIFICATION_TYPE_List: [], // 资质类型数据字典
      varList: [],
      form: {
        NAME: '', // 教师姓名
        PHONE: '', // 手机号
        IDENTITY_NUMBER: '', // 身份证号
        EDUCATION_LEVEL: '', // 文化程度
        PROFESSION: '', // 专业
        IDENTITY_FILEPATH_FRONT: '', // 身份证照片（正面）
        IDENTITY_FILEPATH_REVERSE: '', // 身份证照片（反面）
        DIPLOMA_FILEPATH: '', // 毕业证照片
        CERTIFICATE_FILEPATH: ''// 资格证照片
      },
      multipleSelectionAll: [], // 所有选中的数据包含跨页数据
      multipleSelection: [] // 当前页选中的数据
    }
  },
  created() {
    this.getDict()
    this.getList(this.ROLE_ID)
  },
  methods: {
    getRowKey(row) {
      return row.TEACHER_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    goKeyReset() {
      this.KEYWORDS = ''
      this.QUALIFICATION_TYPE = ''
      this.getQuery()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/teacher/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS,
          QUALIFICATION_TYPE: this.QUALIFICATION_TYPE
        }
      ).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 = 'TeacherEdit'
      this.$parent.TEACHER_ID = ''
    },
    // 修改
    handleEdit(ID) {
      this.$parent.activeName = 'TeacherEdit'
      this.$parent.TEACHER_ID = ID
    },
    // 资质类型
    handleCertificate(ID) {
      this.$parent.activeName = 'CertificateList'
      this.$parent.TEACHER_ID = ID
    },
    handleInfo(ID) {
      this.$parent.activeName = 'TeacherInfo'
      this.$parent.TEACHER_ID = ID
    },
    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/teacher/delete',
          {
            TEACHER_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.TEACHER_ID
      }).join(',')

      this.$confirm('确定要删除选中的数据吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/teacher/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' // 资质类型PARENT_ID
        }
      ).then((data) => {
        this.QUALIFICATION_TYPE_List = data.list
      }).catch((e) => {

      })
    },
    resetForm() {
      this.form = {
        NAME: '', // 教师姓名
        PHONE: '', // 手机号
        IDENTITY_NUMBER: '', // 身份证号
        EDUCATION_LEVEL: '', // 文化程度
        PROFESSION: '', // 专业
        IDENTITY_FILEPATH_FRONT: '', // 身份证照片（正面）
        IDENTITY_FILEPATH_REVERSE: '', // 身份证照片（反面）
        DIPLOMA_FILEPATH: '', // 毕业证照片
        CERTIFICATE_FILEPATH: ''// 资格证照片
      }
    }
  }
}

</script>
