<template>
    <div class="row">
        <div class="col">
            <div class="row">
                <div class="col">
                    <div class="row m-b-10">
                        <div class="col">
                            <h3 class="all-caps m-b-5 bold no-margin">Edit Full Name</h3>
                        </div>
                    </div>
                    <div class="row m-b-10">
                        <div class="col">
                            <validation-wrapper-component :validator="v$.parameters.name">
                                <label>Full Name</label>
                                <input type="text" class="form-control" v-model="parameters.name" @blur="v$.parameters.name.$touch()">
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row m-b-5 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">
                        <div class="col p-r-5">
                            <div class="btn btn-sm btn-default bg-master-lightest btn-block b-rad-none" data-dismiss="modal">Cancel</div>
                        </div>
                        <div class="col p-l-5">
                            <div
                                class="btn btn-sm btn-success btn-block b-rad-none"
                                @click="submitForm"
                                :disabled="uiStore.isLoading(props.section)"
                            >
                                Update
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

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

interface Props {
    data: any;
    section: string;
}

const props = defineProps<Props>();

const authStore = useAuthStore();
const uiStore = useUiStore();
const rules = {
    parameters: {
        name: { required }
    }
};

const parameters = reactive({
    name: ''
});

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

onMounted(() => {
    parameters.name = authStore.getUserName;
});

const { submit, closeModal, error, formHandler } = useFormHandler({ section: props.section });

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

    await submit(
        route('api.account.user.update', authStore.getUserId),
        'put',
        props.section,
        true,
        true,
        parameters,
        {
            successHandler: (response: any)  => {
                if (response?.payload?.data?.name) {
                    authStore.updateProfile({ fullname:response.payload.data.name } );
                }
                closeModal();
                formHandler();
            }
        }
    );
};
</script>
