<?php
session_start();
require_once '../config/db.php';
require_once 'auth.php';

$pageTitle = 'Pesan Masuk';
$activePage = 'messages';
$msg = '';

if (isset($_GET['delete'])) {
    $pdo->prepare("DELETE FROM contact_messages WHERE id=?")->execute([$_GET['delete']]);
    header('Location: messages.php'); exit;
}
if (isset($_GET['read'])) {
    $pdo->prepare("UPDATE contact_messages SET is_read=1 WHERE id=?")->execute([$_GET['read']]);
    header('Location: messages.php?view='.$_GET['read']); exit;
}

$viewMsg = null;
if (isset($_GET['view'])) {
    $stmt = $pdo->prepare("SELECT * FROM contact_messages WHERE id=?");
    $stmt->execute([$_GET['view']]);
    $viewMsg = $stmt->fetch();
    if ($viewMsg && !$viewMsg['is_read']) {
        $pdo->prepare("UPDATE contact_messages SET is_read=1 WHERE id=?")->execute([$_GET['view']]);
    }
}

$items = $pdo->query("SELECT * FROM contact_messages ORDER BY created_at DESC")->fetchAll();
include 'partials/header.php';
?>
<?= $msg ?>
<div class="row g-4">
    <div class="col-lg-5">
        <div class="card">
            <div class="card-header">Semua Pesan (<?= count($items) ?>)</div>
            <div class="list-group list-group-flush">
                <?php foreach ($items as $item): ?>
                <a href="?view=<?= $item['id'] ?>" class="list-group-item list-group-item-action d-flex justify-content-between align-items-start p-3 <?= isset($viewMsg) && $viewMsg['id'] == $item['id'] ? 'active' : '' ?>">
                    <div>
                        <div class="fw-semibold <?= !$item['is_read'] ? 'text-primary' : '' ?>"><?= htmlspecialchars($item['name']) ?> <?= !$item['is_read'] ? '<span class="badge bg-primary ms-1 small">Baru</span>' : '' ?></div>
                        <small class="text-muted"><?= htmlspecialchars(substr($item['subject']??'No Subject',0,40)) ?></small>
                    </div>
                    <small class="text-muted"><?= date('d/m', strtotime($item['created_at'])) ?></small>
                </a>
                <?php endforeach; ?>
                <?php if (empty($items)): ?>
                    <div class="list-group-item text-center text-muted py-4">Belum ada pesan masuk</div>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <div class="col-lg-7">
        <?php if ($viewMsg): ?>
        <div class="card">
            <div class="card-header d-flex justify-content-between">
                <span><?= htmlspecialchars($viewMsg['subject']??'No Subject') ?></span>
                <a href="?delete=<?= $viewMsg['id'] ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Hapus?')"><i class="bi bi-trash"></i></a>
            </div>
            <div class="card-body">
                <div class="row g-2 mb-3">
                    <div class="col-sm-6"><strong>Dari:</strong> <?= htmlspecialchars($viewMsg['name']) ?></div>
                    <div class="col-sm-6"><strong>Email:</strong> <?= htmlspecialchars($viewMsg['email']??'-') ?></div>
                    <div class="col-sm-6"><strong>Telepon:</strong> <?= htmlspecialchars($viewMsg['phone']??'-') ?></div>
                    <div class="col-sm-6"><strong>Tanggal:</strong> <?= date('d/m/Y H:i', strtotime($viewMsg['created_at'])) ?></div>
                </div>
                <hr>
                <div class="mt-3"><?= nl2br(htmlspecialchars($viewMsg['message'])) ?></div>
                <?php if ($viewMsg['email']): ?>
                <div class="mt-4">
                    <a href="mailto:<?= htmlspecialchars($viewMsg['email']) ?>" class="btn btn-dark btn-sm"><i class="bi bi-reply me-1"></i>Balas Email</a>
                </div>
                <?php endif; ?>
            </div>
        </div>
        <?php else: ?>
        <div class="card">
            <div class="card-body text-center py-5 text-muted">
                <i class="bi bi-envelope-open display-4 mb-3 d-block"></i>
                Pilih pesan untuk membacanya
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>
<?php include 'partials/footer.php'; ?>
