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

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

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

	$sql = mysqli_query($con, "UPDATE subcategory SET categoryid='$category', subcategory='$subcat', updationDate='$currentTime' WHERE id='$id'");

	if ($sql) {
		log_activity("Updated Subcategory", "Subcategory ID: $id, Name: $subcat");
		$_SESSION['msg'] = "Subcategory updated successfully!";
	} else {
		$_SESSION['error'] = "Error updating 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>Edit 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-edit me-2"></i> Edit Subcategory</h1>
							<p>Update subcategory 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']);
								// Fetch the subcategory and its current category
								$query = mysqli_query($con, "SELECT subcategory.*, category.categoryName as currentCategoryName FROM subcategory JOIN category ON category.id=subcategory.categoryid WHERE subcategory.id='$id'");
								while ($row = mysqli_fetch_array($query)) {
									?>
									<form method="post">
										<div class="mb-4">
											<label class="form-label">Parent Category *</label>
											<select name="category" class="form-select" required>
												<option value="">Select Category</option>
												<?php
												$cat_query = mysqli_query($con, "SELECT * FROM category");
												while ($cat_row = mysqli_fetch_array($cat_query)) {
													$selected = ($cat_row['id'] == $row['categoryid']) ? 'selected' : '';
													?>
													<option value="<?php echo $cat_row['id']; ?>" <?php echo $selected; ?>>
														<?php echo htmlentities($cat_row['categoryName']); ?>
													</option>
												<?php } ?>
											</select>
										</div>

										<div class="mb-4">
											<label class="form-label">Subcategory Name *</label>
											<input type="text" name="subcategory" placeholder="Enter subcategory name"
												value="<?php echo htmlentities($row['subcategory']); ?>"
												class="form-control" required>
										</div>

										<button type="submit" name="submit" class="btn btn-primary w-100 mb-3">
											<i class="fas fa-save me-2"></i> Update 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>
								<?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>