<template>
  <div class="app-container usermanage">
    <div class="filter-container">
      <el-cascader
        v-model="parentids"
        :options="departmentDropDatas"
        :props="props"
        placeholder="请选择部门"
        clearable
        filterable
        change-on-select>
      </el-cascader>
      <el-select v-model="roleId" class="filter-item" filterable clearable placeholder="请选择角色">
        <el-option
          v-for="item in roleOptions"
          :key="item.F_RoleId"
          :label="item.F_RoleName"
          :value="item.F_RoleId"/>
      </el-select>
      <el-input v-model="keyword" placeholder="请输入工号，姓名，电话，手机" class="filter-item"/>
      <el-button type="primary" class="filter-item" icon="el-icon-search" @click="btn_search">搜索</el-button>
      <el-button v-permission="'HY_add'" type="primary" class="filter-item" icon="el-icon-plus" @click="btn_add">添加</el-button>
      <el-button v-permission="'HY_deletes'" type="primary" class="filter-item" icon="el-icon-delete" @click="btn_deletes">批量删除</el-button>
    </div>

    <el-table v-loading="loading" :data="dataLists" border stripe @selection-change="changeSelects">
      <el-table-column type="selection" width="40"/>
      <el-table-column prop="F_UserCode" label="工号" align="center" min-width=""/>
      <el-table-column prop="F_UserName" label="姓名" align="center" min-width=""/>
      <el-table-column prop="F_Dept" label="部门" align="center" min-width=""/>
      <el-table-column prop="F_Role" label="角色" align="center" min-width=""/>
      <el-table-column prop="F_SeartGroup" label="坐席组" align="center" min-width=""/>
      <el-table-column label="标志" align="center" min-width="">
        <template slot-scope="scope">
          {{ scope.row.F_SeatFlag | userSeatFlagFilter }}
        </template>
      </el-table-column>
      <el-table-column label="坐席权限组" align="center" min-width="">
        <template slot-scope="scope">
          {{ scope.row.F_SeatRight | userSeatRightFilter }}
        </template>
      </el-table-column>

      <el-table-column prop="F_Telephone" label="电话" align="center" min-width=""/>
      <el-table-column prop="F_Mobile" label="手机" align="center" min-width=""/>
      <el-table-column prop="F_Duty" label="职位" align="center" min-width=""/>
      <el-table-column prop="F_DeptTeam" label="团队" align="center" min-width=""/>
      <el-table-column prop="F_EntryDate" label="入职日期" align="center" min-width=""/>
      <el-table-column prop="F_FullDate" label="转正日期" align="center" min-width=""/>
      <el-table-column prop="F_TermDate" label="离职日期" align="center" min-width=""/>
      <el-table-column prop="F_CreateOn" label="添加日期" align="center" min-width=""/>
      <el-table-column prop="F_LastActiveTime" label="最后活跃时间" align="center" min-width=""/>
      <el-table-column label="操作" width="260" align="center" class-name="oparate_btn" fixed="right">
        <template slot-scope="scope">
          <el-button v-permission="'HY_modifyPass'" size="mini" plain type="primary" @click="btn_modifyPass(scope.row.F_UserId)">修改密码</el-button>
          <el-button v-permission="'HY_edit'" size="mini" plain type="primary" @click="btn_edit(scope.row.F_UserId.toString())">编辑</el-button>
          <el-button v-permission="'HY_delete'" size="mini" plain type="danger" @click="btn_delete(scope.row.F_UserId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="pageParams.total > 0"
      :total="pageParams.total"
      :pageindex.sync="pageParams.pageindex"
      :pagesize.sync="pageParams.pagesize"
      class="pagination"
      @pagination="getList" />
  </div>
</template>

<script>

import { getUserAccountLists, deleteUserAccount } from '@/api/systemSetup/roleSetting/userManage'
import { getRoleSelect } from '@/api/commonAPI'
import { getTreeList } from '@/api/systemSetup/roleSetting/department'
import addOrEdit from './addOrEdit'
import modifyPass from './modifyPass'
import Pagination from '@/components/Pagination' // 对el-pagination 二次封装

