<?php
session_start();
require_once 'includes/config.php';
if (!isset($_SESSION['nguoi_dung_id'])) { header('Location: dang-nhap.php'); exit; }

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!empty($_POST['xoa_id'])) {
        $pdo->prepare("DELETE FROM tri_an WHERE id = ?")->execute([(int)$_POST['xoa_id']]);
        header('Location: quan-ly-tri-an.php?tb=xoa'); exit;
    }
    if (!empty($_POST['doi_trang_thai_id']) && !empty($_POST['trang_thai_moi'])) {
        $pdo->prepare("UPDATE tri_an SET trang_thai = ? WHERE id = ?")
            ->execute([$_POST['trang_thai_moi'], (int)$_POST['doi_trang_thai_id']]);
        header('Location: quan-ly-tri-an.php?tb=cap_nhat'); exit;
    }
}

$loc = $_GET['loc'] ?? 'tat_ca';
$where = match($loc) {
    'cho_duyet' => "WHERE ta.trang_thai = 'cho_duyet'",
    'da_duyet'  => "WHERE ta.trang_thai = 'da_duyet'",
    'an'        => "WHERE ta.trang_thai = 'an'",
    default     => ''
};

$ds = $pdo->query("SELECT ta.*, tv.ho_ten, tv.anh_dai_dien FROM tri_an ta LEFT JOIN thanh_vien tv ON tv.id = ta.thanh_vien_id $where ORDER BY ta.ngay_tao DESC")->fetchAll();

