<?php

require_once 'databaseConnection.php';
$mysqli = db_connect();

if ($mysqli) {
    // Initialize required variables
    $currentCategory = '';
    $categories = array(); // Array to store all details

    if (isset($_GET['id'])) {
        // Fetch the specific category details
        $category = $_GET['id'];
        $stmt = $mysqli->prepare("SELECT `category`, `description`, `status` FROM `addCategory` WHERE `category` = ?");
        $stmt->bind_param("s", $category);
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($category, $description, $status);
        if ($stmt->fetch()) {
            $currentCategory = $category;
        }
        $stmt->close();
    } else {
        // Fetch all categories for dropdown options
        $stmt = $mysqli->prepare("SELECT `category`, `description`, `status` FROM `addCategory`");
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($category, $description, $status);

        // Populate the `$categories` array
        while ($stmt->fetch()) {
            $categories[] = array(
                'category' => $category,
                'description' => $description,
                'status' => $status
            );
        }
        $stmt->close(); // Close the statement after populating the array
    }
} else {
    echo "Database connection failed: " . $mysqli->connect_error;
    exit;
}

db_close($mysqli);
