<?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); // Don't display errors on page
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();
}

if (isset($_POST['submit'])) {
$category = $_POST['category'];
$subcat = $_POST['subcategory'];
$sql = mysqli_query($con, "insert into subcategory(categoryid,subcategory) values('$category','$subcat')");
$_SESSION['msg'] = "SubCategory Created!";
}

if (isset($_GET['del'])) {
mysqli_query($con, "delete from subcategory where id = '" . $_GET['id'] . "'");
$_SESSION['delmsg'] = "SubCategory deleted!";
}
?>
<!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" />
	<meta name="description" content="Sub Category - Awesome Mall Admin" />
	<meta name="author" content="" />
	<title>Awesome Mall | Sub Category</title>
	<link href="https://cdn.jsdelivr.net/npm/simple-datatables@latest/dist/style.css" rel="stylesheet" />
	<link href="css/styles.css" rel="stylesheet" />
	<link href="css/admin-professional.css" rel="stylesheet" />
	<script src="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-sitemap me-2"></i> Sub Category</h1>
							<p>Create and manage product subcategories</p>
						</div>
					</div>

					<!-- Success/Error Messages -->
					<?php if (isset($_POST['submit'])) { ?>
						<div class="alert alert-success alert-dismissible fade show" role="alert">
							<i class="fas fa-check-circle me-2"></i>
							<strong>Success!</strong> <?php echo htmlentities($_SESSION['msg']); ?>
							<?php echo htmlentities($_SESSION['msg'] = ""); ?>
							<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
						</div>
					<?php } ?>

					<?php if (isset($_GET['del'])) { ?>
						<div class="alert alert-danger alert-dismissible fade show" role="alert">
							<i class="fas fa-trash me-2"></i>
							<?php echo htmlentities($_SESSION['delmsg']); ?>
							<?php echo htmlentities($_SESSION['delmsg'] = ""); ?>
							<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
						</div>
					<?php } ?>

					<!-- Create SubCategory Form -->
					<div class="table-card mb-4">
						<div class="card-header">
							<h5 class="mb-0"><i class="fas fa-plus-circle me-2"></i>Add New SubCategory</h5>
						</div>
						<div class="card-body">
							<form method="post">
								<div class="row">
									<div class="col-md-6 mb-3">
										<label for="category" class="form-label fw-semibold">Category <span
												class="text-danger">*</span></label>
										<select name="category" id="category" class="form-select" required>
											<option value="">Select 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>
									</div>
									<div class="col-md-6 mb-3">
										<label for="subcategory" class="form-label fw-semibold">SubCategory Name <span
												class="text-danger">*</span></label>
										<input type="text" placeholder="Enter SubCategory Name" name="subcategory"
											id="subcategory" class="form-control" required>
									</div>
								</div>
								<button type="submit" name="submit" class="btn btn-primary">
									<i class="fas fa-save me-2"></i>Create SubCategory
								</button>
							</form>
						</div>
					</div>

					<!-- Manage SubCategories Table -->
					<div class="table-card">
						<div class="card-header">
							<h5 class="mb-0"><i class="fas fa-list me-2"></i>Manage SubCategories</h5>
						</div>
						<div class="card-body">
							<table class="table table-hover" id="datatablesSimple">
								<thead>
									<tr>
										<th>#</th>
										<th>Category</th>
										<th>SubCategory</th>
										<th>Creation Date</th>
										<th>Last Updated</th>
										<th>Action</th>
									</tr>
								</thead>
								<tbody>
									<?php
									$query = mysqli_query($con, "select subcategory.id,category.categoryName,subcategory.subcategory,subcategory.creationDate,subcategory.updationDate from subcategory join category on category.id=subcategory.categoryid");
									$cnt = 1;
									while ($row = mysqli_fetch_array($query)) {
										?>
										<tr>
											<td><?php echo htmlentities($cnt); ?></td>
											<td><strong><?php echo htmlentities($row['categoryName']); ?></strong></td>
											<td><?php echo htmlentities($row['subcategory']); ?></td>
											<td><?php echo htmlentities($row['creationDate']); ?></td>
											<td><?php echo htmlentities($row['updationDate']); ?></td>
											<td>
												<a href="edit-subcategory.php?id=<?php echo $row['id'] ?>"
													class="btn btn-sm btn-outline-primary" title="Edit SubCategory">
													<i class="fas fa-edit"></i>
												</a>
												<a href="subcategory.php?id=<?php echo $row['id'] ?>&del=delete"
													class="btn btn-sm btn-outline-danger" title="Delete SubCategory"
													onClick="return confirm('Are you sure you want to delete?')">
													<i class="fas fa-trash"></i>
												</a>
											</td>
										</tr>
										<?php $cnt = $cnt + 1;
									} ?>
								</tbody>
							</table>
						</div>
					</div>
				</div>
			</main>
			<?php include_once('includes/footer.php'); ?>
		</div>
	</div>
	<script src="js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
	<script src="js/scripts.js"></script>
	<script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest" crossorigin="anonymous"></script>
	<script src="js/datatables-simple-demo.js"></script>
</body>

</html>
?>