export default {
  name: 'UserManage',
  components: {
    Pagination
  },
  filters: {
    userSeatFlagFilter(status) {
      const statusMap = {
        false: '非话务',
        true: '话务'
      }
      return statusMap[status]
    },
    userSeatRightFilter(status) {
      const statusMap = {
        0: '普通坐席',
        1: '班长坐席',
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      loading: false,
      roleId: '',
      parentids: [],
      roleOptions: [],
      keyword: '',
      pageParams: {
        pageindex: 1, // 当前第几页
        pagesize: Number(this.$store.getters.serverConfig.PAGESIZE), // 每页几条数据
        total: 0 // 总共多少数据
      },
      multipleSelection: [], // 选中的数据
      dataLists: [], // 列表数据
      departmentDropDatas: [], // 下拉分类
      // 自定义菜单下拉数据的key值
      props: {
        value: 'id',
        label: 'text'
      },
    }
  },
  created() {
    this.getRoleSelects()
    this.getList()
    this.getTypeDrop()
    document.onkeyup = (e) => {
      if (e.keyCode === 13) {
        this.getList()
      }
    }
  },
  methods: {
    getList() {
      this.loading = true
      return new Promise(resolve => {
        const params = {
          pageindex: this.pageParams.pageindex, // 第几页
          pagesize: this.pageParams.pagesize, // 每页几条信息
          keyword: this.keyword.replace(/\s+/g, ''), // 否	string	模糊查询
          roleid: this.roleId, // 否	string	角色id
          deptid: this.parentids[this.parentids.length - 1], // array  部门id 
        }
        getUserAccountLists(params).then(response => {
          this.loading = false
          if (response.state.toLowerCase() === 'success') {
            this.pageParams.total = response.data.Totals
            this.dataLists = response.data.Rows
          }
        })
        resolve()
      })
    },
    // 获取分类下拉
    getTypeDrop() {
      return new Promise(resolve => {
        getTreeList().then(response => {
          if (response.state.toLowerCase() === 'success') {
            this.departmentDropDatas = response.data
          }
        })
        resolve()
      })
    },
    // 复选框状态改变
    changeSelects(val) {
      this.multipleSelection = val
    },
    btn_search() {
      this.pageParams.pageindex = 1
      this.getList()
    },
    btn_add() {
      this.$layer.iframe({
        content: {
          content: addOrEdit, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { 'rowid': '' }// props//该方法会自动添加一个key为layerid的值， 该值为创建层的id， 可以直接使用
        },
        area: ['60%', '90%'],
        title: '添加用户信息'
      })
    },
    btn_edit(editId) {
      this.$layer.iframe({
        content: {
          content: addOrEdit, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { 'rowid': editId }// props
        },
        area: ['60%', '90%'],
        title: '编辑用户信息'
      })
    },
    btn_modifyPass(editId) {
      this.$layer.iframe({
        content: {
          content: modifyPass, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { 'rowid': editId }// props
        },
        area: ['40%', '360px'],
        title: '修改密码'
      })
    },
    btn_delete(editId) {
      this.$confirm('您确定要将此用户信息删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUserAccount(editId).then(response => {
          if (response.state.toLowerCase() === 'success') {
            this.getList()
            this.$message.success('删除成功!')
          }
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    btn_deletes() {
      const ids = []
      for (let i = 0, len = this.multipleSelection.length; i < len; i++) {
        ids.push(this.multipleSelection[i].F_UserId)
      }
      if (ids.length <= 0) {
        this.$message.warning('没有可删除的选项')
        return
      }
      this.btn_delete(ids)
    },
    // 获取角色下拉
    getRoleSelects() {
      getRoleSelect().then(response => {
        if (response.state.toLowerCase() === 'success') {
          this.roleOptions = response.data
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
	.usermanage .filter-item.el-input{
		width: 240px;
	}
	.usermanage .user_img{
		width: 56px;
		height: 56px;
		border-radius: 50%;
	}
</style>
