<?php
/**
 * Admin - Gelen Ziyaretçi Mesajları
 * Konum: /magaza/admin/mesajlar/index.php
 */
require_once '../../includes/db.php';
require_once '../../includes/functions.php';
adminCheck();

// Mesajları Çek
$mesajlar = $db->query("SELECT * FROM contact_messages ORDER BY id DESC")->fetchAll();

require_once '../header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-4 text-dark">
    <h2 class="fw-bold mb-0"><i class="bi bi-envelope-paper-heart text-primary me-2"></i>Gelen Mesajlar</h2>
</div>

<div class="card shadow-sm border-0 rounded-4 overflow-hidden text-dark">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th class="ps-4 py-3">Tarih</th>
                        <th>Gönderen</th>
                        <th>Mesaj Özeti</th>
                        <th>Durum</th>
                        <th class="text-end pe-4">İşlem</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(count($mesajlar) > 0): ?>
                        <?php foreach($mesajlar as $m): ?>
                        <tr class="<?= $m['status'] == 'new' ? 'table-warning' : '' ?>">
                            <td class="ps-4 text-muted small fw-bold"><?= date('d.m.Y H:i', strtotime($m['created_at'])) ?></td>
                            <td>
                                <div class="fw-bold text-dark"><?= htmlspecialchars($m['name']) ?></div>
                                <div class="small text-muted"><?= htmlspecialchars($m['email']) ?></div>
                            </td>
                            <td class="text-muted">
                                <?= mb_substr(htmlspecialchars($m['message']), 0, 40) ?>...
                            </td>
                            <td>
                                <?php if($m['status'] == 'new'): ?>
                                    <span class="badge bg-danger rounded-pill px-3">Yeni</span>
                                <?php elseif($m['status'] == 'read'): ?>
                                    <span class="badge bg-secondary rounded-pill px-3">Okundu</span>
                                <?php else: ?>
                                    <span class="badge bg-success rounded-pill px-3">Cevaplandı</span>
                                <?php endif; ?>
                            </td>
                            <td class="text-end pe-4">
                                <a href="cevapla.php?id=<?= $m['id'] ?>" class="btn btn-sm btn-dark fw-bold rounded-pill px-3 shadow-sm">
                                    Oku / Cevapla
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr><td colspan="5" class="text-center py-5 text-muted">Hiç mesaj bulunmuyor.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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