<?php
include 'database.php';

if (!isset($_GET['slug'])) {
    header("Location: index.php");
    exit();
}

$slug = $_GET['slug'];

$stmt = $conn->prepare("SELECT id, name FROM categories WHERE slug = ?");
$stmt->bind_param("s", $slug);
$stmt->execute();
$result = $stmt->get_result();
$category = $result->fetch_assoc();
$stmt->close();

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

$stmt = $conn->prepare("
    SELECT p.id, p.image_1, p.post_name, u.fname, u.lname
    FROM posts p
    JOIN users u ON p.user_id = u.id
    WHERE p.category_id = ?
    ORDER BY p.id DESC
");
$stmt->bind_param("i", $category['id']);
$stmt->execute();
$result = $stmt->get_result();
$posts = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
$conn->close();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link href="css/style.css" rel="stylesheet">
<link rel="icon" type="image/x-icon" href="img/favicon.ico">
<title><?php echo htmlspecialchars($category['name']); ?></title>
</head>
<body>
    <header>
        <img src="img/logo.png" alt="Logo" class="logo">
        <button id="navbtn" onclick="window.location.href='index.php'">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="size-6">
                <path d="M11.47 3.841a.75.75 0 0 1 1.06 0l8.69 8.69a.75.75 0 1 0 1.06-1.061l-8.689-8.69a2.25 2.25 0 0 0-3.182 0l-8.69 8.69a.75.75 0 1 0 1.061 1.06l8.69-8.689Z"/>
                <path d="m12 5.432 8.159 8.159c.03.03.06.058.091.086v6.198c0 1.035-.84 1.875-1.875 1.875H15a.75.75 0 0 1-.75-.75v-4.5a.75.75 0 0 0-.75-.75h-3a.75.75 0 0 0-.75.75V21a.75.75 0 0 1-.75.75H5.625a1.875 1.875 0 0 1-1.875-1.875v-6.198a2.29 2.29 0 0 0 .091-.086L12 5.432Z" />
            </svg>
        </button>
    </header>
    <h1><?php echo htmlspecialchars($category['name']); ?></h1>
        <?php if (empty($posts)): ?>
            <p class="text-center">No offers in this category yet.</p>
        <?php else: ?>
            <div class="post_grid">
                <?php foreach ($posts as $post): ?>
                    <div class="post_container">
                    <img src="uploads/<?php echo htmlspecialchars($post['image_1']); ?>" alt="Post Image" class="post_image" onclick="window.location.href='post.php?id=<?php echo htmlspecialchars($post['id']); ?>'">
                    <button class="post_name" onclick="window.location.href='post.php?id=<?php echo htmlspecialchars($post['id']);?>'">
                        <div>
                            <?php echo htmlspecialchars($post['post_name']);?>
                        </div>
                        <div id="post_author">
                            <?php echo htmlspecialchars($post['fname'] . ' ' . $post['lname']); ?>
                        </div>
                    </button>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    <script src="js/script.js"></script>
</body>
</html>