<?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();
}

// Handle category update
if (isset($_POST['submit'])) {
	$id = intval($_GET['id']);
	$category = db_prepare($con, $_POST['category']);
	$description = db_prepare($con, $_POST['description']);

	date_default_timezone_set('Asia/Kolkata');
	$currentTime = date('Y-m-d H:i:s', time());

	$sql = mysqli_query($con, "UPDATE category SET categoryName='$category', categoryDescription='$description', updationDate='$currentTime' WHERE id='$id'");

	if ($sql) {
		log_activity("Updated Category", "Category ID: $id, Name: $category");
		$_SESSION['msg'] = "Category updated successfully!";
	} else {
		$_SESSION['error'] = "Error updating category: " . mysqli_error($con);
	}
	header('location: manage-categories.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>Edit Category - 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-edit me-2"></i> Edit Category</h1>
							<p>Update category details</p>
						</div>
					</div>

					<div class="row justify-content-center">
						<div class="col-lg-6 col-md-8">
							<!-- Form Card -->
							<div class="form-card">
								<?php
								$id = intval($_GET['id']);
								$query = mysqli_query($con, "SELECT * FROM category WHERE id='$id'");
								while ($row = mysqli_fetch_array($query)) {
									?>
									<form method="post">
										<div class="mb-4">
											<label class="form-label">Category Name *</label>
											<input type="text" name="category" placeholder="Enter category name"
												value="<?php echo htmlentities($row['categoryName']); ?>"
												class="form-control" required>
										</div>

										<div class="mb-4">
											<label class="form-label">Description</label>
											<textarea name="description" placeholder="Enter category description"
												class="form-control"
												rows="5"><?php echo htmlentities($row['categoryDescription']); ?></textarea>
										</div>

										<button type="submit" name="submit" class="btn btn-primary w-100 mb-3">
											<i class="fas fa-save me-2"></i> Update Category
										</button>

										<a href="manage-categories.php" class="btn btn-light w-100">
											<i class="fas fa-arrow-left me-2"></i> Back to Categories
										</a>
									</form>
								<?php } ?>
							</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>