<template>
  <header class="header">
    <img src="../icons/logo inpulse.png" alt="INPulse Logo" class="logo" />

    <div class="header-actions">
      <div class="dropdown-wrapper">
        <button class="contact-button" @click="toggleDropdown">Contact</button>
        <div class="contact-dropdown" :class="{ 'dropdown-visible': isDropdownOpen }">
          <button @click="contactAll">Contacter tous</button>
          <button v-for="(email, index) in entrepreneurEmails" :key="index">
            {{ email }}
          </button>
        </div>
      </div>

      <RouterLink to="/" class="return-button">Retour</RouterLink>
    </div>
  </header>
</template>


<script setup lang="ts">
import { ref, onMounted } from "vue";
import axios from "axios";

const IS_MOCK_MODE = true; 

const props = defineProps<{
  projectId: number;
}>();

type Entrepreneur = {
  id: number;
  name: string;
  primaryMail: string;
};

const isDropdownOpen = ref(false);
const entrepreneurEmails = ref([]);

const toggleDropdown = () => {
  isDropdownOpen.value = !isDropdownOpen.value;
  console.log("Dropdown toggled:", isDropdownOpen.value);
};


const fetchEntrepreneurs = async (projectId :number, useMock = IS_MOCK_MODE) => {
  try {
    const responseData = useMock
      ? await mockFetchEntrepreneurs(projectId)
      : (await axios.get(`http://localhost:5000/shared/projects/entrepreneurs/${projectId}`)).data;

    if (responseData.length > 0) {
      entrepreneurEmails.value = responseData.map((item) => item.primaryMail);
    } else {
      console.warn("Aucun entrepreneur trouvé.");
    }
  } catch (error) {
    console.error("Erreur lors de la récupération des entrepreneurs :", error);
  }
};

// Fonction de simulation de l'API
const mockFetchEntrepreneurs = async (projectId :number) => {
  console.log(`Mock fetch pour projectId: ${projectId}`);

  return new Promise((resolve) => {
    setTimeout(() => {
      resolve([
        {
          idUser: 1,
          userSurname: "Doe",
          userName: "John",
          primaryMail: "john.doe@example.com",
          secondaryMail: "johndoe@backup.com",
          phoneNumber: "612345678",
          school: "ENSEIRB",
          course: "Info",
          sneeStatus: false
        },
        {
          idUser: 2,
          userSurname: "Smith",
          userName: "Jane",
          primaryMail: "jane.smith@example.com",
          secondaryMail: "janesmith@backup.com",
          phoneNumber: "698765432",
          school: "ENSEIRB",
          course: "Info",
          sneeStatus: true
        }
      ]);
    }, 500);
  });
};

const contactAll = () => {
  alert("Contacter tous les entrepreneurs : " + entrepreneurEmails.value.join(", "));
};

onMounted(() => fetchEntrepreneurs(props.projectId, IS_MOCK_MODE));
</script>

<style scoped>
@import "@/components/canvas/style-project.css";

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 30px;
  background-color: #f9f9f9;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.logo {
  height: 50px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 20px;
  position: relative;
}

.contact-button,
.return-button {
  background-color: #009CDE;
  color: white;
  border: none;
  padding: 10px 15px;
  cursor: pointer;
  font-size: 14px;
  border-radius: 5px;
  text-decoration: none;
  transition: background-color 0.2s ease;
  font-family: Arial, sans-serif;
}

.return-button:hover,
.contact-button:hover {
  background-color: #007bad;
}


.contact-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  background-color: #000;
  color: white;
  box-shadow: 0px 4px 8px rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  padding: 10px;
  margin-top: 5px;
  z-index: 1000;
  min-width: 200px;
  display: none;
}

.contact-dropdown button {
  display: block;
  width: 100%;
  padding: 5px;
  text-align: left;
  border: none;
  background: none;
  cursor: pointer;
  color: white;
}

.contact-dropdown button:hover {
  background-color: #009CDE;
}

.contact-dropdown.dropdown-visible {
  display: block;
}


</style>
