<template>
  <div class="relative w-full md:max-w-md">
    <div class="flex flex-col items-center">
      <p class="text-xl font-medium">Termin</p>
    </div>
    <br />
    <div class="flex flex-col gap-4 py-2">
      <div>
        <label for="title">Terminart</label>
        <input type="text" id="title" readonly :value="data.type?.type" />
      </div>
      <div>
        <label for="title">Titel</label>
        <input type="text" id="title" readonly :value="data.title" />
      </div>
      <div>
        <label for="content">Beschreibung</label>
        <textarea id="content" class="h-18" readonly :value="data.content"></textarea>
      </div>
      <div v-if="data.allDay" class="flex flex-row gap-2 items-center">Der Termin findet ganztägig statt.</div>
      <div v-if="data.allDay == false" class="flex flex-row gap-2">
        <div class="w-full">
          <label for="starttime">Startzeit</label>
          <input type="datetime-local" id="starttime" readonly :value="formatForDateTimeLocalInput(data.starttime)" />
        </div>
        <div class="w-full">
          <label for="endtime">Endzeit</label>
          <input
            ref="endtime"
            type="datetime-local"
            id="endtime"
            readonly
            :value="formatForDateTimeLocalInput(data.endtime)"
          />
        </div>
      </div>
      <div v-else class="flex flex-row gap-2">
        <div class="w-full">
          <label for="startdate">Startdatum</label>
          <input type="date" id="startdate" readonly :value="formatForDateInput(data.starttime)" />
        </div>
        <div class="w-full">
          <label for="enddate">Enddatum</label>
          <input ref="enddate" type="date" id="enddate" readonly :value="formatForDateInput(data.endtime)" />
        </div>
      </div>
      <div>
        <label for="location">Ort</label>
        <input type="text" id="location" readonly :value="data.location" />
      </div>
    </div>

    <div class="flex flex-row justify-end">
      <div class="flex flex-row gap-4 py-2">
        <button primary-outline @click="closeModal">schließen</button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineAsyncComponent, defineComponent, markRaw } from "vue";
import { mapState, mapActions } from "pinia";
import { useModalStore } from "@/stores/modal";
import Spinner from "@/components/Spinner.vue";
</script>

<script lang="ts">
export default defineComponent({
  computed: {
    ...mapState(useModalStore, ["data"]),
  },
  methods: {
    ...mapActions(useModalStore, ["closeModal"]),
    formatForDateTimeLocalInput(utcDateString: string) {
      const localDate = new Date(utcDateString);

      const year = localDate.getFullYear();
      const month = String(localDate.getMonth() + 1).padStart(2, "0");
      const day = String(localDate.getDate()).padStart(2, "0");
      const hours = String(localDate.getHours()).padStart(2, "0");
      const minutes = String(localDate.getMinutes()).padStart(2, "0");

      return `${year}-${month}-${day}T${hours}:${minutes}`;
    },
    formatForDateInput(utcDateString: string) {
      const localDate = new Date(utcDateString);

      const year = localDate.getFullYear();
      const month = String(localDate.getMonth() + 1).padStart(2, "0");
      const day = String(localDate.getDate()).padStart(2, "0");

      return `${year}-${month}-${day}`;
    },
  },
});
</script>
