<?php
$pageTitle = "Emails envoyés";
require_once __DIR__ . '/header.php';

try {
    $db = getDB();
    $sent = $db->query("SELECT * FROM email_logs ORDER BY sent_at DESC")->fetchAll();
} catch (Exception $e) {
    $sent = [];
}
?>

<div class="page-header">
    <div class="page-header-left">
        <div class="page-eyebrow">— Boîte d'envoi</div>
        <h1 class="page-title">Emails <span class="italic">envoyés</span></h1>
        <p class="page-subtitle">Historique de tous les messages envoyés</p>
    </div>
    <a href="compose.php" class="btn btn-or">
        <i class="fas fa-pen"></i>
        <span>Nouveau message</span>
    </a>
</div>

<?php if (empty($sent)): ?>
    <div class="card">
        <div class="empty-state">
            <i class="fas fa-paper-plane"></i>
            <h3>Aucun email envoyé</h3>
            <p>Vos emails envoyés apparaîtront ici</p>
        </div>
    </div>
<?php else: ?>
    <div class="message-list">
        <?php foreach ($sent as $email): ?>
            <div class="message-row" style="cursor: default;">
                <span class="message-star" style="color: var(--green);">
                    <i class="fas fa-paper-plane"></i>
                </span>
                <div>
                    <div class="message-from">À : <?= escape($email['to_email']) ?></div>
                    <div class="message-from-email">Status : <?= escape($email['status']) ?></div>
                </div>
                <div class="message-subject">
                    <?= escape($email['subject']) ?>
                    <span class="message-subject-preview"> — <?= escape(mb_substr($email['body'], 0, 80)) ?>...</span>
                </div>
                <div class="message-date"><?= timeAgo($email['sent_at']) ?></div>
                <div class="message-actions"></div>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

<?php require_once __DIR__ . '/footer.php'; ?>
