<template>
  <div class="roles_list">
    <apass-list
      @list-action="initDatas"
      search-placeholder="请输入关键字"
      :list-header="listHeader"
      :list-data="listData"
      :list-total="listTotal"
    >
      <el-breadcrumb separator="/" slot="breadcrumb">
        <el-breadcrumb-item to="/authority">权限管理</el-breadcrumb-item>
        <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      </el-breadcrumb>
      <template slot="header-left">
        <el-button type="primary" @click="$router.push('/authority/roles/add')">
          新建角色
        </el-button>
      </template>
    </apass-list>

    <apass-dialog
      ref="dialog"
      :title="dialogInfo.title"
      :msg="dialogInfo.msg"
      :submit="dialogInfo.submit"
    ></apass-dialog>
  </div>
</template>

<script>
import apassList from "@/components/apass-list";
import apassDialog from "@/components/apass-dialog";

export default {
  components: { apassList, apassDialog },
  data: () => ({
    dialogInfo: {
      title: "",
      msg: "",
      submit: null,
    },
    listTotal: 0,
    listHeader: [],
    listData: [],
    tempFilter: null,
  }),
  methods: {
    initDatas(filters) {
      this.tempFilter = filters;

      this.$http
        .get(`/apaas/backmgt/role/list`, {
          params: {
            search: filters.keyword,
            page: filters.page,
            limit: filters.size,
          },
        })
        .then(({ data }) => {
          this.listTotal = data.total;
          this.listData = data.data;
        })
        .catch((error) => {
          console.log(error);
        });
    },
    showDialog() {
      this.$refs.dialog.show();
    },
    editItem(item) {
      this.$router.push(`/authority/roles/edit/${item.role_id}`);
    },
    allotItem(item) {
      this.$router.push(`/authority/roles/allot/${item.role_id}`);
    },
    deleteItem(item) {
      this.dialogInfo.title = "";
      this.dialogInfo.msg = "确认删除当前角色?";
      this.dialogInfo.cancelText = "";
      this.dialogInfo.sunbmitText = "";
      this.dialogInfo.submit = () => {
        this.$http
          .delete(`/apaas/backmgt/role/id/${item.role_id}`)
          .then(({ data }) => {
            this.$message({
              message: `已删除${item.role_name}.`,
              type: "success",
            });
            this.initDatas(this.tempFilter);
          })
          .catch((error) => {
            this.$message({
              message: `删除${this.deleteItem.role_name}失败`,
              type: "warning",
            });
          });
      };
      this.showDialog();
    },
  },
  mounted() {
    this.listHeader = [
      {
        label: "角色类型",
        prop: "role_name",
        type: "button",
        callback: this.editItem,
      },
      {
        label: "创建人",
        prop: "create_user",
        align: "center",
      },
      {
        label: "创建日期",
        prop: "create_date",
        align: "center",
      },
      {
        label: "操作",
        type: "buttons",
        align: "center",
        width: 240,
        actionList: [
          {
            label: "分配权限",
            callback: this.allotItem,
          },
          {
            label: "删除",
            class: "warn",
            callback: this.deleteItem,
          },
        ],
      },
    ];
  },
};
</script>

<style scoped>
.roles_list {
  height: 100%;
}
</style>
