<template>
    <div class="row">
       <div class="col bg-white padding-40 b-rad-lg">
            <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 Booking Amount</h3>
                            <p>To ensure both updates take effect, please update the purchase order details with the equivalent booking amount immediately after this update. If not done in sequence, neither update will be applied.</p>
                        </div>
                    </div>
                    <div class="row m-b-10">
                        <div class="col">
                            <validation-wrapper-component :validator="v$.amount_to_edit">
                                <label class="muted">Booking Amount ({{data.fixed_currency.short_code}})</label>
                                <input type="text" class="form-control" v-model.lazy="amount_to_edit" v-money3="money">
                            </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()">Update</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import { useFormHandler } from '@/composables/useFormHandler';
import { useQueueStore } from '@/stores/queue';
import useVuelidate from '@vuelidate/core';
import { required } from '@vuelidate/validators';

const props = defineProps<{
    data: any;
    section: string;
}>();

const { submit, closeModal, error, parameters, formHandler } = useFormHandler(props);
const queueStore = useQueueStore();

const amount_to_edit = ref((Math.round((props.data.paid_amount + Number.EPSILON) * 100) / 100).toFixed(2));

const rules = {
    amount_to_edit: { required }
};

const money = {decimal: '.',thousands: ',', precision: 2};

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

// watch(() => props.data, (val) => {
//     amount_to_edit.value = (Math.round((val.paid_amount + Number.EPSILON) * 100) / 100).toFixed(2);
// }, { deep: true });

const submitForm = async () => {
    error.value = '';

    const isFormCorrect = await v$.value.$validate();
    if (!isFormCorrect) return;

    const enteredAmount = parseFloat(amount_to_edit.value.toString().replace(/,/g, ''));
    const paidAmount = parseFloat(props.data.paid_amount);

    if (enteredAmount < paidAmount) {
        error.value = `Booking amount cannot be less than paid amount (${paidAmount.toFixed(2)}).`;
        return;
    }

    parameters.value = { amount_to_edit: enteredAmount };
    submit(route('api.booking.amount.update', props.data.id), 'put', props.section, true, true, parameters.value, {
        successHandler: () => {
            closeModal();
            formHandler();
            queueStore.reloadList({ name: "bookingDetailSection" });
        }
    });
};
</script>
