<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">Atemschutz-Einsätze</h1>
      </div>
    </template>
    <template #diffMain>
      <div class="flex flex-col w-full h-full gap-2 justify-center px-7">
        <Pagination
          :items="respiratoryMissions"
          :totalCount="totalCount"
          :indicateLoading="loading == 'loading'"
          useSearch
          @load-data="(offset, count, search) => fetchRespiratoryMissions(offset, count, search)"
          @search="(search) => fetchRespiratoryMissions(0, maxEntriesPerPage, search, true)"
        >
          <template #pageRow="{ row }: { row: RespiratoryMissionViewModel }">
            <RespiratoryMissionListItem :respiratoryMission="row" />
          </template>
        </Pagination>

        <div class="flex flex-row gap-4">
          <button v-if="can('create', 'club', 'respiratory_mission')" primary class="!w-fit" @click="openCreateModal">
            Einsatz erfassen
          </button>
        </div>
      </div>
    </template>
  </MainTemplate>
</template>

<script setup lang="ts">
import { defineAsyncComponent, defineComponent, markRaw } from "vue";
import { mapActions, mapState } from "pinia";
import MainTemplate from "@/templates/Main.vue";
import { useRespiratoryMissionStore } from "@/stores/admin/unit/respiratoryMission/respiratoryMission";
import { useModalStore } from "@/stores/modal";
import Pagination from "@/components/Pagination.vue";
import { useAbilityStore } from "@/stores/ability";
import type { RespiratoryMissionViewModel } from "@/viewmodels/admin/unit/respiratoryMission/respiratoryMission.models";
import RespiratoryMissionListItem from "@/components/admin/unit/respiratoryMission/RespiratoryMissionListItem.vue";
</script>

<script lang="ts">
export default defineComponent({
  data() {
    return {
      currentPage: 0,
      maxEntriesPerPage: 25,
    };
  },
  computed: {
    ...mapState(useRespiratoryMissionStore, ["respiratoryMissions", "totalCount", "loading"]),
    ...mapState(useAbilityStore, ["can"]),
  },
  mounted() {
    this.fetchRespiratoryMissions(0, this.maxEntriesPerPage, "", true);
  },
  methods: {
    ...mapActions(useRespiratoryMissionStore, ["fetchRespiratoryMissions"]),
    ...mapActions(useModalStore, ["openModal"]),
    openCreateModal() {
      this.openModal(
        markRaw(
          defineAsyncComponent(
            () => import("@/components/admin/unit/respiratoryMission/CreateRespiratoryMissionModal.vue")
          )
        )
      );
    },
  },
});
</script>
