<template>
  <MainTemplate title="Kleidungen">
    <template #diffMain>
      <div class="flex flex-col w-full h-full gap-2 justify-center px-7">
        <Pagination
          :items="wearables"
          :totalCount="totalCount"
          :indicateLoading="loading == 'loading'"
          useSearch
          useScanner
          @load-data="(offset, count, search) => fetchWearables(offset, count, search)"
          @search="(search) => fetchWearables(0, maxEntriesPerPage, search, true)"
        >
          <template #pageRow="{ row }: { row: WearableViewModel }">
            <WearableListItem :wearable="row" />
          </template>
        </Pagination>

        <div class="flex flex-row gap-4">
          <RouterLink
            v-if="can('create', 'unit', 'wearable')"
            :to="{ name: 'admin-unit-wearable-create' }"
            primary
            button
            class="w-fit!"
          >
            Kleidung erfassen
          </RouterLink>
        </div>
      </div>
    </template>
  </MainTemplate>
</template>

<script setup lang="ts">
import { defineComponent } from "vue";
import { mapActions, mapState } from "pinia";
import MainTemplate from "@/templates/Main.vue";
import { useWearableStore } from "@/stores/admin/unit/wearable/wearable";
import Pagination from "@/components/Pagination.vue";
import { useAbilityStore } from "@/stores/ability";
import type { WearableViewModel } from "@/viewmodels/admin/unit/wearable/wearable.models";
import WearableListItem from "@/components/admin/unit/wearable/WearableListItem.vue";
</script>

<script lang="ts">
export default defineComponent({
  data() {
    return {
      currentPage: 0,
      maxEntriesPerPage: 25,
    };
  },
  computed: {
    ...mapState(useWearableStore, ["wearables", "totalCount", "loading"]),
    ...mapState(useAbilityStore, ["can"]),
  },
  mounted() {
    this.fetchWearables(0, this.maxEntriesPerPage, "", true);
  },
  methods: {
    ...mapActions(useWearableStore, ["fetchWearables"]),
  },
});
</script>
