<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>
        {{ user.firstname }} {{ user.lastname }} <small v-if="user.permissions_total.admin">(Admin)</small
        ><small v-if="user.isOwner"> (Owner)</small>
      </p>
      <div class="flex flex-row">
        <RouterLink
          v-if="can('admin', 'management', 'user')"
          :to="{ name: 'admin-user-user-roles', params: { id: user.id } }"
        >
          <UserGroupIcon class="w-5 h-5 p-1 box-content cursor-pointer" />
        </RouterLink>
        <RouterLink
          v-if="can('admin', 'management', 'user')"
          :to="{ name: 'admin-user-user-permission', params: { id: user.id } }"
        >
          <WrenchScrewdriverIcon class="w-5 h-5 p-1 box-content cursor-pointer" />
        </RouterLink>
        <RouterLink
          v-if="can('update', 'management', 'user')"
          :to="{ name: 'admin-user-user-edit', params: { id: user.id } }"
        >
          <PencilIcon class="w-5 h-5 p-1 box-content cursor-pointer" />
        </RouterLink>
        <div
          v-if="can('delete', 'management', 'user')"
          :class="user.isOwner ? 'opacity-75 pointer-events-none' : ''"
          @click="openDeleteModal"
        >
          <TrashIcon class="w-5 h-5 p-1 box-content cursor-pointer" />
        </div>
      </div>
    </div>
    <div class="flex flex-col p-2">
      <div class="flex flex-row gap-2">
        <p class="min-w-16">Benutzer:</p>
        <p class="grow overflow-hidden">{{ user.username }}</p>
      </div>
      <div class="flex flex-row gap-2">
        <p class="min-w-16">Mail:</p>
        <p class="grow overflow-hidden">{{ user.mail }}</p>
      </div>
      <div class="flex flex-row gap-2">
        <p class="min-w-16">Rollen:</p>
        <div class="flex flex-row gap-2 flex-wrap grow">
          <p v-for="role in user.roles" :key="role.id" class="px-1 border border-gray-300 rounded-md">
            {{ role.role }}
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

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

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