<template>
    <SMPage>
        <SMRow>
            <SMFormCard narrow>
                <template v-if="!formDone">
                    <h1>Reset Password</h1>
                    <SMForm v-model="form" @submit="handleSubmit">
                        <SMInput control="code" />
                        <SMInput control="password" type="password" />
                        <SMButtonRow>
                            <template #left>
                                <div class="small">
                                    <router-link
                                        :to="{ name: 'forgot-password' }"
                                        >Resend Code</router-link
                                    >
                                </div>
                            </template>
                            <template #right>
                                <SMButton
                                    type="submit"
                                    label="Reset Password"
                                    icon="arrow-forward-outline" />
                            </template>
                        </SMButtonRow>
                    </SMForm>
                </template>
                <template v-else>
                    <h1>Password Reset!</h1>
                    <p class="text-center">
                        Hurrah, Your password has been changed!
                    </p>
                    <SMButtonRow>
                        <template #right>
                            <SMButton :to="{ name: 'login' }" label="Login" />
                        </template>
                    </SMButtonRow>
                </template>
            </SMFormCard>
        </SMRow>
    </SMPage>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
// import { useReCaptcha } from "vue-recaptcha-v3";
import { useRoute } from "vue-router";
import SMButton from "../components/SMButton.vue";
import SMFormCard from "../components/SMFormCard.vue";
import SMForm from "../components/SMForm.vue";
import SMButtonRow from "../components/SMButtonRow.vue";
import SMInput from "../components/SMInput.vue";
import { api } from "../helpers/api";
import { Form, FormControl } from "../helpers/form";
import { And, Max, Min, Password, Required } from "../helpers/validate";

// const { executeRecaptcha, recaptchaLoaded } = useReCaptcha();
const formDone = ref(false);
let form = reactive(
    Form({
        code: FormControl("", And([Required(), Min(6), Max(6)])),
        password: FormControl("", And([Required(), Password()])),
    })
);

if (useRoute().query.code !== undefined) {
    let queryCode = useRoute().query.code;
    if (Array.isArray(queryCode)) {
        queryCode = queryCode[0];
    }

    form.controls.code.value = queryCode;
}

const handleSubmit = async () => {
    form.loading(true);

    try {
        // await recaptchaLoaded();
        // const captcha = await executeRecaptcha("submit");

        await api.post({
            url: "/users/resetPassword",
            body: {
                code: form.controls.code.value,
                password: form.controls.password.value,
                // captcha_token: captcha,
            },
        });

        formDone.value = true;
    } catch (error) {
        form.apiErrors(error);
    } finally {
        form.loading(false);
    }
};
</script>
