<script setup>
import { nextTick, ref } from "vue";
import { Head, useForm } from "@inertiajs/inertia-vue3";
import JetAuthenticationCard from "@/Jetstream/AuthenticationCard.vue";
import JetAuthenticationCardLogo from "@/Jetstream/AuthenticationCardLogo.vue";
import JetButton from "@/Jetstream/Button.vue";
import JetInput from "@/Jetstream/Input.vue";
import JetLabel from "@/Jetstream/Label.vue";
import JetValidationErrors from "@/Jetstream/ValidationErrors.vue";
import Layout from "@/Layouts/PromoSidebar";

const recovery = ref(false);

const form = useForm({
    code: "",
    recovery_code: "",
});

const recoveryCodeInput = ref(null);
const codeInput = ref(null);

const toggleRecovery = async () => {
    recovery.value ^= true;

    await nextTick();

    if (recovery.value) {
        recoveryCodeInput.value.focus();
        form.code = "";
    } else {
        codeInput.value.focus();
        form.recovery_code = "";
    }
};

const submit = () => {
    form.post(route("two-factor.login"));
};
</script>

<template>
    <Layout title="Two-factor Confirmation">
        <div
            class="container flex flex-col items-center justify-center flex-1 py-5"
        >
            <JetAuthenticationCard>
                <template #logo>
                    <JetAuthenticationCardLogo />
                </template>

                <div class="mb-4 text-sm text-gray-600">
                    <template v-if="!recovery">
                        Please confirm access to your account by entering the
                        authentication code provided by your authenticator
                        application.
                    </template>

                    <template v-else>
                        Please confirm access to your account by entering one of
                        your emergency recovery codes.
                    </template>
                </div>

                <JetValidationErrors class="mb-4" />

                <form @submit.prevent="submit">
                    <div v-if="!recovery">
                        <JetLabel for="code" value="Code" />
                        <JetInput
                            id="code"
                            ref="codeInput"
                            v-model="form.code"
                            autocomplete="one-time-code"
                            autofocus
                            class="block w-full mt-1"
                            inputmode="numeric"
                            type="text"
                        />
                    </div>

                    <div v-else>
                        <JetLabel for="recovery_code" value="Recovery Code" />
                        <JetInput
                            id="recovery_code"
                            ref="recoveryCodeInput"
                            v-model="form.recovery_code"
                            autocomplete="one-time-code"
                            class="block w-full mt-1"
                            type="text"
                        />
                    </div>

                    <div class="flex items-center justify-end mt-4">
                        <button
                            class="text-sm text-gray-600 underline !cursor-pointer hover:text-gray-900"
                            type="button"
                            @click.prevent="toggleRecovery"
                        >
                            <template v-if="!recovery">
                                Use a recovery code
                            </template>

                            <template v-else>
                                Use an authentication code
                            </template>
                        </button>

                        <JetButton
                            :class="{ 'opacity-25': form.processing }"
                            :disabled="form.processing"
                            class="ml-4"
                            @click="submit"
                        >
                            Log in
                        </JetButton>
                    </div>
                </form>
            </JetAuthenticationCard>
        </div>
    </Layout>
</template>
