<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-36" />
        <h2 class="text-center text-4xl font-extrabold text-gray-900">TOTP zurücksetzen</h2>
      </div>

      <div v-if="verification == 'loading'" class="flex flex-col gap-2 items-center">
        <p class="w-fit">Rücksetz-Link wird verifiziert</p>
        <Spinner class="my-auto" />
      </div>
      <div v-else-if="verification == 'failed'" class="flex flex-col gap-2 items-center">
        <p class="w-fit">Rücksetz-Link nicht gültig</p>
        <RouterLink to="/reset" class="text-primary">Zum zurücksetzen Start</RouterLink>
      </div>
      <form v-else class="flex flex-col gap-2" @submit.prevent="reset">
        <img :src="image" alt="totp" class="w-56 h-56 self-center" />

        <TextCopy :copyText="otp" />

        <div class="-space-y-px">
          <div>
            <input id="totp" name="totp" type="text" required placeholder="TOTP" />
          </div>
        </div>

        <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="resetError" class="text-center">{{ resetError }}</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 { RouterLink } from "vue-router";
import { ClipboardIcon } from "@heroicons/vue/24/outline";
import FormBottomBar from "@/components/FormBottomBar.vue";
import TextCopy from "@/components/TextCopy.vue";
</script>

<script lang="ts">
export default defineComponent({
  props: {
    token: String,
    mail: String,
  },
  data() {
    return {
      verification: "loading" as "success" | "loading" | "failed",
      image: undefined as undefined | string,
      otp: undefined as undefined | string,
      resetStatus: undefined as undefined | "loading" | "success" | "failed",
      resetError: "" as string,
    };
  },
  mounted() {
    this.$http
      .post(`/reset/verify`, {
        token: this.token,
        mail: this.mail,
      })
      .then((result) => {
        setTimeout(() => {
          this.verification = "success";
          this.image = result.data.dataUrl;
          this.otp = result.data.otp;
        }, 1000);
      })
      .catch((err) => {
        setTimeout(() => {
          this.verification = "failed";
        }, 1000);
      });
  },
  methods: {
    reset(e: any) {
      let formData = e.target.elements;
      this.resetStatus = "loading";
      this.resetError = "";
      this.$http
        .put(`/reset`, {
          token: this.token,
          mail: this.mail,
          totp: formData.totp.value,
        })
        .then((result) => {
          this.resetStatus = "success";
          localStorage.setItem("accessToken", result.data.accessToken);
          localStorage.setItem("refreshToken", result.data.refreshToken);
          setTimeout(() => {
            this.$router.push(`/admin`);
          }, 1000);
        })
        .catch((err) => {
          this.resetStatus = "failed";
          this.resetError = err.response.data;
        });
    },
  },
});
</script>
