<template>
    <div class="resource-container deployment-container">
        <div class="header">
            <p>Name</p>
            <p>Namespace</p>
            <p>Replicas</p>
            <p>Aktionen</p>
        </div>
        <DeploymentComponent :deployment="deployment" v-for="deployment, index in deployments" class="resource" :class="{ even: index % 2 }"></DeploymentComponent>
    </div>
</template>

<script setup lang="ts">
import { Deployment } from '~/classes/Deployment';
import { useNamespaceStore } from '#imports';
import type { Namespace } from '~/classes/Namespace';
import { getDeployments } from '~/requests/deployments';
import DeploymentComponent from '~/components/deployments/DeploymentComponent.vue';

const deployments: Ref<Deployment[] | undefined> = ref(undefined);

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

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

function loadDeployments(namespace?: Namespace)
{
    getDeployments(namespace?.metadata?.name, (_deployments: Deployment[]) => {
        deployments.value = _deployments;
    });
}
</script>

<style scoped>
.deployment-container {
    grid-template-columns: 1fr 1fr 1fr auto;
}
</style>