<?php
require_once __DIR__ . '/header.php';

$error = isset($_GET['error']) ? trim((string)$_GET['error']) : '';
$success = isset($_GET['success']) ? trim((string)$_GET['success']) : '';
?>

<main class="memories-main">
    <div class="memories-btn">
        <a href="ajout-souvenir.php" class="memories-btn-back">
           Ajouter un souvenir
            <svg viewBox="0 0 24 24" fill="none" xmlns ="http://www.w3.org/2000/svg">
                <path d="M12 5v14M5 12h14" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>

        </a>
    </div>
    <div class="memories-text">
        <h1>Choisi ton souvenir</h1>
        <!-- 🎀 ligne de séparation 🎀 -->
                 <div class="separation">
                    <span class="separation-line"></span>
                  </div>
    </div>

    <?php if ($error !== ''): ?>
        <p class="memory-message memory-message-error"><?= htmlspecialchars($error, ENT_QUOTES, 'UTF-8') ?></p>
    <?php elseif ($success !== ''): ?>
        <p class="memory-message memory-message-success"><?= htmlspecialchars($success, ENT_QUOTES, 'UTF-8') ?></p>
    <?php endif; ?>

    <?php if (empty($souvenirs)): ?>
        <p class="memory-message memory-message-success">Aucun souvenir pour le moment. Ajoute ton premier souvenir pour voir Modifier et Supprimer.</p>
    <?php endif; ?>

    <div class="memories-list">
        <?php foreach ($souvenirs as $index => $souvenir): ?>
            <?php $visibility = isset($souvenir['visibility']) ? (string)$souvenir['visibility'] : 'public'; ?>
            <div class="memory-card">
                <div class="memory-image-wrap">
                    <img
                        src="<?= htmlspecialchars($souvenir['image'], ENT_QUOTES, 'UTF-8') ?>"
                        alt="<?= htmlspecialchars($souvenir['nom'], ENT_QUOTES, 'UTF-8') ?>"
                        class="memory-image"
                    >
                    <div class="memory-overlay"></div>
                    <span class="memory-visibility-badge <?= $visibility === 'private' ? 'memory-visibility-private' : 'memory-visibility-public' ?>">
                        <?= $visibility === 'private' ? 'Prive' : 'Public' ?>
                    </span>
                </div>
                <div class="memory-bar">
                    <div class="memory-bar-left">
                        <span class="memory-icon">
                            <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                                <path d="M12 2C8.13 2 5 5.13 5 9c0 5.25 7 13 7 13s7-7.75 7-13c0-3.87-3.13-7-7-7zm0 9.5c-1.38 0-2.5-1.12-2.5-2.5s1.12-2.5 2.5-2.5 2.5 1.12 2.5 2.5-1.12 2.5-2.5 2.5z" fill="currentColor"/>
                            </svg>
                        </span>
                        <span class="memory-title"><?= htmlspecialchars($souvenir['nom'], ENT_QUOTES, 'UTF-8') ?></span>
                    </div>
                    <a
                        href="souvenir-detail.php?id=<?= htmlspecialchars((string)$souvenir['id'], ENT_QUOTES, 'UTF-8') ?>"
                        class="memory-arrow"
                        aria-label="Voir le souvenir <?= htmlspecialchars($souvenir['nom'], ENT_QUOTES, 'UTF-8') ?>"
                    >
                        <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                            <path d="M9 18l6-6-6-6" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"/>
                        </svg>
                    </a>
                </div>

                <div class="memory-actions">
                    <?php if ($souvenirsAreEditable): ?>
                        <a href="modifier-souvenir.php?id=<?= htmlspecialchars((string)$souvenir['id'], ENT_QUOTES, 'UTF-8') ?>" class="memory-action-link">Modifier</a>
                        <form action="supprimer-souvenir.php" method="post" class="memory-delete-form" onsubmit="return confirm('Supprimer ce souvenir ?');">
                            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars(csrfToken(), ENT_QUOTES, 'UTF-8') ?>">
                            <input type="hidden" name="id" value="<?= htmlspecialchars((string)$souvenir['id'], ENT_QUOTES, 'UTF-8') ?>">
                            <button type="submit" class="memory-action-delete">Supprimer</button>
                        </form>
                    <?php else: ?>
                        <a href="../login.php" class="memory-action-link">Se connecter pour modifier</a>
                    <?php endif; ?>
                </div>
            </div>

            <?php if ($index < count($souvenirs) - 1): ?>
                <div class="memory-separation" aria-hidden="true"></div>
            <?php endif; ?>
        <?php endforeach; ?>
    </div>
</main>
<?php
require_once __DIR__ . '/footer.php';
?>