<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Announcements</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: 'Arial', sans-serif;
            background: linear-gradient(135deg, #101820, #1e3a8a);
            color: #fff;
        }
        header {
            text-align: center;
            padding: 1.5rem 0;
            background-color: #101820;
            border-bottom: 3px solid #1e3a8a;
        }
        header h1 {
            font-size: 2.5rem;
            margin: 0;
        }
        .container {
            max-width: 800px;
            margin: 2rem auto;
            padding: 1.5rem;
            background: rgba(0, 0, 0, 0.8);
            border-radius: 12px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
        }
        .announcement {
            background: #1e1e2e;
            border-left: 5px solid #1e90ff;
            margin-bottom: 1.5rem;
            padding: 1rem;
            border-radius: 8px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
        }
        .announcement h2 {
            margin: 0 0 0.5rem;
            color: #ffcc00;
            font-size: 1.2rem;
        }
        .announcement p {
            margin: 0;
            font-size: 1rem;
            color: #ddd;
        }
        .announcement .date {
            text-align: right;
            font-size: 0.9rem;
            color: #888;
            margin-top: 0.5rem;
        }
        .announcement .show-announces {
            text-align: left;
            color: #00ffcc;
            font-style: italic;
            margin-top: 0.5rem;
        }
        footer {
            text-align: center;
            margin-top: 2rem;
            padding: 1rem 0;
            background: #101820;
            color: #bbb;
        }
        footer a {
            color: #1e90ff;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <header>
        <h1>📢 Latest Announcements</h1>
        <p>Stay updated with the latest news and updates.</p>
    </header>
    <div class="container" id="announcementContainer">
        <!-- Les annonces s'affichent ici dynamiquement -->
    </div>
    <footer>
        <p>Created by <a href="#">Next Gen Innovators</a>. Powered by Masked.</p>
    </footer>
    <script>
        // Récupérer les annonces depuis ton API Google Apps Script
        fetch("https://script.google.com/macros/s/AKfycbyfbBuyc1onSoIOFnLal3xesJbk50iMDHjGa8YHaaaH4blhLgVgcImyf9Qdz8tbV8sKDg/exec?action=getAnnouncement")
            .then(response => response.json()) // Convertir la réponse en JSON
            .then(data => {
                if (data.success) {
                    // Trier les annonces par date (les plus récentes en premier)
                    const sortedAnnouncements = data.announcements.sort((a, b) => new Date(b.date) - new Date(a.date));
                    displayAnnouncements(sortedAnnouncements, data.showAnnounces);
                } else {
                    console.error("Error: Unable to fetch announcements");
                }
            })
            .catch(error => console.error("Error fetching announcements:", error));

        // Fonction pour formater une date au format "Month Day Year"
        function formatDate(isoDate) {
            const date = new Date(isoDate);
            return date.toLocaleDateString("en-US", {
                year: "numeric",
                month: "long",
                day: "numeric"
            });
        }

        // Fonction pour afficher les annonces dynamiquement
        function displayAnnouncements(announcements, showAnnounces) {
            const container = document.getElementById('announcementContainer');
            container.innerHTML = ""; // Vider le conteneur

            // Afficher chaque annonce
            announcements.forEach(item => {
                const formattedDate = formatDate(item.date); // Formater la date
                const announcementElement = `
                    <div class="announcement">
                        <h2>From: ${item.member}</h2>
                        <p>${item.announcement}</p>
                        <div class="date">📅 ${formattedDate}</div>
                    </div>
                `;
                container.innerHTML += announcementElement;
            });

            // Afficher le texte Show Announces (D1)
            if (showAnnounces) {
                const showAnnouncesElement = `
                    <div class="announcement show-announces">
                        ${showAnnounces}
                    </div>
                `;
                container.innerHTML += showAnnouncesElement;
            }
        }
    </script>
</body>
</html>