<?php
// Start session if not already started
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Enable error reporting for development
error_reporting(E_ALL);
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/php_errors.log');

require_once('../includes/admin-security.php');
include_once('includes/config.php');

// Access Check
if (!has_permission('manage_categories')) {
    echo "<script>alert('Access Denied. Redirecting to Dashboard...'); window.location.href='dashboard.php';</script>";
    exit();
}

// Handle subcategory submission
if (isset($_POST['submit'])) {
    $categoryid = intval($_POST['category']);
    $subcategory = db_prepare($con, $_POST['subcategory']);
    $createdby = $_SESSION['aid'] ?? $_SESSION['id'];

    $sql = mysqli_query($con, "INSERT INTO subcategory(categoryid, subcategory, createdBy) VALUES('$categoryid', '$subcategory', '$createdby')");

    if ($sql) {
        log_activity("Added Subcategory", "Subcategory Name: $subcategory (Parent CID: $categoryid)");
        $_SESSION['msg'] = "Subcategory created successfully!";
    } else {
        $_SESSION['error'] = "Error creating subcategory: " . mysqli_error($con);
    }

    header('location: manage-subcategories.php');
    exit();
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>Add Subcategory - Admin</title>
    <link href="css/styles.css" rel="stylesheet" />
    <link href="css/admin-professional.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/js/all.min.js"
        crossorigin="anonymous"></script>


</head>

<body class="sb-nav-fixed">
    <?php include_once('includes/header.php'); ?>

    <div id="layoutSidenav">
        <?php include_once('includes/sidebar.php'); ?>



        <div id="layoutSidenav_content">
            <main>
                <div class="container-fluid px-4">
                    <!-- Page Header -->
                    <div class="page-header mt-4">
                        <div>
                            <h1><i class="fas fa-layer-group me-2"></i> Add New Subcategory</h1>
                            <p>Create a subcategory within a parent category</p>
                        </div>
                    </div>

                    <div class="row justify-content-center">
                        <div class="col-lg-6 col-md-8">
                            <!-- Form Card -->
                            <div class="form-card">
                                <form method="post">
                                    <div class="mb-4">
                                        <label class="form-label">Parent Category *</label>
                                        <select name="category" class="form-select" required>
                                            <option value="">Select Parent Category</option>
                                            <?php
                                            $query = mysqli_query($con, "SELECT * FROM category");
                                            while ($row = mysqli_fetch_array($query)) {
                                                ?>
                                                <option value="<?php echo $row['id']; ?>">
                                                    <?php echo $row['categoryName']; ?>
                                                </option>
                                            <?php } ?>
                                        </select>
                                        <small class="text-muted">Choose the main category for this subcategory</small>
                                    </div>

                                    <div class="mb-4">
                                        <label class="form-label">Subcategory Name *</label>
                                        <input type="text" name="subcategory" placeholder="Enter subcategory name"
                                            class="form-control" required>
                                        <small class="text-muted">Enter a descriptive name for the subcategory</small>
                                    </div>

                                    <button type="submit" name="submit" class="btn btn-primary w-100 mb-3">
                                        <i class="fas fa-check me-2"></i> Create Subcategory
                                    </button>

                                    <a href="manage-subcategories.php" class="btn btn-light w-100">
                                        <i class="fas fa-arrow-left me-2"></i> Back to Subcategories
                                    </a>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </main>
            <?php include_once('includes/footer.php'); ?>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
        crossorigin="anonymous"></script>
    <script src="js/scripts.js"></script>
</body>

</html>