<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 Booking Amount</h3>
                        </div>
                    </div>
                    <div class="row m-b-10">
                        <div class="col">
                            <validation-wrapper-component :validator="v$.fix_amount">
                                <label class="muted">Booking Amount ({{data.fixed_currency.short_code}})</label>
                                <input type="text" class="form-control" v-model.lazy="fix_amount" 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 fix_amount = ref((Math.round((props.data.amount + Number.EPSILON) * 100) / 100).toFixed(2));

const rules = {
    fix_amount: { required }
};

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

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

watch(() => props.data, (val) => {
    fix_amount.value = (Math.round((val.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(fix_amount.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 = { fix_amount: enteredAmount };
    submit(route('api.booking.booking_amount.update', props.data.id), 'put', props.section, true, true, parameters.value, {
        successHandler: () => {
            closeModal();
            formHandler();
        }
    });
};
</script>
