<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">Vereinsämter</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">
          <ExecutivePositionListItem
            v-for="executivePosition in executivePositions"
            :key="executivePosition.id"
            :executivePosition="executivePosition"
          />
        </div>
        <div class="flex flex-row gap-4">
          <button
            v-if="can('create', 'settings', 'executive_position')"
            primary
            class="!w-fit"
            @click="openCreateModal"
          >
            Vereinsämt erstellen
          </button>
        </div>
      </div>
    </template>
  </MainTemplate>
</template>

<script setup lang="ts">
import { defineComponent, defineAsyncComponent, markRaw } from "vue";
import { mapState, mapActions } from "pinia";
import MainTemplate from "@/templates/Main.vue";
import { useExecutivePositionStore } from "@/stores/admin/executivePosition";
import ExecutivePositionListItem from "@/components/admin/settings/executivePosition/ExecutivePositionListItem.vue";
import { useModalStore } from "@/stores/modal";
import { useAbilityStore } from "@/stores/ability";
</script>

<script lang="ts">
export default defineComponent({
  computed: {
    ...mapState(useExecutivePositionStore, ["executivePositions"]),
    ...mapState(useAbilityStore, ["can"]),
  },
  mounted() {
    this.fetchExecutivePositions();
  },
  methods: {
    ...mapActions(useExecutivePositionStore, ["fetchExecutivePositions"]),
    ...mapActions(useModalStore, ["openModal"]),
    openCreateModal() {
      this.openModal(
        markRaw(
          defineAsyncComponent(
            () => import("@/components/admin/settings/executivePosition/CreateExecutivePositionModal.vue")
          )
        )
      );
    },
  },
});
</script>
