<template>
  <client-presentation
    :client-data="clientStore.clients"
    :loading-data="clientStore.loading"
    :pagination="clientStore.getPagination"
    @push-details="goDetails"
    @delete-client="handleDeleteClient"
  />
</template>
<script setup>
import ClientPresentation from "@/components/Organism/CRM/ClientPresentation.vue";
import { useClientStore } from "@/stores/clientStore";
import { useNotificationStore } from "@/stores/notification";
import { onMounted } from "vue";
import { useRouter } from "vue-router";

const clientStore = useClientStore();
const router = useRouter();
const notificationStore = useNotificationStore();

onMounted(async () => {
  await clientStore.fetchClients();
});

const goDetails = (id) => {
  router.push({
    name: "Client details",
    params: {
      id: id,
    },
  });
};

const handleDeleteClient = async (clientId) => {
  try {
    await clientStore.deleteClient(Number(clientId));
    await clientStore.fetchClients();
    notificationStore.deleted("Client");
  } catch (error) {
    console.error("Error deleting client:", error);
    notificationStore.error("Erreur", "Impossible de supprimer le client");
  }
};
</script>
