<template>
    <div class="row">
        <div class="col">
            <loading-component style="height: 300px; top: 0;" key="1" color="success" v-show="isLoading" ></loading-component>
            <div class="row justify-content-center" v-show="!isLoading">
                <div class="col">
                    <div class="row m-b-20">
                        <div class="col">
                            <h3 class="all-caps">Are you Sure?</h3>
                            <div class="fs-11">{{ contentText }}</div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col p-r-5">
                            <div class="btn btn-sm btn-default btn-block bg-master-lighter" data-dismiss="modal">Cancel</div>
                        </div>
                        <div class="col p-l-5">
                            <div class="btn btn-sm btn-block b-rad-none" :class="[{'btn-danger': modalType !== 'confirm'}, {'btn-success': modalType === 'confirm'}]" @click="submitForm()">{{ buttonText }}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { useFormHandler } from '@/composables/useFormHandler';
import { useUiStore } from '@/stores/ui';
import { useQueueStore } from '@/stores/queue';

const props = withDefaults(defineProps<{
    contentText: string;
    params?: any;
    modalType?: string;
    buttonText?: string;
    apiRoute: string;
    apiMethod: string;
    section?: string;
}>(), {
    modalType: 'confirm',
    buttonText: 'Confirm'
});

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

const successHandler = () => {
    closeModal();
    formHandler();

    if (props.section && ['paymentInProgressBillGroupList', 'paymentVerificationBillGroupList', 'paidBillGroupList'].includes(props.section)) {
        uiStore.toggleSection('paymentInProgressBillGroupList', !uiStore.isShowing('paymentInProgressBillGroupList'));
        uiStore.toggleSection('paymentVerificationBillGroupList', !uiStore.isShowing('paymentVerificationBillGroupList'));
        uiStore.toggleSection('paidBillGroupList', !uiStore.isShowing('paidBillGroupList'));
    }

    if(props.section && ['bookingDetailPaymentHistorySection'].includes(props.section)) {
        queueStore.reloadList({ name: "bookingDetailSection" });
    }
};

const submitForm = () => {
    if (props.params) {
        parameters.value = props.params;
    }
    return submit(props.apiRoute, props.apiMethod, props.section, true, true, undefined, { successHandler });
};
</script>
