<template>
  <MainTemplate title="Rollen">
    <template #diffMain>
      <div class="flex flex-col gap-4 h-full pl-7">
        <div class="flex flex-col gap-2 grow overflow-y-scroll pr-7">
          <RoleListItem v-for="role in roles" :key="role.id" :role="role" />
        </div>
        <div class="flex flex-row gap-4">
          <button v-if="can('create', 'management', 'role')" primary class="w-fit!" @click="openCreateModal">
            Rolle erstellen
          </button>
        </div>
      </div>
    </template>
  </MainTemplate>
</template>

<script setup lang="ts">
import { defineAsyncComponent, defineComponent, markRaw } from "vue";
import { mapState, mapActions } from "pinia";
import MainTemplate from "@/templates/Main.vue";
import { useRoleStore } from "@/stores/admin/management/role";
import RoleListItem from "@/components/admin/management/role/RoleListItem.vue";
import { useModalStore } from "@/stores/modal";
import { useAbilityStore } from "@/stores/ability";
</script>

<script lang="ts">
export default defineComponent({
  computed: {
    ...mapState(useRoleStore, ["roles"]),
    ...mapState(useAbilityStore, ["can"]),
  },
  mounted() {
    this.fetchRoles();
  },
  methods: {
    ...mapActions(useRoleStore, ["fetchRoles"]),
    ...mapActions(useModalStore, ["openModal"]),
    openCreateModal() {
      this.openModal(
        markRaw(defineAsyncComponent(() => import("@/components/admin/management/role/CreateRoleModal.vue")))
      );
    },
  },
});
</script>
