<template>
    <div class="row m-b-20">
        <div class="col">
            <div class="row">
                <div class="col">
                    <div class="row m-l-0 m-r-0">
                        <div class="col-12 col-md mb-2 mb-md-0 p-l-3 p-r-3 p-md-0">
                            <validation-wrapper-component :validator="v$.parameters.startDate">
                                <label class="all-caps">Start Date</label>
                                <date-picker-component :parameters="parameters" v-model.lazy="parameters.startDate"></date-picker-component>
                            </validation-wrapper-component>
                        </div>
                        <div class="col-12 col-md mb-2 mb-md-0 p-l-3 p-r-3 p-md-0">
                            <validation-wrapper-component :validator="v$.parameters.endDate">
                                <label class="all-caps">End Date</label>
                                <date-picker-component :parameters="parameters" v-model.lazy="parameters.endDate"></date-picker-component>
                            </validation-wrapper-component>
                        </div>
                        <div class="col-12 col-md mb-2 mb-md-0 p-l-3 p-r-3 p-md-0">
                            <validation-wrapper-component selectable :validator="v$.parameters.reportType">
                                <label class="all-caps">Report Type</label>
                                <select-component :options="reportTypeOptions" v-model="parameters.reportType"></select-component>
                            </validation-wrapper-component>
                        </div>
                        <div class="col-12 col-md-auto p-l-3 p-r-3 p-md-0">
                            <div class="btn btn-lg btn-primary fs-11 w-100 h-100 d-flex justify-content-center align-items-center" @click="downloadReport()">
                                <span>Export</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue';
import { useRequest } from '@/composables/useRequest';
import { useVuelidate } from '@vuelidate/core';
import { required } from '@vuelidate/validators';
import route from '@/general/functions/router';

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

const parameters = ref({
    startDate: '',
    endDate: '',
    reportType: '',
});

const rules = {
    parameters: {
        startDate: { required },
        endDate: { required },
        reportType: { required },
    }
};

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

onMounted(() => {
    switch (props.section) {
        case 'paymentsReportSection':
            parameters.value.reportType = 'Payments Report';
            break;
        case 'walletsReportSection':
            parameters.value.reportType = 'Wallets Report';
            break;
    }
});

const reportTypeOptions = computed(() => [
    { id: 'Payments Report', text: 'Payments Report' },
    { id: 'White Form Report', text: 'White Form Report' },
    { id: 'Wallet Top Up Report', text: 'Wallet Top Up Report' },
    { id: 'Wallet Payment Report', text: 'Wallet Payment Report' },
    { id: 'Wallet Credit Note Report', text: 'Wallet Credit Note Report' },
    { id: 'Wallet Debit Note Report', text: 'Wallet Debit Note Report' },
]);

const successHandler = (response: any) => {
    if (window.LARAVEL_VAPOR_ENABLED) {
        if (response.src) {
            window.open(response.src, '_blank');
        }
    }
};

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

    const reportType = parameters.value.reportType;
    const walletTypes: Record<string, string> = {
        'Wallet Payment Report': 'payment',
        'Wallet Credit Note Report': 'credit_note',
        'Wallet Debit Note Report': 'debit_note',
    };

    const routesMap: Record<string, string> = {
        'Payments Report': route('paymentTransactions.export'),
        'White Form Report': route('whiteFormTransactions.export'),
        'Wallet Top Up Report': route('walletTransactions.export'),
        'Wallet Payment Report': route('walletTransactions.export'),
        'Wallet Credit Note Report': route('walletTransactions.export'),
        'Wallet Debit Note Report': route('walletTransactions.export'),
    };

    let url = `${routesMap[reportType]}?startDate=${parameters.value.startDate}&endDate=${parameters.value.endDate}`;

    if (walletTypes[reportType]) {
        url += `&type=${walletTypes[reportType]}`;
    }

    if (window.LARAVEL_VAPOR_ENABLED) {
        submit(url, 'get', props.section, false, false, parameters.value, { successHandler });
    } else {
        window.open(url, '_blank');
    }
};
</script>
