<template>
  <MainTemplate>
    <template #headerInsert>
      <RouterLink to="../" class="text-primary w-fit">zurück zur Liste</RouterLink>
    </template>
    <template #topBar>
      <div class="flex flex-row gap-2 items-center justify-between pt-5 pb-3 px-7">
        <h1 class="font-bold text-xl h-8 min-h-fit grow">{{ origin?.title }}</h1>
        <NewsletterSyncing
          :executeSyncAll="executeSyncAll"
          @syncState="
            (state) => {
              syncState = state;
            }
          "
        />
      </div>
    </template>
    <template #diffMain>
      <div class="flex flex-col gap-2 grow px-7 overflow-hidden">
        <div class="flex flex-col grow gap-2 overflow-hidden">
          <div class="w-full flex flex-row max-lg:flex-wrap justify-center">
            <RouterLink
              v-for="tab in tabs"
              :key="tab.route"
              v-slot="{ isActive }"
              :to="{ name: tab.route }"
              class="w-1/2 md:w-1/3 lg:w-full p-0.5 first:pl-0 last:pr-0"
            >
              <p
                :class="[
                  'w-full rounded-lg py-2.5 text-sm text-center font-medium leading-5 focus:ring-0 outline-none',
                  isActive ? 'bg-red-200 shadow border-b-2 border-primary rounded-b-none' : ' hover:bg-red-200',
                ]"
              >
                {{ tab.title }}
              </p>
            </RouterLink>
          </div>
          <RouterView />
        </div>
      </div>
    </template>
  </MainTemplate>
</template>

<script setup lang="ts">
import { defineAsyncComponent, defineComponent, markRaw } from "vue";
import { mapActions, mapState } from "pinia";
import MainTemplate from "@/templates/Main.vue";
import { RouterLink, RouterView } from "vue-router";
import { useNewsletterStore } from "@/stores/admin/club/newsletter/newsletter";
import { useModalStore } from "@/stores/modal";
import NewsletterSyncing from "@/components/admin/club/newsletter/NewsletterSyncing.vue";
import { PrinterIcon } from "@heroicons/vue/24/outline";
import { useNewsletterDatesStore } from "../../../../stores/admin/club/newsletter/newsletterDates";
import { useNewsletterRecipientsStore } from "../../../../stores/admin/club/newsletter/newsletterRecipients";
</script>

<script lang="ts">
export default defineComponent({
  props: {
    newsletterId: String,
  },
  watch: {
    syncState() {
      if (this.wantToClose && this.syncState == "synced") {
        this.wantToClose = false;
        this.closeModal();
      }
    },
  },
  data() {
    return {
      tabs: [
        { route: "admin-club-newsletter-overview", title: "Übersicht" },
        { route: "admin-club-newsletter-data", title: "Daten" },
        { route: "admin-club-newsletter-dates", title: "Termine" },
        { route: "admin-club-newsletter-recipients", title: "Empfänger" },
        { route: "admin-club-newsletter-printout", title: "Druck/Versand" },
      ],
      wantToClose: false as boolean,
      executeSyncAll: undefined as any,
      syncState: "synced" as "synced" | "syncing" | "detectedChanges" | "failed",
    };
  },
  computed: {
    ...mapState(useNewsletterStore, ["origin"]),
  },
  mounted() {
    this.fetchNewsletterByActiveId();
    this.fetchNewsletterDates();
    this.fetchNewsletterRecipients();
  },
  // this.syncState is undefined, so it will never work
  // beforeRouteLeave(to, from, next) {
  //   const answer = window.confirm('Do you really want to leave? you have unsaved changes!')
  //   if (answer) {
  //      next()
  //   } else {
  //     next(false)
  //   }
  //   if (this.syncState != "synced") {
  //     this.executeSyncAll = Date.now();
  //     this.wantToClose = true;
  //     this.openInfoModal();
  //     next(false);
  //   } else {
  //     next();
  //   }
  // },
  methods: {
    ...mapActions(useNewsletterStore, ["fetchNewsletterByActiveId"]),
    ...mapActions(useNewsletterDatesStore, ["fetchNewsletterDates"]),
    ...mapActions(useNewsletterRecipientsStore, ["fetchNewsletterRecipients"]),
    ...mapActions(useModalStore, ["openModal"]),
    openInfoModal() {
      this.openModal(
        markRaw(defineAsyncComponent(() => import("@/components/admin/club/newsletter/CurrentlySyncingModal.vue")))
      );
    },
    openDeleteModal() {
      // this.openModal(
      //   markRaw(defineAsyncComponent(() => import("@/components/admin/club/newsletter/DeleteNewsletterModal.vue"))),
      //   parseInt(this.newsletterId ?? "")
      // );
    },
  },
});
</script>
