<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button
        v-permission="'sys:user:edit'"
        class="filter-item"
        style="margin-left: 10px"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >
        添加用户
      </el-button>
    </div>
    <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column label="序号" align="center" width="50">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>
      <el-table-column label="头像" align="center" width="100">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" alt="" width="50" height="50" />
        </template>
      </el-table-column>
      <el-table-column label="登录名" align="center">
        <template slot-scope="scope">{{ scope.row.username }}</template>
      </el-table-column>
      <el-table-column label="姓名" align="center">
        <template slot-scope="scope">{{ scope.row.fullName }}</template>
      </el-table-column>
      <el-table-column label="手机号" align="center">
        <template slot-scope="scope">{{ scope.row.phone }}</template>
      </el-table-column>
      <el-table-column label="角色描述" align="center">
        <template slot-scope="scope">{{ scope.row.roleDesc }}</template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button v-permission="'sys:user:edit'" size="mini" type="primary" @click="handleUpdate(scope.row)">
            修改
          </el-button>
          <el-button v-permission="'sys:user:del'" size="mini" type="danger" @click="handleDelete(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { fetchList, deleteUser } from '@/api/sys/user'
import Pagination from '@/components/Pagination'
// 权限判断指令
import permission from '@/directive/permission/index.js'

export default {
  name: 'SysUserList',
  directives: { permission },
  components: { Pagination },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 5,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        parentId: 0
      }
    }
  },
  watch: {
    $route(route) {
      this.resetParentId()
      this.getList()
    }
  },
  created() {
    this.resetParentId()
    this.getList()
  },
  methods: {
    resetParentId() {
      this.listQuery.pageNum = 1
      if (this.$route.query.parentId != null) {
        this.parentId = this.$route.query.parentId
      } else {
        this.parentId = 0
      }
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then((response) => {
        this.listLoading = false
        console.log(response)
        this.list = response.data.results
        this.total = response.data.totalRecord
        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleCreate() {
      this.$router.push({ path: '/sys/users/add' })
    },
    handleUpdate(row) {
      this.$router.push({
        path: '/sys/users/edit',
        query: { id: row.id }
      })
    },
    handleDelete(row) {
      this.$confirm('是否要删除该用户', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUser(row.id).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1000
          })
          this.getList()
        })
      })
    }
  }
}
</script>

<style scoped></style>
