<template>
  <div class="w-full md:max-w-md">
    <div class="flex flex-col items-center">
      <p class="text-xl font-medium">Termintyp erstellen</p>
    </div>
    <br />
    <form class="flex flex-col gap-4 py-2" @submit.prevent="triggerCreate">
      <div>
        <label for="type">Bezeichnung</label>
        <input type="text" id="type" required />
      </div>
      <div class="flex flex-row items-center gap-2">
        <input type="color" id="color" required class="!px-1 !py-0 !w-10" />
        <label for="color">Farbe</label>
      </div>
      <div class="flex flex-row items-center gap-2">
        <input type="checkbox" id="nscdr" v-model="nscdr" />
        <label for="nscdr">Standard Kalender Auslieferung (optional)</label>
      </div>
      <div v-if="!nscdr">
        <label for="passphrase">Passphrase (optional)</label>
        <input type="text" id="passphrase" />
      </div>

      <div class="flex flex-row gap-2">
        <button primary type="submit" :disabled="status == 'loading' || status?.status == 'success'">erstellen</button>
        <Spinner v-if="status == 'loading'" class="my-auto" />
        <SuccessCheckmark v-else-if="status?.status == 'success'" />
        <FailureXMark v-else-if="status?.status == 'failed'" />
      </div>
    </form>

    <div class="flex flex-row justify-end">
      <div class="flex flex-row gap-4 py-2">
        <button primary-outline @click="closeModal" :disabled="status == 'loading' || status?.status == 'success'">
          abbrechen
        </button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineComponent } from "vue";
import { mapState, mapActions } from "pinia";
import { useModalStore } from "@/stores/modal";
import Spinner from "@/components/Spinner.vue";
import SuccessCheckmark from "@/components/SuccessCheckmark.vue";
import FailureXMark from "@/components/FailureXMark.vue";
import { useCalendarTypeStore } from "@/stores/admin/settings/calendarType";
import type { CreateCalendarTypeViewModel } from "@/viewmodels/admin/settings/calendarType.models";
</script>

<script lang="ts">
export default defineComponent({
  data() {
    return {
      status: null as null | "loading" | { status: "success" | "failed"; reason?: string },
      timeout: undefined as any,
      nscdr: false as boolean,
    };
  },
  beforeUnmount() {
    try {
      clearTimeout(this.timeout);
    } catch (error) {}
  },
  methods: {
    ...mapActions(useModalStore, ["closeModal"]),
    ...mapActions(useCalendarTypeStore, ["createCalendarType"]),
    triggerCreate(e: any) {
      let formData = e.target.elements;
      let createCalendarType: CreateCalendarTypeViewModel = {
        type: formData.type.value,
        color: formData.color.value,
        nscdr: formData.nscdr.checked,
        passphrase: formData.passphrase?.value,
      };
      this.status = "loading";
      this.createCalendarType(createCalendarType)
        .then(() => {
          this.status = { status: "success" };
          this.timeout = setTimeout(() => {
            this.closeModal();
          }, 1500);
        })
        .catch(() => {
          this.status = { status: "failed" };
        });
    },
  },
});
</script>
