<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" v-if="!returnData">
                        <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 Honey Trap Csv</div>
                                </div>
                            </div>
                            <error-message-component class="m-b-20" :error="error"></error-message-component>
                            <div class="row m-b-15">
                                <div class="col">
                                    <validation-wrapper-component selectable :validator="v$.parameters.segment_id">
                                        <label>Segment</label>
                                        <selectable-component :endpoint="route('api.segment.list')" section="segmentsListSection_" valueColumn="id" :labelColumn="['name']" v-model="parameters.segment_id"></selectable-component>
                                    </validation-wrapper-component>
                                </div>
                            </div>
                            <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">Honey Trap 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-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 v-if="returnData">
                        <div class="font-heading fs-16 all-caps bold m-b-15">Uploaded Customer</div>
                        <table class="w-100 table table-bordered">
                            <thead class="thead-dark">
                                <tr>
                                    <th class="text-capitalize" v-for="key in returnDataKeys" :key="key">{{ key }}</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr v-for="(row, rowIndex) in returnData" :key="rowIndex">
                                    <td v-for="(col, colIndex) in row" :key="colIndex" :class="[{ 'text-danger': row['status'] === 'failed' }]">
                                        {{ col }}
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { computed, 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, error } = useFormHandler({
    section: props.section,
    data: null
});

Object.assign(parameters.value, {
    files: [] as File[],
    segment_id: null as number | null
});

const returnData = ref<Record<string, any>[] | null>(null);

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

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

const returnDataKeys = computed(() => {
    if (!returnData.value || !returnData.value[0]) return [];
    return Object.keys({ ...returnData.value[0] });
});

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

    submit(route('api.honey_trap.upload'), 'post', props.section, true, false, parameters.value, {
        successHandler: (response: any) => {
            returnData.value = response;
        },
        errorHandler: () => {
            // Preserve legacy silent error flow while allowing global notifications.
        }
    });
};
</script>
