<template>
    <div class="row">
        <div class="col">
            <div class="row" v-show="!uiStore.isShowing('resetPasswordSuccess')">
                <div class="col">
                    <div class="row m-b-15">
                        <div class="col">
                            <div class="inline v-align-middle">
                                <div class="font-heading fs-20 all-caps text-primary no-margin lh-28 bold">Reset your password</div>
                            </div>
                        </div>
                    </div>
                    <div class="row m-r-0 m-l-0 m-b-20" v-if="error">
                        <div class="col p-l-50">
                            <small class="bold fs-10 text-danger">{{ error }}</small>
                        </div>
                    </div>
                    <validation-wrapper-component class="m-b-15" :validator="v$.parameters.password">
                        <label class="text-primary">Password</label>
                        <div class="controls">
                            <input type="password" class="form-control fs-12" v-model="parameters.password" @blur="v$.parameters.password.$touch()">
                        </div>
                    </validation-wrapper-component>
                    <validation-wrapper-component class="m-b-15" :validator="v$.parameters.confirmPassword">
                        <label class="text-primary">Confirm Password</label>
                        <div class="controls">
                            <input type="password" class="form-control fs-12" v-model="parameters.confirmPassword" @blur="v$.parameters.confirmPassword.$touch()">
                        </div>
                    </validation-wrapper-component>
                    <div class="row align-items-center">
                        <div class="col"></div>
                        <div class="col-auto">
                            <div
                                class="btn btn-lg b-rad-none btn-success"
                                @click="submitForm"
                                :disabled="uiStore.isLoading(props.section)"
                            >
                                Reset Password
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row" v-show="uiStore.isShowing('resetPasswordSuccess')">
                <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 Changed successfully</h4>
                        </div>
                    </div>
                    <div class="row m-b-10">
                        <div class="col">
                            <small class="font-lato">You have successfully changed your password, now you can login into your account using your new password.</small>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col">
                            <a :href="loginUrl">
                                <div class="btn btn-sm btn-default b-rad-none light" style="letter-spacing: 2px;">
                                    <i class="fa fa-angle-left" style="padding-right: 18px;"></i>Go to login
                                </div>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { computed, reactive } from 'vue';
import { useVuelidate } from '@vuelidate/core';
import { required } from '@vuelidate/validators';
import { useErrorMessageHandler } from '@/composables/useErrorMessageHandler';
import { useRequest } from '@/composables/useRequest';
import { useUiStore } from '@/stores/ui';
import route from '@/general/functions/router';

interface Props {
    section: string;
    token: string;
}

const props = defineProps<Props>();

const uiStore = useUiStore();
const { error, errorMessageHandler } = useErrorMessageHandler();
const { submit } = useRequest();
const rules = {
    parameters: {
        password: { required },
        confirmPassword: { required },
        token: { required }
    }
};


const parameters = reactive({
    password: '',
    confirmPassword: '',
    token: props.token,
    email: '' // Added as per legacy data structure though not in validaitons
});

const v$ = useVuelidate(rules, { parameters });

const loginUrl = computed(() => route('login'));

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

    await submit(
        route('api.account.authentication.password.reset'),
        'post',
        props.section,
        false, // No success notification
        false, // No default error notification
        parameters,
        {
            successHandler: () => {
                uiStore.toggleSection('resetPasswordSuccess', true);
                errorMessageHandler('');
            },
            errorHandler: (response) => {
                errorMessageHandler(response.message);
            }
        }
    );
};
</script>
