<template>
    <div class="resource-container service-container">
        <div class="header">
            <p>Service</p>
            <p>Namespace</p>
            <p>Type</p>
            <p>Aktionen</p>
        </div>
        <ServiceComponent :service="service" v-for="service, index in services" class="resource" :class="{ even: index % 2 }"></ServiceComponent>
    </div>
</template>

<script setup lang="ts">
import { useNamespaceStore } from '#imports';
import type { Namespace } from '~/classes/Namespace';
import type { Service } from '~/classes/Service';
import { getServices } from '~/requests/services';

const services: Ref<Service[] | undefined> = ref(undefined);

const namespace = computed(useNamespaceStore().getCurrentNamespace);

let interval: NodeJS.Timeout | undefined = undefined;
onMounted(() => {
    watch(namespace, (newNamespace) => {
        loadServices(newNamespace);
        clearInterval(interval);
        interval = setInterval(() => {
            loadServices(newNamespace);
        }, 10000);
    }, { immediate: true })
})
onUnmounted(() => {
    clearInterval(interval);
})

function loadServices(namespace?: Namespace)
{
    getServices(namespace?.metadata?.name, (_services: Service[]) => {
        services.value = _services;
    });
}
</script>

<style>
.service-container {
    grid-template-columns: auto auto 1fr auto;
}
.action-buttons {
    display: flex;
    gap: 0.5rem;
}
</style>