<template>
  <div class="flex flex-col h-fit w-full border border-primary rounded-md">
    <div class="bg-primary p-2 text-white flex flex-row justify-between items-center">
      <p>{{ role.role }} <small v-if="role.permissions.admin">(Admin)</small></p>
      <div class="flex flex-row">
        <RouterLink
          v-if="can('admin', 'user', 'role')"
          :to="{ name: 'admin-user-role-permission', params: { id: role.id } }"
        >
          <WrenchScrewdriverIcon class="w-5 h-5 p-1 box-content cursor-pointer" />
        </RouterLink>
        <RouterLink
          v-if="can('update', 'user', 'role')"
          :to="{ name: 'admin-user-role-edit', params: { id: role.id } }"
        >
          <PencilIcon class="w-5 h-5 p-1 box-content cursor-pointer" />
        </RouterLink>
        <div v-if="can('delete', 'user', 'role')" @click="openDeleteModal">
          <TrashIcon class="w-5 h-5 p-1 box-content cursor-pointer" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineComponent, defineAsyncComponent, markRaw, type PropType } from "vue";
import { mapState, mapActions } from "pinia";
import { PencilIcon, WrenchScrewdriverIcon, TrashIcon } from "@heroicons/vue/24/outline";
import type { RoleViewModel } from "@/viewmodels/admin/user/role.models";
import { RouterLink } from "vue-router";
import { useAbilityStore } from "@/stores/ability";
import { useModalStore } from "@/stores/modal";
</script>

<script lang="ts">
export default defineComponent({
  props: {
    role: { type: Object as PropType<RoleViewModel>, default: {} },
  },
  computed: {
    ...mapState(useAbilityStore, ["can"]),
  },
  methods: {
    ...mapActions(useModalStore, ["openModal"]),
    openDeleteModal() {
      this.openModal(
        markRaw(defineAsyncComponent(() => import("@/components/admin/user/role/DeleteRoleModal.vue"))),
        this.role.id
      );
    },
  },
});
</script>
