<?php
session_start();
include 'strec.php';

// Ensure the admin is logged in
if (!isset($_SESSION['is_admin']) || $_SESSION['is_admin'] !== true) {
    header("Location: login.php");
    exit();
}

// Fetch all game categories
$categoriesQuery = "SELECT * FROM game_categories";
$categoriesResult = $conn->query($categoriesQuery);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin - Manage Game Categories</title>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container my-5">
    <h1 class="text-center">Manage Game Categories</h1>
    <div class="my-4">
        <button class="btn btn-primary" onclick="showAddCategoryForm()">Add New Category</button>
    </div>

    <table class="table table-striped table-dark">
        <thead>
            <tr>
                <th>ID</th>
                <th>Category</th>
                <th>Game ID</th>
                <th>Image</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
        <?php
        if ($categoriesResult->num_rows > 0) {
            while ($row = $categoriesResult->fetch_assoc()) {
                echo "<tr>
                    <td>{$row['category_id']}</td>
                    <td>{$row['category']}</td>
                    <td>{$row['game_id']}</td>
                    <td><img src='" . htmlspecialchars($row['category_image']) . "' width='80'></td>
                    <td>
                        <button class='btn btn-warning btn-sm' onclick='editCategory({$row['category_id']})'>Edit</button>
                        <button class='btn btn-danger btn-sm' onclick='deleteCategory({$row['category_id']})'>Delete</button>
                    </td>
                </tr>";
            }
        } else {
            echo "<tr><td colspan='5' class='text-center'>No categories available.</td></tr>";
        }
        ?>
        </tbody>
    </table>

    <!-- Add/Edit Modal -->
    <div id="categoryModal" class="modal" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Add/Edit Category</h5>
                    <button type="button" class="close" onclick="closeModal()" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <form id="categoryForm" action="save_category.php" method="post" enctype="multipart/form-data">
                    <div class="modal-body">
                        <input type="hidden" name="category_id" id="category_id">
                        <div class="form-group">
                            <label for="category">Category Name</label>
                            <input type="text" class="form-control" name="category" id="category" required>
                        </div>
                        <div class="form-group">
                            <label for="game_id">Game ID</label>
                            <input type="number" class="form-control" name="game_id" id="game_id" required>
                        </div>
                        <div class="form-group">
                            <label for="category_image">Category Image</label>
                            <input type="file" class="form-control" name="category_image" id="category_image">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">Save</button>
                        <button type="button" class="btn btn-secondary" onclick="closeModal()">Close</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
function showAddCategoryForm() {
    document.getElementById('categoryForm').reset();
    document.getElementById('category_id').value = '';
    document.getElementById('categoryModal').style.display = 'block';
}

function editCategory(categoryId) {
    // Fetch category details using AJAX and populate form
    // Example: Call fetch_category.php?category_id=categoryId
}

function deleteCategory(categoryId) {
    if (confirm('Are you sure you want to delete this category?')) {
        window.location.href = 'delete_category.php?category_id='
