<template>
  <div class="flex flex-col gap-2 h-full w-full overflow-y-auto">
    <div v-if="memberEducations != null" class="flex flex-col gap-2 w-full">
      <MemberEducationListItem v-for="education in memberEducations" :key="education.id" :education="education" />
    </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 v-if="can('create', 'club', 'member')" primary class="w-fit!" @click="openCreateModal">
      Aus-/Fortbildung hinzufügen
    </button>
  </div>
</template>

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

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