<template>
  <div class="w-full md:max-w-md">
    <div class="flex flex-col items-center">
      <p class="text-xl font-medium">Nutzer einladen?</p>
    </div>
    <br />

    <form class="flex flex-col gap-4 py-2" @submit.prevent="invite">
      <div class="-space-y-px">
        <div>
          <input id="username" name="username" type="text" required placeholder="Benutzer" class="rounded-b-none!" />
        </div>
        <div>
          <input id="mail" name="mail" type="email" required placeholder="Mailadresse" class="rounded-none!" />
        </div>
        <div>
          <input id="firstname" name="firstname" type="text" required placeholder="Vorname" class="rounded-none!" />
        </div>
        <div>
          <input id="lastname" name="lastname" type="text" required placeholder="Nachname" class="rounded-t-none!" />
        </div>
      </div>
      <div class="flex flex-row gap-2">
        <button primary type="submit" :disabled="status == 'loading' || status?.status == 'success'">
          Nutzer einladen
        </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 { useInviteStore } from "@/stores/admin/management/invite";
import type { CreateInviteViewModel } from "@/viewmodels/admin/management/invite.models";
</script>

<script lang="ts">
export default defineComponent({
  data() {
    return {
      status: null as null | "loading" | { status: "success" | "failed"; reason?: string },
      timeout: undefined as any,
    };
  },
  beforeUnmount() {
    try {
      clearTimeout(this.timeout);
    } catch (error) {}
  },
  computed: {
    ...mapState(useModalStore, ["data"]),
  },
  methods: {
    ...mapActions(useModalStore, ["closeModal"]),
    ...mapActions(useInviteStore, ["createInvite"]),
    invite(e: any) {
      let formData = e.target.elements;
      let createInvite: CreateInviteViewModel = {
        username: formData.username.value,
        mail: formData.mail.value,
        firstname: formData.firstname.value,
        lastname: formData.lastname.value,
      };
      this.status = "loading";
      this.createInvite(createInvite)
        .then((result) => {
          this.status = { status: "success" };
          setTimeout(() => {
            this.closeModal();
          }, 2000);
        })
        .catch((err) => {
          this.status = { status: "failed", reason: err.response.data };
        });
    },
  },
});
</script>
