<template>
  <div class="grow flex items-center justify-center py-12 px-4 sm:px-6 lg:px-8">
    <div class="max-w-md w-full space-y-8 pb-20">
      <div class="flex flex-col items-center gap-4">
        <img src="/Logo.png" alt="LOGO" class="h-auto w-full" />
        <h2 class="text-center text-4xl font-extrabold text-gray-900">TOTP zurücksetzen</h2>
      </div>

      <form class="flex flex-col gap-2" @submit.prevent="reset">
        <div class="-space-y-px">
          <div>
            <input id="username" name="username" type="text" required placeholder="Benutzer" />
          </div>
        </div>
        <RouterLink to="/" class="w-fit self-end text-primary">zum Login</RouterLink>

        <div class="flex flex-row gap-2">
          <button type="submit" primary :disabled="resetStatus == 'loading' || resetStatus == 'success'">
            TOTP zurücksetzen
          </button>
          <Spinner v-if="resetStatus == 'loading'" class="my-auto" />
          <SuccessCheckmark v-else-if="resetStatus == 'success'" />
          <FailureXMark v-else-if="resetStatus == 'failed'" />
        </div>
        <p v-if="resetMessage" class="text-center">{{ resetMessage }}</p>
      </form>

      <FormBottomBar />
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineComponent } from "vue";
import Spinner from "@/components/Spinner.vue";
import SuccessCheckmark from "@/components/SuccessCheckmark.vue";
import FailureXMark from "@/components/FailureXMark.vue";
import FormBottomBar from "@/components/FormBottomBar.vue";
</script>

<script lang="ts">
export default defineComponent({
  data() {
    return {
      resetStatus: undefined as undefined | "loading" | "success" | "failed",
      resetMessage: "" as string,
    };
  },
  methods: {
    reset(e: any) {
      let formData = e.target.elements;
      this.resetStatus = "loading";
      this.resetMessage = "";
      this.$http
        .post(`/reset`, {
          username: formData.username.value,
        })
        .then((result) => {
          this.resetStatus = "success";
          this.resetMessage = "Sie haben einen Verifizierungslink per Mail erhalten.";
        })
        .catch((err) => {
          this.resetStatus = "failed";
          this.resetMessage = err.response.data;
        });
    },
  },
});
</script>
