<template>
    <div class="row">
        <div class="col">
            <div class="row" @keyup.enter="submitForm">
                <div class="col ml-md-3 ml-0 mt-md-0 mt-3 bg-white padding-15">
                    <loading-component style="height: 200px; top: 0;" key="1" color="success" v-show="isLoading"></loading-component>
                    <div class="row" v-show="!isLoading">
                        <div class="col">
                            <div class="row m-b-10">
                                <div class="col">
                                    <div class="font-heading fs-16 all-caps bold m-b-15">Upload Debtors</div>
                                </div>
                            </div>
                            <error-message-component class="m-b-20" :error="error"></error-message-component>
                            <div class="row">
                                <div class="col">
                                    <file-input-component :validator="v$.files" v-model="files">
                                        <template #label>
                                            <div class="font-heading fs-11 text-primary all-caps">Debtor Excel</div>
                                        </template>
                                    </file-input-component>
                                </div>
                            </div>
                            <div class="row m-t-20">
                                <div class="col">
                                    <div class="row">
                                        <div class="col">
                                            <button type="button" class="btn btn-block p-t-10 p-b-10 p-r-35 p-l-35 btn-success b-rad-none" @click="submitForm">Upload & Update</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row m-t-20">
                <div class="col ml-md-3 ml-0">
                    <div class="row">
                        <div class="col">
                            <button type="button" class="btn btn-block p-t-10 p-b-10 p-r-35 p-l-35 btn-primary b-rad-none" @click="downloadReport()">Download New Debtors Report</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

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

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

const crudStore = useCrudStore();
const { submit, error, isLoading } = useFormHandler(props);

const files = ref<any[]>([]);

const rules = {
    files: { required }
};

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

const submitForm = () => {
    const parameters = {
        files: files.value
    };

    submit(route('api.debtor.import'), 'post', props.section, true, false, parameters);
};

const downloadReport = () => {
    const url = route('newDebtor.export');
    if ((window as any).LARAVEL_VAPOR_ENABLED) {
        crudStore.crudRequest({ endpoint: url, method: 'get' })
            .then((response: Response) => {
                const success = response.ok;
                response.json().then((data: any) => {
                    if (!success) return;
                    if (data.src) {
                        window.open(data.src, '_blank');
                    }
                });
            });
    } else {
        window.open(url, '_blank');
    }
};
</script>
