<template>
  <div class="role-manage">
    <div class="part-box part-box-pad part-box-flex">
      <div></div>
      <el-button
        type="primary"
        icon="el-icon-circle-plus-outline"
        @click="toAdd"
        >添加角色</el-button
      >
    </div>
    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="roles">
        <el-table-column
          type="index"
          label="序号"
          width="70"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column prop="name" label="角色名称"></el-table-column>
        <el-table-column class-name="action-column" label="操作" width="220px">
          <template slot-scope="scope">
            <!-- <el-button
              :class="scope.row.defaultRole ? 'btn-danger' : 'btn-primary'"
              type="text"
              @click="toSetDefaultRole(scope.row)"
              >{{ scope.row.defaultRole ? "取消为内置角色" : "设置为内置角色" }}
            </el-button> -->
            <el-button
              class="btn-primary"
              type="text"
              @click="toEdit(scope.row)"
              >编辑</el-button
            >
            <!-- <el-button
              v-if="!scope.row.defaultRole"
              class="btn-danger"
              type="text"
              @click="toDelete(scope.row)"
              >删除</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- ModifySystemRole -->
    <modify-system-role
      ref="ModifySystemRole"
      :instance="curRole"
      @modified="getList"
    ></modify-system-role>
  </div>
</template>

<script>
import { ABLE_TYPE } from "@/constants/enumerate";
import { adminRoleListPage, setRoleDefault } from "../api";
import { deleteRole } from "../../base/api";
import ModifySystemRole from "../components/ModifySystemRole";

export default {
  name: "role-manage",
  components: {
    ModifySystemRole,
  },
  data() {
    return {
      roles: [],
      curRole: {},
      ABLE_TYPE,
      ROLE_TYPE: {},
    };
  },
  created() {
    this.toPage(1);
  },
  methods: {
    async getList() {
      const data = await adminRoleListPage();
      this.roles = data || [];
      this.roles = this.roles.filter((item) => item.type !== "ADMIN");
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toEdit(row) {
      this.curRole = row;
      this.$refs.ModifySystemRole.open();
    },
    toAdd() {
      this.curRole = {};
      this.$refs.ModifySystemRole.open();
    },
    toSetDefaultRole(row) {
      const action = row.defaultRole ? "取消" : "设置";
      this.$confirm(`确定要${action}角色【${row.name}】为内置角色吗?`, "提示", {
        type: "warning",
      })
        .then(async () => {
          const defaultRole = !row.defaultRole;
          await setRoleDefault({
            id: row.id,
            defaultRole,
          });
          row.defaultRole = defaultRole;
          this.$message.success("操作成功！");
        })
        .catch(() => {});
    },
    toDelete(row) {
      this.$confirm(`确定要删除角色【${row.name}】吗?`, "提示", {
        type: "warning",
      })
        .then(async () => {
          await deleteRole([row.id]);
          this.$message.success("删除成功！");
          this.deletePageLastItem();
        })
        .catch(() => {});
    },
  },
};
</script>
