<template>
    <div class="row">
        <div class="col">
            <div class="row" @keyup.enter="submitForm">
                <div class="col">
                    <loading-component style="height: 200px; top: 0;" key="1" color="success" v-show="uiStore.isLoading(section)"></loading-component>
                    <div class="row" v-show="!uiStore.isLoading(section)">
                        <div class="col">
                            <div class="row">
                                <div class="col">
                                    <file-input-component :validator="v$.parameters.files" v-model="parameters.files">
                                        <template #label>
                                            <div class="font-heading fs-11 text-primary all-caps">Import Bank Statement CSV</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-sm btn-block 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>
        </div>
    </div>
</template>

<script setup lang="ts">
    import { reactive } from 'vue';
    import { useFormHandler } from '@/composables/useFormHandler';
    import { useUiStore } from '@/stores/ui';
    import { useVuelidate } from '@vuelidate/core';
    import { required } from "@vuelidate/validators";

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

    const { submit } = useFormHandler(props);
    const uiStore = useUiStore();

    const parameters = reactive({
        files: []
    });

    const rules = {
        parameters: {
            files: {
                required
            }
        }
    };

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

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

        submit(
            (window as any).route('api.accounting.statement.import'),
            'post',
            props.section,
            true,
            true,
            parameters,
            {
                successHandler: () => {
                    parameters.files = [];
                    v$.value.$reset();
                }
            }
        );
    };
</script>

