<template>
  <div class="app-container">
    <el-form label-width="130px">
      <el-row>
        <el-col :span="6">
          <el-form-item label="用户名">
            <el-input v-model="KEYWORDS" placeholder="请输入用户名"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="培训状态">
            <el-select v-model="STATUS" placeholder="请选择培训状态" style="width: 100%;" clearable>
              <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label-width="10px">
            <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>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      ref="multipleTable"
      :data="varList"
      :header-cell-style="{'font-weight': 'bold','color': '#000'}"
      tooltip-effect="dark"
      border
      fit
      highlight-current-row>
      <el-table-column :selectable="handleSelectWithDifferentStatus" 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="NAME" label="姓名" align="center"/>
      <el-table-column prop="BELONG_TO_CORP_NAME" label="外派公司名称" align="center"/>
      <el-table-column label="操作" align="center" width="250">
        <template slot-scope="{row}">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="handleShow(row)">详情</el-button>
          <el-button type="primary" icon="el-icon-s-claim" size="mini" @click="approve([row])">审批</el-button>
          <el-button v-if="false" type="success" icon="el-icon-edit" size="mini" @click="getUserInfo(row)">电子合格证</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div>
        <el-button v-if="false" 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>
    <user ref="userInfo" append-to-body/>
    <user-info ref="userInfos" append-to-body/>
    <send-util ref="sendUtil" append-to-body @refresh="getList"/>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves' // waves directive
import vueQr from 'vue-qr'
import apply from './apply'
import user from './user.vue'
import SendUtil from './sendUtil.vue'
import UserInfo from './userInfo.vue'

export default {
  components: { UserInfo, SendUtil, Pagination, apply, vueQr, user },
  directives: { waves },
  data() {
    return {
      config: config,
      QRCodeDialog: false,
      qrcodeStr: '',
      userDetailForm: {},
      specialUsersList: {},
      detailForm: {},
      userEntryForm: {},
      imgUrl: '',
      SUPERVISE_CORPINFO_ID: '',
      wenhuachengduList: [], // 文化程度
      postList: [], // 岗位名称
      photosOfLevel: [], // 三级教育照片
      trainRecordList: [], // 培训记录
      contractFileList: [], // 合同文件
      insuranceFileList: [], // 保险文件
      gongshangbaoxianFile: [],
      userPhotoFile: [], // 头像
      userCardIDPhotoFile: [], // 身份证照片
      socialPhotoFile: [], // 身份证照片
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      varList: [],
      KEYWORDS: '',
      statusList: [{ value: 0, label: '未培训' }, { value: 1, label: '培训中' }, {
        value: 2,
        label: '已培训(合格)'
      }, { value: 3, label: '已培训(不合格)' }, { value: 9, label: '申请中' }],
      STATUS: '',
      add: false,		// 新增按钮
      del: false,		// 删除按钮
      edit: false	// 修改按钮
    }
  },
  created() {
    this.getDict()
    this.getList()
    this.hasButton()
  },
  methods: {
    handleSelectWithDifferentStatus(row, rowIndex) {
      if (row.STATUS === 0) {
        // 不禁用
        return true
      } else {
        // 禁用
        return false
      }
    },
    getQuery() {
      if (this.$refs.multipleTable) {
        this.$refs.multipleTable.clearSelection()
      }
      this.listQuery = {
        page: 1,
        limit: 10
      }
      this.getList()
    },
    goKeyReset() {
      this.KEYWORDS = ''
      this.STATUS = ''
      this.getQuery()
    },
    getList() {
      this.listLoading = true
      requestFN(
        '/trainingbatch/userList?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS,
          STATUS: this.STATUS,
          EMPLOY_FLAG: '1'
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
      })
        .catch((e) => {
          this.listLoading = false
        })
    },
    handleShow(row) {
      this.$refs.userInfos.init(row)
    },
    approve(row) {
      this.$refs.sendUtil.init(row)
    },
    getUserInfo(row) {
      this.$refs.userInfo.init(row)
    },
    handleAdd() {
      const _selectData = this.$refs.multipleTable.selection
      if (_selectData == null || _selectData.length == 0) {
        this.$message({
          message: '未勾选培训人员...',
          type: 'error'
        })
        return false
      }

      var users = []
      _selectData.map(item => {
        if (item.STATUS === 0) {
          users.push(item.USER_ID)
        }
      })

      if (users == null || users.length == 0) {
        this.$message({
          message: '勾选人员暂无法提交申请...',
          type: 'error'
        })
        return false
      }
      this.$refs.apply.init(users)
    },
    hasButton() {
      var keys = 'trainingbatch:add,trainingbatch:del,trainingbatch:edit,fhSms,email,fromExcel,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys, tm: new Date().getTime()
        }
      ).then((data) => {
        if (data.result == 'success') {
          this.add = data.trainingbatchfhadminadd	// 增
          this.del = data.trainingbatchfhadmindel	// 删
          this.edit = data.trainingbatchfhadminedit	// 改
          this.fromExcel = data.fromExcel	// 从excel导入权限
          this.toExcel = data.toExcel		// 导出到excel权限
        } else if (data.result == 'exception') {
          // showException('按钮权限', data.exception)// 显示异常
          console.info('按钮权限:', data.exception)
        }
      })
        .catch((e) => {
          this.listLoading = false
        })
    },
    getDict: function() {
      requestFN(
        'dictionaries/getLevels',
        { DICTIONARIES_ID: 'd7d80f08d73a4accbccf4fd3d8d1d867' }
      ).then((data) => {
        this.wenhuachengduList = data.list
      }).catch((e) => {
        this.listLoading = false
      })
      requestFN(
        'dictionaries/getLevels',
        { DICTIONARIES_ID: '09e36ac01e9540f8bc84eab1c1a78754' }
      ).then((data) => {
        this.postList = data.list
      }).catch((e) => {
        this.listLoading = false
      })
    }
  }
}
</script>