<template>
  <div class="role-wrapper">
    <el-button class="new-role" type="primary" size="small" @click="showConfigDialog">New Role</el-button>
    <el-table :data="tableData" style="min-height:500px">
      <el-table-column prop="roleName" label="Role Name"></el-table-column>
      <el-table-column prop="resName" label="Resources" show-overflow-tooltip></el-table-column>
      <el-table-column prop="action" label="Operate" width="250px">
        <template v-slot="scope">
          <el-popconfirm
            confirm-button-text="Yes"
            cancel-button-text="No"
            icon="el-icon-warning"
            icon-color="red"
            title="Are you sure to delete this?"
            @confirm="toDelete(scope.row.id)"
          >
            <el-button type="danger" size="mini" slot="reference">Delete</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
      <template slot="empty" v-if="tableData.length == 0">
        <span>No data available</span>
      </template>
    </el-table>
    <el-pagination
      v-if="total > 0"
      :page-size="limit"
      :current-page.sync="page"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      background
      :page-sizes="[10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next"
    ></el-pagination>

    <el-dialog title="New Role" :visible.sync="dialogVisible" width="600px">
      <el-form :model="form" ref="ruleForm" label-width="100px" :rules="rules">
        <el-form-item label="Role Name" prop="name">
          <el-input v-model="form.name" placeholder="Please input role name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">Cancel</el-button>
        <el-button type="primary" size="small" @click="addRole">Confirm</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { GetRoleList, DeleteRole, AddRole } from "@/api/permission";
export default {
  data() {
    return {
      visible: false,
      dialogVisible: false,
      form: {
        name: "",
        type: "1",
      },
      rules: {
        name: [
          { required: true, message: "Please input role name", trigger: "blur" },
        ],
      },
      tableData: [],
      page: 1,
      limit: 10,
      total: 0,
      multipleSelection: [],
    };
  },
  created() {
    this.getTableData();
  },
  methods: {
    getTableData() {
      const { page, limit } = this;
      GetRoleList({ currentPage: page, pageSize: limit }).then((res) => {
        const { content, totalElements} = res;
        this.tableData = content;
        this.total = totalElements;
        if (page > 1 && content.length === 0) {
          this.handleCurrentChange(page - 1);
        }
      });
    },
    toDelete(id) {
      DeleteRole(id).then(() => {
        this.$message.success("Delete successfully!");
        this.handleCurrentChange(this.page);
      });
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getTableData();
    },
    handleSizeChange(val) {
      this.limit = val;
      this.handleCurrentChange(1);
    },
    showConfigDialog() {
      this.dialogVisible = true;
      setTimeout(() => {
        this.$refs.ruleForm.resetFields();
      }, 100)
    },
    addRole() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          AddRole({ roleName: this.form.name }).then(() => {
            this.dialogVisible = false;
            this.getTableData();
          });
        }
      });
    },
  },
};
</script>
<style scoped>
.role-wrapper {
  position: relative;
}
.new-role {
  position: absolute;
  right: 0;
  top: -56px
}
</style>