<template>
    <div class="row" @keyup.enter="submitForm">
        <div class="col">
            <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: {{ reportType }}</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">.xls file ONLY</div>
                                </template>
                                <template #tips>
                                    <div class="row">
                                        <div class="col">
                                            <div class="row m-b-10">
                                                <div class="col">
                                                    <div class="font-heading fs-11 text-warning m-b-10">Please double check the Report Type selected before proceed upload.</div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </template>
                            </file-input-component>
                        </div>
                    </div>
                    <div class="row m-t-20">
                        <div class="col">
                            <div class="row">
                                <div class="col-auto">
                                    <button type="button" class="btn btn-sm bg-master-lighter p-t-10 p-b-10 p-r-35 p-l-35 btn-default b-rad-none" data-dismiss="modal">Cancel</button>
                                </div>
                                <div class="col text-right">
                                    <button type="button" class="btn btn-sm p-t-10 p-b-10 p-r-35 p-l-35 btn-success b-rad-none" @click="submitForm">Upload</button>
                                </div>
                            </div>
                        </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 { useFormHandler } from '@/composables/useFormHandler';
import route from '@/general/functions/router';

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

const emit = defineEmits<{
    (e: 'importSuccess', value: any): void;
}>();

const { submit, error, isLoading, closeModal, formHandler } = useFormHandler(props);

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

const rules = {
    files: { required }
};

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

const submitForm = () => {
    const parameters = {
        files: files.value,
        report_type: props.reportType
    };

    submit(props.url, 'post', props.section, true, true, parameters, {
        successHandler: (response: any) => {
            closeModal();
            formHandler();
            if (props.url === route('api.import.official_receipt')) {
                emit('importSuccess', response.payload);
            }
        }
    });
};
</script>
