<template>
    <div class="row">
        <div class="col">
            <div class="row m-b-5" @keyup.enter="submitSearch">
                <div class="col p-r-0">
                    <validation-wrapper-component :validator="v$.parameters.marking">
                        <label class="all-caps">Marking</label>
                        <input type="text" class="form-control" v-model="parameters.marking" :class="[{ 'not-allowed': hasMarking }]" :disabled="hasMarking">
                    </validation-wrapper-component>
                </div>
                <div class="col p-r-0">
                    <validation-wrapper-component :validator="v$.parameters.startDate">
                        <label class="all-caps">Start Date</label>
                        <date-picker-component v-model.lazy="parameters.startDate"></date-picker-component>
                    </validation-wrapper-component>
                </div>
                <div class="col p-r-0">
                    <validation-wrapper-component :validator="v$.parameters.endDate">
                        <label class="all-caps">End Date</label>
                        <date-picker-component v-model.lazy="parameters.endDate"></date-picker-component>
                    </validation-wrapper-component>
                </div>
                <div class="col col-md-auto d-flex justify-content-center align-items-center">
                    <button type="button" class="btn btn-lg btn-primary fs-11 w-100" @click="submitSearch">Download</button>
                </div>
            </div>
            <loading-component style="height: 100px; top: 0;" key="1" color="success" v-show="isLoading"></loading-component>
            <div class="row align-items-center justify-content-center bg-white p-t-50 p-b-50 p-l-15 p-r-15 margin-15" v-if="!isLoading && returnData">
                <div class="col-10">
                    <div class="row align-items-center justify-content-center hint-text">
                        <div class="col-4 hint-text"><img :src="asset('images/not-found-illustration.png')" class="w-100 hint-text" /></div>
                    </div>
                    <div class="row text-center">
                        <div class="col">
                            <div class="row m-t-20">
                                <div class="col">
                                    <p class="all-caps no-margin fs-11" :class="[{ 'text-danger': returnData.status === 'Error' }]" style="letter-spacing: 2px;">{{ returnData.message }}</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

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

interface Props {
    section: string;
}

const props = defineProps<Props>();

const { parameters, submit, isLoading } = useFormHandler({
    section: props.section,
    data: null
});

Object.assign(parameters.value, {
    marking: null as string | null,
    startDate: null as string | null,
    endDate: null as string | null
});

const returnData = ref<Record<string, any> | null>(null);
const hasMarking = ref(false);

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

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

const asset = (path: string) => (window as any).Vapor.asset(path);

const setMarkingFromQuery = () => {
    const urlSearchParams = new URLSearchParams(window.location.search);
    if (urlSearchParams.has('marking')) {
        parameters.value.marking = urlSearchParams.get('marking');
    }
    hasMarking.value = urlSearchParams.has('marking');
};

onMounted(() => {
    setMarkingFromQuery();
});

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

    submit(route('api.customers.invoices'), 'post', props.section, false, false, parameters.value, {
        successHandler: (response: any) => {
            if (response.message) {
                returnData.value = response;
            } else {
                returnData.value = null;
                if (window.LARAVEL_VAPOR_ENABLED && response.src) {
                    window.open(response.src, '_blank');
                }
            }
        },
        errorHandler: (response: any) => {
            console.log(response);
        }
    });
};
</script>
