<?php
session_start();
if (!isset($_SESSION['user_id']) || $_SESSION['role'] != 'admin') {
    header("Location: login.php");
    exit();
}

include(__DIR__ . '/../config/db.php');

// อัปเดตสถานะ Approve
if (isset($_GET['approve'])) {
    $comment_id = intval($_GET['approve']);
    $stmt = $conn->prepare("UPDATE comments SET is_approved=1 WHERE id=?");
    $stmt->bind_param("i", $comment_id);
    $stmt->execute();
    $stmt->close();
    header("Location: manage_comments.php");
    exit();
}

// ลบ comment
if (isset($_GET['delete'])) {
    $comment_id = intval($_GET['delete']);
    $stmt = $conn->prepare("DELETE FROM comments WHERE id=?");
    $stmt->bind_param("i", $comment_id);
    $stmt->execute();
    $stmt->close();
    header("Location: manage_comments.php");
    exit();
}

// ดึง comment ทั้งหมด
$sql = "SELECT c.id, c.author_name, c.comment, c.is_approved, c.created_at, a.title as article_title
        FROM comments c
        JOIN articles a ON c.article_id = a.id
        ORDER BY c.created_at DESC";
$comments = $conn->query($sql);
?>

<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<title>จัดการความคิดเห็น</title>
<style>
    body {
        font-family: "Segoe UI", Arial, sans-serif;
        background: #eef2f7;
        padding: 20px;
        margin: 0;
    }
    .container {
        max-width: 1100px;
        margin: auto;
        background: #fff;
        padding: 25px;
        border-radius: 12px;
        box-shadow: 0 4px 15px rgba(0,0,0,0.1);
    }
    h2 {
        text-align: center;
        color: #222;
        margin-bottom: 20px;
    }
    .top-links {
        margin-bottom: 20px;
        text-align: center;
    }
    .top-links a {
        display: inline-block;
        margin: 0 8px;
        padding: 10px 18px;
        background: #007BFF;
        color: #fff;
        text-decoration: none;
        border-radius: 6px;
        transition: 0.3s;
    }
    .top-links a:hover {
        background: #0056b3;
    }
    table {
        width: 100%;
        border-collapse: collapse;
        margin-top: 15px;
    }
    th, td {
        border: 1px solid #ddd;
        padding: 14px;
        text-align: left;
        vertical-align: top;
    }
    th {
        background: #007BFF;
        color: #fff;
        text-align: center;
    }
    tr:nth-child(even) {
        background: #f9f9f9;
    }
    tr:hover {
        background: #f1f7ff;
    }
    .btn {
        padding: 6px 12px;
        border-radius: 6px;
        color: #fff;
        text-decoration: none;
        font-size: 14px;
        transition: 0.2s;
    }
    .approve {
        background: #28a745;
    }
    .approve:hover {
        background: #218838;
    }
    .delete {
        background: #dc3545;
    }
    .delete:hover {
        background: #c82333;
    }
    td.comment-text {
        max-width: 350px;
        word-wrap: break-word;
        white-space: pre-wrap;
    }
    .status {
        font-weight: bold;
        text-align: center;
    }
    .approved {
        color: #28a745;
    }
    .pending {
        color: #dc3545;
    }
    @media (max-width: 768px) {
        table, th, td {
            font-size: 13px;
        }
        .top-links a {
            padding: 8px 12px;
            margin: 5px 3px;
        }
    }
</style>
</head>
<body>
<div class="container">
    <h2>จัดการความคิดเห็น</h2>
    <div class="top-links">
        <a href="admin_dashboard.php">⬅ กลับสู่ Dashboard</a>
        <a href="../admin/logout.php">🚪 ออกจากระบบ</a>
    </div>

    <table>
        <tr>
            <th>ID</th>
            <th>ข่าว</th>
            <th>ชื่อผู้แสดงความคิดเห็น</th>
            <th>ความคิดเห็น</th>
            <th>สถานะ</th>
            <th>วันที่</th>
            <th>จัดการ</th>
        </tr>
        <?php while($c = $comments->fetch_assoc()): ?>
        <tr>
            <td style="text-align:center;"><?php echo $c['id']; ?></td>
            <td><?php echo htmlspecialchars($c['article_title']); ?></td>
            <td><?php echo htmlspecialchars($c['author_name']); ?></td>
            <td class="comment-text"><?php echo nl2br(htmlspecialchars($c['comment'])); ?></td>
            <td class="status <?php echo $c['is_approved'] ? 'approved' : 'pending'; ?>">
                <?php echo $c['is_approved'] ? '✅ อนุมัติแล้ว' : '⏳ รออนุมัติ'; ?>
            </td>
            <td><?php echo $c['created_at']; ?></td>
            <td>
                <?php if(!$c['is_approved']): ?>
                    <a class="btn approve" href="manage_comments.php?approve=<?php echo $c['id']; ?>">อนุมัติ</a>
                <?php endif; ?>
                <a class="btn delete" href="manage_comments.php?delete=<?php echo $c['id']; ?>" onclick="return confirm('ยืนยันลบความคิดเห็นนี้?')">ลบ</a>
            </td>
        </tr>
        <?php endwhile; ?>
    </table>
</div>
</body>
</html>
