<template>
  <MainTemplate>
    <template #topBar>
      <div class="flex flex-row items-center justify-between pt-5 pb-3 px-7">
        <h1 class="font-bold text-xl h-8">Benutzer</h1>
      </div>
    </template>
    <template #diffMain>
      <div class="flex flex-col gap-4 grow pl-7">
        <div class="flex flex-col gap-2 grow overflow-y-scroll pr-7">
          <UserListItem v-for="user in users" :key="user.id" :user="user" />
        </div>
        <div class="flex flex-row gap-4">
          <button v-if="can('create', 'user', 'user')" primary class="!w-fit" @click="inviteUser">
            Nutzer einladen
          </button>
          <RouterLink button primary-outline :to="{ name: 'admin-user-user-invites' }" class="!w-fit">
            offene Einladungen
          </RouterLink>
        </div>
      </div>
    </template>
  </MainTemplate>
</template>

<script setup lang="ts">
import { defineComponent, markRaw, defineAsyncComponent } from "vue";
import { RouterLink } from "vue-router";
import { mapState, mapActions } from "pinia";
import MainTemplate from "@/templates/Main.vue";
import { useUserStore } from "@/stores/admin/user";
import { useModalStore } from "@/stores/modal";
import UserListItem from "@/components/admin/user/user/UserListItem.vue";
import { useAbilityStore } from "@/stores/ability";
</script>

<script lang="ts">
export default defineComponent({
  computed: {
    ...mapState(useUserStore, ["users"]),
    ...mapState(useAbilityStore, ["can"]),
  },
  mounted() {
    this.fetchUsers();
  },
  methods: {
    ...mapActions(useUserStore, ["fetchUsers"]),
    ...mapActions(useModalStore, ["openModal"]),
    inviteUser() {
      this.openModal(markRaw(defineAsyncComponent(() => import("@/components/admin/user/user/InviteUserModal.vue"))));
    },
  },
});
</script>
