<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-control">
        <el-button type="primary" @click="navigationTo('roles/add?type=add')">添加角色</el-button>
      </div>
    </div>
    <!-- 搜索区域END -->
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      border
      fit
      class="table-cell"
      header-row-class-name="tableHeader"
    >
      <el-table-column :index="indexMethod" label="序号" type="index" width="80" align="center" />
      <el-table-column prop="roleName" label="角色名" align="center" />
      <el-table-column label="角色描述" align="center">
        <template slot-scope="{ row }">
          <span v-if="row.roleDesc">{{ row.roleDesc }}</span>
          <span v-else>—</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" width="160px">
        <template slot-scope="{ row }">
          <span v-if="row.createTime">{{ row.createTime | formatTime }}</span>
          <span v-else>—</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" width="160px">
        <template slot-scope="{ row }">
          <span v-if="row.updateTime">{{ row.updateTime | formatTime }}</span>
          <span v-else>—</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" @click="navigationTo(`roles/edit?type=edit&id=${row.id}`)">编辑</el-button>
          <el-button type="danger" size="mini" @click="removeRole(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { fetchRoleList, deleteRole } from '@/api/system'
export default {
  data() {
    return {
      listLoading: false,
      listQuery: {
        status: '',
        pageSize: 10,
        pageNum: 1
      },
      list: []
    }
  },
  created() {
    this.fetchRoleList()
  },
  methods: {
    indexMethod(index) {
      return index + this.listQuery.pageSize * (this.listQuery.pageNum - 1) + 1
    },
    getList() {
      this.list = []
      this.listQuery.pageNum = 1
      this.fetchRoleList()
    },
    // 获取角色列表
    fetchRoleList() {
      fetchRoleList(this.listQuery).then(res => {
        this.list = [...this.list, ...res.data.list]
      })
    },
    // 删除角色
    removeRole(row) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteRole(row.id).then(res => {
            this.$message.success('删除角色成功')
            this.getList()
          })
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    }
  }
}
</script>

<style scoped lang="scss">
.avatar {
  display: block;
  width: 40px;
  height: 40px;
}
</style>
