<template>
  <MainTemplate :showBack="false">
    <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">Kalender</h1>
      </div>
    </template>
    <template #diffMain>
      <div class="flex flex-col w-full h-full gap-2 justify-between px-7 overflow-hidden">
        <FullCalendar :options="calendarOptions" class="max-h-full h-full" />
      </div>
    </template>
  </MainTemplate>
</template>

<script setup lang="ts">
import { defineComponent } from "vue";
import MainTemplate from "@/templates/Main.vue";
import FullCalendar from "@fullcalendar/vue3";
import deLocale from "@fullcalendar/core/locales/de";
import dayGridPlugin from "@fullcalendar/daygrid";
import timeGridPlugin from "@fullcalendar/timegrid";
import interactionPlugin from "@fullcalendar/interaction";
import type { CalendarViewModel } from "@/viewmodels/admin/calendar.models";
</script>

<script lang="ts">
export default defineComponent({
  data() {
    return {
      calendars: [] as Array<CalendarViewModel>,
    };
  },
  computed: {
    formattedItems() {
      return this.calendars.map((c) => ({
        id: c.id,
        title: c.title,
        start: c.starttime,
        end: c.endtime,
        backgroundColor: c.type.color,
      }));
    },
    calendarOptions() {
      return {
        timeZone: "local",
        locale: deLocale,
        plugins: [dayGridPlugin, timeGridPlugin, interactionPlugin],
        initialView: "dayGridMonth",
        headerToolbar: {
          left: "dayGridMonth,timeGridWeek",
          center: "title",
          right: "prev,today,next",
        },
        eventDisplay: "block",
        weekends: true,
        editable: false,
        selectable: false,
        selectMirror: false,
        dayMaxEvents: true,
        weekNumbers: true,
        displayEventTime: true,
        nowIndicator: true,
        weekText: "KW",
        allDaySlot: false,
        events: this.formattedItems,
      };
    },
  },
  mounted() {
    this.fetchCalendars();
  },
  methods: {
    fetchCalendars() {
      this.$http
        .get("/public/calendar?output=json")
        .then((result) => {
          this.calendars = result.data;
        })
        .catch((err) => {});
    },
  },
});
</script>
