<template>
    <div class="row">
        <div class="col bg-white padding-40 b-rad-lg">
            <loading-component style="height: 300px; top: 0;" key="1" color="success" v-show="isLoading" />
            <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">Do you need E-Invoice?</h3>
                            <div class="fs-11">You won't be able to request one after clicking 'No'.</div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col p-r-5">
                            <div class="btn btn-sm btn-block b-rad-none btn-danger" @click="handleChoice(false)">No</div>
                        </div>
                        <div class="col p-l-5">
                            <div class="btn btn-sm btn-block b-rad-none btn-success" @click="handleChoice(true)">Yes</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { useFormHandler } from '@/composables/useFormHandler';
import route from '@/general/functions/router';

interface Props {
    section?: string;
    companyId: number;
}

const props = defineProps<Props>();
const emit = defineEmits(['choice-made']);

const { parameters, submit, isLoading, closeModal, formHandler } = useFormHandler({
    section: props.section,
    data: {
        company_id: props.companyId,
        e_invoice_request: false
    }
});

const handleChoice = (choice: boolean) => {
    parameters.value.e_invoice_request = choice;
    emit('choice-made', { choice, params: parameters.value });
    submitForm();
};

const submitForm = () => {
    submit(route('api.company.einvoice.request.update'), 'post', props.section, true, true, parameters.value, {
        successHandler: () => {
            closeModal();
            formHandler();
        }
    });
};
</script>
