<template>
  <div class="flex flex-col gap-2 h-full w-full overflow-y-auto">
    <div v-if="memberships != null" class="flex flex-col gap-2 w-full">
      <MembershipListItem v-for="membership in memberships" :key="membership.id" :membership="membership" />
    </div>
    <Spinner v-if="loading == 'loading'" class="mx-auto" />
    <p v-else-if="loading == 'failed'" @click="fetchItem" class="cursor-pointer">&#8634; laden fehlgeschlagen</p>
  </div>
  <div class="flex flex-row gap-4">
    <button primary class="!w-fit" @click="openCreateModal">Mitgliedschaft hinzufügen</button>
  </div>
</template>

<script setup lang="ts">
import { defineComponent, markRaw, defineAsyncComponent } from "vue";
import { mapActions, mapState } from "pinia";
import Spinner from "@/components/Spinner.vue";
import { useMembershipStore } from "@/stores/admin/membership";
import { useModalStore } from "@/stores/modal";
import MembershipListItem from "@/components/admin/club/member/MembershipListItem.vue";
</script>

<script lang="ts">
export default defineComponent({
  props: {
    memberId: String,
  },
  computed: {
    ...mapState(useMembershipStore, ["memberships", "loading"]),
  },
  mounted() {
    this.fetchItem();
  },
  methods: {
    ...mapActions(useMembershipStore, ["fetchMembershipsForMember"]),
    ...mapActions(useModalStore, ["openModal"]),
    fetchItem() {
      this.fetchMembershipsForMember();
    },
    openCreateModal() {
      this.openModal(
        markRaw(defineAsyncComponent(() => import("@/components/admin/club/member/MembershipCreateModal.vue")))
      );
    },
  },
});
</script>
