<template>
    <div class="min-h-screen flex items-center justify-center">
        <div class="max-w-md w-full py-12 px-6">
            <img class="mx-auto h-24 w-auto" src="/favicon-lfk.png" alt />
            <h1 class="sm:text-3xl text-2xl font-semibold title-font mb-4 text-center font-[Athiti]">Lauf für Kaya! - {{
                $t('profile')
                }}</h1>
            <p class="mx-auto leading-relaxed text-base text-center">
                {{ $t('access_is_only_provided_via_your_email_link') }}
            </p>
            <div class="mt-6">
                <div class="relative">
                    <div class="absolute inset-0 flex items-center">
                        <div class="w-full border-t border-gray-300"></div>
                    </div>
                    <div class="relative flex justify-center text-sm">
                        <span class="px-2 bg-white dark:bg-gray-900">{{ $t('lost_your_registration_mail') }}</span>
                    </div>
                </div>
                <div class="mt-4">
                    <label for="email_address" class="block font-medium">
                        {{ $t('e_mail_adress') }}
                        <span class="font-bold">*</span>
                    </label>
                    <input v-model="user_email" name="email_address" id="email_address" autocomplete="email"
                        :placeholder="[[$t('e_mail_adress')]]" type="email"
                        :class="{ 'border-red-500': (!isEmail(user_email)), 'border-green-300': (isEmail(user_email)) }"
                        class="dark:bg-gray-800 mt-1 block w-full shadow-sm sm:text-sm border-2 bg-gray-50 text-gray-500 rounded-md p-2" />
                    <p v-if="!isEmail(user_email) && user_email !== ''" class="text-sm">{{
                        $t('please_provide_valid_mail')
                    }}</p>
                </div>
                <div class="mt-2">
                    <button :disabled="(!state.submit_enabled)"
                        :class="{ 'opacity-50': (!state.submit_enabled), 'cursor-not-allowed': (!state.submit_enabled) }"
                        @click="resendMail"
                        class="block w-full text-center py-2 px-3 border-2 border-gray-300 rounded-md p-1 dark:bg-gray-800 font-medium hover:border-gray-400 focus:outline-none focus:border-gray-400 sm:text-sm">{{
                            $t('resend_the_registration_mail') }}</button>
                </div>
            </div>
            <div class="mt-12">
                <div class="relative">
                    <div class="absolute inset-0 flex items-center">
                        <div class="w-full border-t border-gray-300"></div>
                    </div>
                    <div class="relative flex justify-center text-sm">
                        <span class="px-2 bg-white dark:bg-gray-900">{{ $t('not_registered_yet') }}</span>
                    </div>
                </div>
                <div class="mt-2">
                    <a href="/register/"
                        class="text-white block w-full text-center py-2 px-3 border-2 border-gray-300 rounded-md p-1 bg-blue-800 font-medium hover:border-gray-400 focus:outline-none focus:border-gray-400 sm:text-sm">{{
                            $t('register_now_small') }}</a>
                </div>
            </div>
        </div>
    </div>
    <Footer></Footer>
</template>

<script setup>
import { computed, ref, reactive } from "vue";
import axios from "redaxios";
import isEmail from 'validator/es/lib/isEmail';
import { TYPE, useToast } from "vue-toastification";
import Footer from "@/components/Footer.vue";
import { useI18n } from 'vue-i18n'
const { t } = useI18n()

let user_email = ref("");
// 
const state = reactive({
    org_name: "",
    org_teams: [],
    submit_enabled: computed(() => isEmail(user_email.value))
})
const toast = useToast();
function resendMail() {
    if (isEmail(user_email.value)) {
        toast(t('login_link_is_requested'));
        const browserlocale = ((navigator.languages && navigator.languages[0]) || '').substr(0, 2);
        axios.post(`${config.baseurl}api/runners/login?mail=${user_email.value}&locale=${browserlocale}`)
            .then(({ data }) => {
                console.log(data);
                toast(t('login_link_gesendet_an_user_email_value') + user_email.value);
            })
            .catch((error) => {
                console.log(error);
                toast(t('error_requesting_the_login_link'), { type: TYPE.ERROR });
            });
    }
}
</script>