$dem = $pdo->query("SELECT trang_thai, COUNT(*) as so FROM tri_an GROUP BY trang_thai")->fetchAll(PDO::FETCH_KEY_PAIR);
?>
<!DOCTYPE html>
<html lang="vi">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Quản lý Lời Tri ân - Admin</title>
    <link rel="stylesheet" href="includes/admin-style.css">
    <style>
        .the-tri-an { background:#fff; border-radius:10px; padding:20px; margin-bottom:14px; box-shadow:0 2px 8px rgba(0,0,0,.06); display:flex; gap:16px; align-items:flex-start; transition:.2s; }
        .the-tri-an.cho_duyet { border-left:4px solid #ffc107; }
        .the-tri-an.da_duyet  { border-left:4px solid #28a745; }
        .the-tri-an.an        { border-left:4px solid #adb5bd; opacity:.7; }
        .avatar { width:48px; height:48px; border-radius:50%; object-fit:cover; flex-shrink:0; }
        .avatar-chu { width:48px; height:48px; border-radius:50%; background:#6f42c1; color:white; display:flex; align-items:center; justify-content:center; font-weight:bold; font-size:20px; flex-shrink:0; }
        .noi-dung { flex:1; min-width:0; }
        .ten { font-weight:bold; color:#333; font-size:15px; }
        .ngay { font-size:12px; color:#999; margin:3px 0 10px; }
        .van-ban { font-size:14px; color:#555; line-height:1.7; font-style:italic; }
        .nhom-nut { display:flex; gap:8px; flex-shrink:0; flex-wrap:wrap; justify-content:flex-end; }
        .nut { border:none; padding:7px 14px; border-radius:6px; cursor:pointer; font-size:13px; font-weight:bold; }
        .nut-duyet { background:#28a745; color:white; }
        .nut-an    { background:#6c757d; color:white; }
        .nut-hien  { background:#0056b3; color:white; }
        .nut-xoa   { background:#dc3545; color:white; }
        .nut:hover { opacity:.85; }
        .badge { display:inline-block; padding:3px 10px; border-radius:999px; font-size:12px; font-weight:bold; margin-bottom:8px; }
        .badge.cho_duyet { background:#fff3cd; color:#856404; }
        .badge.da_duyet  { background:#d4edda; color:#155724; }
        .badge.an        { background:#e9ecef; color:#6c757d; }
        .tab-loc { display:flex; gap:8px; margin-bottom:24px; flex-wrap:wrap; }
        .tab-loc a { padding:8px 18px; border-radius:8px; text-decoration:none; font-size:14px; font-weight:500; background:#f1f3f5; color:#495057; }
        .tab-loc a.active { background:#6f42c1; color:white; }
        .thong-bao { background:#d4edda; color:#155724; border:1px solid #c3e6cb; padding:12px 16px; border-radius:8px; margin-bottom:20px; }
    </style>
</head>
<body>
<?php require_once 'includes/menu-admin.php'; ?>
<div class="noi-dung-chinh" style="padding:30px;">
    <h1 style="color:#6f42c1; margin-bottom:20px;">💌 Lời Tri ân Cộng đồng</h1>

    <?php if (isset($_GET['tb'])): ?>
        <div class="thong-bao">✅ <?php echo $_GET['tb'] === 'xoa' ? 'Đã xóa.' : 'Đã cập nhật trạng thái.'; ?></div>
    <?php endif; ?>

    <div class="tab-loc">
        <a href="?loc=tat_ca"   class="<?php echo $loc === 'tat_ca'   ? 'active' : ''; ?>">Tất cả (<?php echo array_sum($dem); ?>)</a>
        <a href="?loc=cho_duyet" class="<?php echo $loc === 'cho_duyet' ? 'active' : ''; ?>">⏳ Chờ duyệt (<?php echo $dem['cho_duyet'] ?? 0; ?>)</a>
        <a href="?loc=da_duyet"  class="<?php echo $loc === 'da_duyet'  ? 'active' : ''; ?>">✅ Đã duyệt (<?php echo $dem['da_duyet'] ?? 0; ?>)</a>
        <a href="?loc=an"        class="<?php echo $loc === 'an'        ? 'active' : ''; ?>">🙈 Đã ẩn (<?php echo $dem['an'] ?? 0; ?>)</a>
    </div>

    <?php if (empty($ds)): ?>
        <div style="text-align:center; color:#999; padding:40px; background:#fff; border-radius:10px;">Không có lời tri ân nào.</div>
    <?php else: ?>
        <?php foreach ($ds as $ta): ?>
        <div class="the-tri-an <?php echo $ta['trang_thai']; ?>">
            <?php if (!empty($ta['anh_dai_dien'])): ?>
                <img src="<?php echo htmlspecialchars($ta['anh_dai_dien']); ?>" class="avatar">
            <?php else: ?>
                <div class="avatar-chu"><?php echo mb_substr($ta['ho_ten'] ?? '?', 0, 1, 'UTF-8'); ?></div>
            <?php endif; ?>
            <div class="noi-dung">
                <div class="ten"><?php echo htmlspecialchars($ta['ho_ten'] ?? 'Thành viên'); ?></div>
                <div class="ngay"><?php echo date('d/m/Y H:i', strtotime($ta['ngay_tao'])); ?></div>
                <span class="badge <?php echo $ta['trang_thai']; ?>">
                    <?php echo ['cho_duyet'=>'⏳ Chờ duyệt','da_duyet'=>'✅ Đã duyệt','an'=>'🙈 Đã ẩn'][$ta['trang_thai']]; ?>
                </span>
                <div class="van-ban">"<?php echo nl2br(htmlspecialchars($ta['noi_dung'])); ?>"</div>
            </div>
            <div class="nhom-nut">
                <?php if ($ta['trang_thai'] !== 'da_duyet'): ?>
                <form method="POST">
                    <input type="hidden" name="doi_trang_thai_id" value="<?php echo $ta['id']; ?>">
                    <input type="hidden" name="trang_thai_moi" value="da_duyet">
                    <button class="nut nut-duyet">✅ Duyệt</button>
                </form>
                <?php endif; ?>
                <?php if ($ta['trang_thai'] !== 'an'): ?>
                <form method="POST">
                    <input type="hidden" name="doi_trang_thai_id" value="<?php echo $ta['id']; ?>">
                    <input type="hidden" name="trang_thai_moi" value="an">
                    <button class="nut nut-an">🙈 Ẩn</button>
                </form>
                <?php else: ?>
                <form method="POST">
                    <input type="hidden" name="doi_trang_thai_id" value="<?php echo $ta['id']; ?>">
                    <input type="hidden" name="trang_thai_moi" value="da_duyet">
                    <button class="nut nut-hien">👁 Hiện</button>
                </form>
                <?php endif; ?>
                <form method="POST" onsubmit="return confirm('Xóa vĩnh viễn?')">
                    <input type="hidden" name="xoa_id" value="<?php echo $ta['id']; ?>">
                    <button class="nut nut-xoa">🗑 Xóa</button>
                </form>
            </div>
        </div>
        <?php endforeach; ?>
    <?php endif; ?>
</div>
</body>
</html>
