<template>
  <div class="app-warp">
    <div class="app-structure">
      <div class="app-search">
        <el-form class="app-search-form">
          <div class="form-item">
            <label> 教师姓名：</label>
            <el-input v-model="KEYWORDS" placeholder="搜索教师姓名" clearable @clear="getQuery"/>
          </div>
          <div class="form-item">
            <label> 资质类型：</label>
            <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>
          <div class="form-item text-right">
            <el-button v-waves type="primary" icon="el-icon-search" @click="getQuery">
              搜索
            </el-button>
            <el-button v-waves icon="el-icon-refresh" @click="goKeyReset">
              重置
            </el-button>
          </div>
        </el-form>
      </div>
      <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 class="app-minbtn">
          <span class="screening">
            <el-dropdown trigger="click">
              <el-button type="text" icon="el-icon-finished"/>
              <el-dropdown-menu slot="dropdown" style="margin-top:3px;">
                <div class="app-checkbox-group">
                  <el-checkbox-group v-model="checkList">
                    <el-checkbox v-for="item in checkboxList" :key="item" :label="item" style="display:block;"/>
                  </el-checkbox-group>
                </div>
              </el-dropdown-menu>
            </el-dropdown>
          </span>

          <el-button type="text" icon="el-icon-refresh-left"/>
          <el-button type="text" icon="el-icon-full-screen"/>
        </div>
      </div>
      <div class="app-container">
        <el-table
          v-loading="listLoading"
          ref="multipleTable"
          :data="varList"
          :row-key="getRowKey"
          :header-cell-style="{
            background:'#f5f7fa',
            '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 v-if="checkList.includes('手机号')" prop="PHONE" label="手机号" />-->
          <el-table-column v-if="checkList.includes('身份证号')" prop="IDENTITY_NUMBER" label="身份证号" />
          <el-table-column v-if="checkList.includes('资质类型')" prop="QUALIFICATION_TYPE_NAMES" label="资质类型" />
          <!--          <el-table-column v-if="checkList.includes('文化程度')" prop="EDUCATION_LEVEL_NAME" label="文化程度" />-->
          <!--          <el-table-column v-if="checkList.includes('专业')" prop="PROFESSION_NAME" label="专业" />-->

          <el-table-column v-if="checkList.includes('证书编号')" prop="VOCATIONAL_CERTIFICATE_NUMBERS" label="证书编号" />
          <el-table-column v-if="checkList.includes('上传时间')" prop="CREATTIMES" label="上传时间" />
          <el-table-column label="操作" align="center" width="280">
            <template slot-scope="{row}">
              <el-button type="text" @click="handleCertificate(row.TEACHER_ID)">证书</el-button>
              <span class="app-divider"/>
              <el-button type="text" @click="handleInfo(row.TEACHER_ID)">查看</el-button>
              <span class="app-divider"/>
              <el-button v-show="edit" type="text" @click="handleEdit(row.TEACHER_ID)">编辑</el-button>
              <span class="app-divider"/>
              <el-button v-show="del" type="text" class="color-red" @click="handleDelete(row.TEACHER_ID)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="page-btn-group">
        <div/>
        <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
      </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: '',
      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: [], // 当前页选中的数据
      checkList: ['身份证号', '资质类型', '证书编号', '上传时间'],
      checkboxList: ['身份证号', '资质类型', '证书编号', '上传时间']
    }
  },
  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>
