<template>
  <el-card class="mod-user">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="handleGetTableList()">
      <el-form-item>
        <el-input v-model="dataForm.userName" placeholder="员工姓名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleGetTableList()">查询</el-button>
        <el-button v-if="isAuth('sys:user:save')" type="primary" @click="handleAddOrUpdate()">新增账户</el-button>
        <el-button
          v-if="isAuth('sys:user:delete')"
          type="danger"
          @click="handleDelete()"
          :disabled="dataListSelections.length <= 0"
        >批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="handleChangeSelection"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="userId" header-align="center" align="center" width="80" label="ID"></el-table-column>
      <el-table-column prop="username" header-align="center" align="center" label="员工姓名"></el-table-column>
      <el-table-column prop="email" header-align="center" align="center" label="账号"></el-table-column>
      <el-table-column prop="mobile" header-align="center" align="center" label="手机号"></el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="danger">禁用</el-tag>
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="180"
        label="创建时间"
      ></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="isAuth('sys:user:update')"
            type="text"
            size="small"
            @click="handleAddOrUpdate(scope.row.userId)"
          >编辑</el-button>
          <el-button
            v-if="isAuth('sys:user:delete')"
            type="text"
            size="small"
            @click="handleDelete(scope.row.userId)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleChangeSize"
      @current-change="handleChangeCurrent"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 编辑 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="handleGetTableList"
    ></add-or-update>
  </el-card>
</template>

<script>
import AddOrUpdate from './user-add-or-update'
import { apiSysUserList, apiSysUserDelete } from '@/api/api_sys'

export default {
  data () {
    return {
      dataForm: {
        userName: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  activated () {
    this.handleGetTableList()
  },
  methods: {
    // 获取数据列表
    handleGetTableList () {
      apiSysUserList({
        'page': this.pageIndex,
        'limit': this.pageSize,
        'username': this.dataForm.userName
      }).then(res => {
        if (res && res.code === 0) {
          this.dataList = res.page.list
          this.totalPage = res.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    handleChangeSize (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.handleGetTableList()
    },
    // 当前页
    handleChangeCurrent (val) {
      this.pageIndex = val
      this.handleGetTableList()
    },
    // 多选
    handleChangeSelection (val) {
      this.dataListSelections = val
    },
    // 新增 / 编辑
    handleAddOrUpdate (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    handleDelete (id) {
      let userIds = id ? [id] : this.dataListSelections.map(item => {
        return item.userId
      })
      this.$confirm(`确定删除该账户吗？删除后该账户将无法使用`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        apiSysUserDelete(userIds).then(res => {
          if (res && res.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.handleGetTableList()
              }
            })
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => { })
    }
  }
}
</script>
