<template>
  <el-card class="mod-role">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="handleGetTableList()">
      <el-form-item>
        <el-input v-model="dataForm.roleName" placeholder="角色名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleGetTableList()">查询</el-button>
        <el-button
          icon="el-icon-plus"
          v-if="isAuth('sys:role:save')"
          type="primary"
          @click="handleAddOrUpdate()"
        >新增角色</el-button>
        <el-button
          v-if="isAuth('sys:role: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="handleSelectionChange"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="roleId" header-align="center" align="center" width="80" label="ID"></el-table-column>
      <el-table-column prop="roleName" header-align="center" align="center" label="角色名称"></el-table-column>
      <el-table-column prop="remark" header-align="center" align="center" label="备注"></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="200" label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="isAuth('sys:role:update')"
            type="text"
            size="mini"
            @click="handleAddOrUpdate(scope.row.roleId)"
          >编辑</el-button>
          <el-button
            v-if="isAuth('sys:role:delete')"
            type="text"
            size="mini"
            @click="handleDelete(scope.row.roleId)"
          >删除</el-button>
          <el-button type="text" size="mini" @click="handleAddOrUpdate(scope.row.roleId,true)">已授权菜单</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :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 './role-add-or-update'
import { apiSysRoleList, apiSysRoleDelete } from '@/api/api_sys'

export default {
  data () {
    return {
      dataForm: {
        roleName: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 20,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: true
    }
  },
  components: {
    AddOrUpdate
  },
  activated () {
    this.handleGetTableList()
  },
  methods: {
    handleAuthorizedMenu (item) {
      console.log(item)
    },
    // 获取数据列表
    handleGetTableList () {
      apiSysRoleList({
        'page': this.pageIndex,
        'limit': this.pageSize,
        'roleName': this.dataForm.roleName
      }).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
      })
    },
    // 每页数
    handleSizeChange (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.handleGetTableList()
    },
    // 当前页
    handleCurrentChange (val) {
      this.pageIndex = val
      this.handleGetTableList()
    },
    // 多选
    handleSelectionChange (val) {
      this.dataListSelections = val
    },
    // 新增 / 编辑
    handleAddOrUpdate (id, disabled = false) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, disabled)
      })
    },
    // 删除
    handleDelete (id) {
      let ids = id ? [id] : this.dataListSelections.map(item => {
        return item.roleId
      })
      this.$confirm(`确定删除该角色吗？删除后该角色所有关联账号将无法使用相关权限。`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        apiSysRoleDelete(ids).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>
