<template>
  <NuxtLink
    class="w-full sm:w-72 h-96 rounded-lg shadow-md border border-gray-200 flex-col justify-start items-start inline-flex overflow-hidden"
    :class="allowNavigation ? '' : 'pointer-events-none'"
    :to="`${urlOverwrite ?? $route.path}/${data?.slug}`"
  >
    <div class="w-full h-56 relative">
      <NuxtPicture
        loading="lazy"
        class="w-full h-full object-cover object-center"
        :src="data?.image?.url || logo?.url ? baseUrl + (data?.image?.url ?? logo?.url) : '/favicon.png'"
        :imgAttrs="{ class: 'w-full h-full object-cover object-center' }"
      />
      <h1
        v-if="itemIndex"
        class="text-center text-black text-4xl my-auto absolute bottom-2 left-2"
        style="text-shadow: 2px 2px 4px white"
      >
        {{ itemIndex }}.
      </h1>
    </div>

    <div class="w-full h-44 relative bg-white px-2 py-5 flex flex-col justify-start items-start gap-2 overflow-y-auto">
      <h1>
        {{ data?.title }}
      </h1>
      <p v-if="itemDate && lookup?.show_date" class="w-full text-[#5c5c5c]">
        {{ itemDate }}
      </p>
      <p class="w-full text-[#5c5c5c] line-clamp-2 overflow-hidden">
        {{ data?.description }}
      </p>
    </div>
  </NuxtLink>
</template>

<script setup lang="ts">
import type { PropType } from "vue";
import type BaseCollection from "~/types/collection/baseCollection";
import type Lookup from "../../types/collection/lookup";

const runtimeConfig = useRuntimeConfig();
const baseUrl = runtimeConfig.public.strapi.url;

const { logo } = await provideGlobal();

const props = defineProps({
  data: Object as PropType<BaseCollection>,
  lookup: Object as PropType<Lookup>,
  numberOverwrite: { type: Number, default: undefined },
  allowNavigation: { type: Boolean, default: false },
  urlOverwrite: { type: String, default: undefined },
});

const itemIndex = computed(() => {
  if (props.lookup?.items_with_number != "none") {
    return props.numberOverwrite;
  } else if (props.lookup?.list_with_date != "none") {
    return new Date(props.data?.date ?? "").toLocaleString("de-DE", {
      day: "2-digit",
    });
  } else {
    return undefined;
  }
});

const itemDate = computed(() => {
  let config = {
    year:
      props.lookup?.list_with_date == "none" || props.lookup?.items_with_number != "none"
        ? ("numeric" as const)
        : undefined,
    month:
      props.lookup?.list_with_date != "by-month" || props.lookup?.items_with_number != "none"
        ? ("long" as const)
        : undefined,
    day:
      props.lookup?.list_with_date == "none" || props.lookup?.items_with_number != "none"
        ? ("2-digit" as const)
        : undefined,
    minute: props.data?.date.includes("T") ? ("2-digit" as const) : undefined,
    hour: props.data?.date.includes("T") ? ("2-digit" as const) : undefined,
  };
  if (Object.values(config).filter((c) => c).length != 0) {
    //(props.lookup?.list_with_date != "none" || props.lookup?.show_date)
    return (
      new Date(props.data?.date ?? "").toLocaleString("de-DE", config) + (props.data?.date.includes("T") ? "Uhr" : "")
    );
  } else {
    return undefined;
  }
});
</script>