<?php
session_start();
include(__DIR__ . '/../config/db.php');

// รับ ID ข่าวจาก GET
$id = $_GET['id'] ?? 0;
$id = intval($id);

// ดึงข่าวตาม ID (เฉพาะ published)
$stmt = $conn->prepare("
    SELECT a.*, c.name AS category_name 
    FROM articles a 
    JOIN categories c ON a.category_id = c.id 
    WHERE a.id = ? AND a.status = 'published'
");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$article = $result->fetch_assoc();
$stmt->close();

if (!$article) {
    header("Location: index.php");
    exit;
}

// ดึงความคิดเห็น
$stmt_c = $conn->prepare("
    SELECT * FROM comments 
    WHERE article_id = ? AND is_approved = 1 
    ORDER BY created_at DESC
");
$stmt_c->bind_param("i", $article['id']);
$stmt_c->execute();
$comments = $stmt_c->get_result();
$stmt_c->close();
?>
<!DOCTYPE html>
<html lang="th">
<head>
    <meta charset="UTF-8">
    <title><?php echo htmlspecialchars($article['title']); ?></title>
    <style>
        body {
            font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
            background: #f4f6f9;
            margin: 0;
            padding: 0;
            color: #333;
        }
        .container {
            max-width: 900px;
            margin: 30px auto;
            background: #fff;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 0 15px rgba(0,0,0,0.08);
        }
        h1 {
            font-size: 28px;
            margin-bottom: 10px;
            color: #222;
        }
        .meta {
            font-size: 14px;
            color: #777;
            margin-bottom: 20px;
        }
        .meta strong {
            color: #333;
        }
        .content {
            font-size: 16px;
            line-height: 1.8;
            color: #444;
            margin-bottom: 30px;
        }
        hr {
            border: none;
            border-top: 1px solid #eee;
            margin: 25px 0;
        }
        h3 {
            margin-top: 25px;
            color: #007BFF;
        }
        .comment-box {
            margin-bottom: 15px;
            padding: 12px;
            background: #f9f9f9;
            border-left: 4px solid #007BFF;
            border-radius: 5px;
        }
        .comment-box strong {
            color: #333;
        }
        .comment-box small {
            color: #888;
            font-size: 12px;
        }
        form {
            margin-top: 15px;
        }
        input[type="text"], textarea {
            width: 100%;
            padding: 10px;
            margin: 8px 0 15px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 14px;
        }
        button {
            background: #007BFF;
            color: #fff;
            border: none;
            padding: 10px 18px;
            font-size: 15px;
            border-radius: 6px;
            cursor: pointer;
            transition: 0.3s;
        }
        button:hover {
            background: #0056b3;
        }
        .back-link {
            display: inline-block;
            margin-top: 20px;
            color: #007BFF;
            text-decoration: none;
            font-size: 14px;
        }
        .back-link:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
<div class="container">
    <h1><?php echo htmlspecialchars($article['title']); ?></h1>
    <p class="meta">
        <strong>หมวด:</strong> <?php echo htmlspecialchars($article['category_name']); ?> |
        <strong>วันที่:</strong> <?php echo $article['created_at']; ?>
    </p>
    <div class="content">
        <?php echo nl2br(htmlspecialchars($article['content'])); ?>
    </div>

    <hr>

    <h3>ความคิดเห็น</h3>
    <?php if($comments->num_rows > 0): ?>
        <?php while($c = $comments->fetch_assoc()): ?>
            <div class="comment-box">
                <p>
                    <strong><?php echo htmlspecialchars($c['author_name']); ?></strong><br>
                    <?php echo nl2br(htmlspecialchars($c['comment'])); ?><br>
                    <small><?php echo $c['created_at']; ?></small>
                </p>
            </div>
        <?php endwhile; ?>
    <?php else: ?>
        <p>ยังไม่มีความคิดเห็น</p>
    <?php endif; ?>

    <hr>

    <h3>แสดงความคิดเห็น</h3>
    <form method="post" action="comments.php">
        <input type="hidden" name="article_id" value="<?php echo $article['id']; ?>">
        <label>ชื่อ:</label>
        <input type="text" name="author_name" required>

        <label>ความคิดเห็น:</label>
        <textarea name="comment" rows="5" required></textarea>

        <button type="submit">ส่งความคิดเห็น</button>
    </form>

    <a class="back-link" href="index.php">← กลับสู่หน้าหลัก</a>
</div>
</body>
</html>
