<template>
    <div class="row">
        <div class="col">
            <div class="row m-b-10 animate__animated animate__fadeInUpBig animate__fast" v-if="error">
                <div class="col">
                    <small class="bold fs-10 text-danger">{{ error }}</small>
                </div>
            </div>

            <div class="row" v-show="!uiStore.isShowing('forgetPasswordSuccess')">
                <div class="col animate__animated">
                    <div class="row align-items-center">
                        <div class="col">
                            <validation-wrapper-component class="m-b-15" :validator="v$.email">
                                <label class="text-primary">Email Address</label>
                                <div class="controls">
                                    <input type="email" class="form-control fs-12" v-model="state.email" @blur="v$.email.$touch()">
                                </div>
                            </validation-wrapper-component>
                        </div>
                    </div>
                </div>
            </div>

            <div class="row m-b-15" v-show="uiStore.isShowing('forgetPasswordSuccess')">
                <div class="col">
                    <div class="row m-b-5">
                        <div class="col">
                            <h4 class="font-lato text-success bold"><i class="fa fa-check-circle m-r-5"></i> Password Reset Email Sent</h4>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col">
                            <small class="font-lato">An email has been sent to your rescue email address. Follow the directions in the email to reset your password.</small>
                        </div>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col">
                    <div
                        class="btn btn-sm btn-default b-rad-none light"
                        @click="authStore.returnToLoginAfterSendResetPasswordLink()"
                        style="letter-spacing: 2px;"
                    >
                        <i class="fa fa-angle-left" style="padding-right: 18px;"></i>Back to login
                    </div>
                </div>
                <div class="col-auto" v-show="!uiStore.isShowing('forgetPasswordSuccess')">
                    <button
                        class="btn btn-sm btn-primary b-rad-none"
                        :disabled="uiStore.isLoading(props.section)"
                        @click="submitForm"
                    >
                        Request Reset Link
                    </button>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useVuelidate } from '@vuelidate/core';
import { required, email } from '@vuelidate/validators';
import { useUiStore } from '@/stores/ui';
import { useAuthStore } from '@/stores/auth';
import { useErrorMessageHandler } from '@/composables/useErrorMessageHandler';
import { useRequest } from '@/composables/useRequest';

interface Props {
    section: string;
}

const props = defineProps<Props>();

const uiStore = useUiStore();
const authStore = useAuthStore();
const { error, errorMessageHandler } = useErrorMessageHandler();
const { submit } = useRequest();

const state = reactive({
    email: ''
});

const rules = {
    email: { required, email }
};

const v$ = useVuelidate(rules, state);

const resetForm = () => {
    state.email = '';
    v$.value.$reset();
};

const submitForm = async () => {
    const isFormValid = await v$.value.$validate();
    if (!isFormValid) return;

    await submit(
        route('api.account.authentication.password.forget'),
        'post',
        props.section,
        false,
        false,
        { email: state.email },
        {
            successHandler: () => {
                uiStore.toggleSection('forgetPasswordSuccess', true);
                authStore.passwordResetEmail({ email: state.email });
                resetForm();
                errorMessageHandler('');
            },
            errorHandler: (response) => {
                errorMessageHandler(response.message);
            }
        }
    );
};
</script>
