<template>
    <div class="row m-b-15">
        <div class="col-12">
            <div class="row">
                <div class="col-12">
                    <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 selectable :validator="v$.parameters.supplier">
                                <label>Supplier</label>
                                <selectable-component :disableOnFetch="true" :endpoint="route('api.company.list') + '?filters=' + JSON.stringify({'business_type': 3, 'status_in': [1, 2, 0]})" section="exportSupplierFilterSection" valueColumn="id" :labelColumn="['name']" v-model="parameters.supplier"></selectable-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.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-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="bulkDownloadWhiteForm()">
                                <span>Export</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <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="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 { ref } 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 returnData = ref<any>(null);
const parameters = ref({
    startDate: '',
    endDate: '',
    supplier: ''
});

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

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

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

    submit(route('api.suppliers.white_forms'), '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) {
                    if (response.src) {
                        window.open(response.src, '_blank');
                    }
                }
            }
        }
    });
};
</script>
