<template>
  <div class="flex flex-col gap-2 h-full w-full">
    <Pagination
      :items="damageReports"
      :totalCount="totalCount"
      :indicateLoading="false"
      @load-data="(offset, count, search) => fetchDamageReportForVehicle(offset, count, search)"
      @search="(search) => fetchDamageReportForVehicle(0, 25, search, true)"
    >
      <template #pageRow="{ row }: { row: DamageReportViewModel }">
        <RouterLink
          :to="{ name: 'admin-unit-damage_report-overview', params: { damageReportId: row.id } }"
          class="flex flex-col h-fit w-full border border-primary rounded-md"
        >
          <div class="bg-primary p-2 text-white flex flex-row gap-2 items-center">
            <PencilSquareIcon v-if="!row.done" class="w-5 h-5" />
            <p class="grow">{{ new Date(row.reportedAt).toLocaleString("de") }} - {{ row.status }}</p>
            <div class="flex flex-row gap-2">
              <div v-if="row.images.length != 0" class="cursor-pointer">
                <PhotoIcon class="w-5 h-5" />
              </div>
              <div v-if="row.location" class="cursor-pointer">
                <MapPinIcon class="w-5 h-5" />
              </div>
              <div v-if="row.reportedBy" class="cursor-pointer">
                <UserIcon class="w-5 h-5" />
              </div>
              <div v-if="row.repair" class="cursor-pointer">
                <WrenchScrewdriverIcon class="w-5 h-5" />
              </div>
            </div>
          </div>
          <div class="p-2">
            <p>Beschreibung: {{ row.description }}</p>
          </div>
        </RouterLink>
      </template>
    </Pagination>
  </div>
</template>

<script setup lang="ts">
import { defineComponent } from "vue";
import { mapActions, mapState } from "pinia";
import { useAbilityStore } from "@/stores/ability";
import { useVehicleDamageReportStore } from "@/stores/admin/unit/vehicle/damageReport";
import Pagination from "@/components/Pagination.vue";
import type { DamageReportViewModel } from "@/viewmodels/admin/unit/damageReport.models";
import { PhotoIcon, PencilSquareIcon, MapPinIcon, WrenchScrewdriverIcon, UserIcon } from "@heroicons/vue/24/outline";
</script>

<script lang="ts">
export default defineComponent({
  props: {
    vehicleId: String,
  },
  computed: {
    ...mapState(useAbilityStore, ["can"]),
    ...mapState(useVehicleDamageReportStore, ["damageReports", "loading", "totalCount"]),
  },
  mounted() {
    this.fetchDamageReportForVehicle(0, 25, "", true);
  },
  methods: {
    ...mapActions(useVehicleDamageReportStore, ["fetchDamageReportForVehicle"]),
  },
});
</script>
