<?php
session_start();
include_once('includes/config.php');
if (strlen($_SESSION["aid"]) == 0) {
	header('location:logout.php');
	exit;
} else {
	date_default_timezone_set('Asia/Kolkata'); // change according timezone
	$currentTime = date('d-m-Y h:i:s A', time());

	if (isset($_POST['submit'])) {
		$category = $_POST['category'];
		$description = $_POST['description'];
		$sql = mysqli_query($con, "insert into category(categoryName,categoryDescription) values('$category','$description')");
		$_SESSION['msg'] = "Category Created!";
	}

	if (isset($_GET['del'])) {
		mysqli_query($con, "delete from category where id = '" . $_GET['id'] . "'");
		$_SESSION['delmsg'] = "Category 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="Create Category - Awesome Mall Admin" />
		<meta name="author" content="" />
		<title>Awesome Mall | Create 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-tags me-2"></i> Create Category</h1>
								<p>Add new product category and manage existing categories</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 Category 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 Category</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 Name <span
													class="text-danger">*</span></label>
											<input type="text" placeholder="Enter category name" name="category"
												id="category" class="form-control" required>
										</div>
										<div class="col-md-6 mb-3">
											<label for="description" class="form-label fw-semibold">Description</label>
											<textarea class="form-control" name="description" id="description" rows="3"
												placeholder="Enter category description"></textarea>
										</div>
									</div>
									<button type="submit" name="submit" class="btn btn-primary">
										<i class="fas fa-save me-2"></i>Create Category
									</button>
								</form>
							</div>
						</div>

						<!-- Manage Categories Table -->
						<div class="table-card">
							<div class="card-header">
								<h5 class="mb-0"><i class="fas fa-list me-2"></i>Manage Categories</h5>
							</div>
							<div class="card-body">
								<table class="table table-hover" id="datatablesSimple">
									<thead>
										<tr>
											<th>#</th>
											<th>Category</th>
											<th>Description</th>
											<th>Creation Date</th>
											<th>Last Updated</th>
											<th>Action</th>
										</tr>
									</thead>
									<tbody>
										<?php
										$query = mysqli_query($con, "select * from category");
										$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['categoryDescription']); ?></td>
												<td><?php echo htmlentities($row['creationDate']); ?></td>
												<td><?php echo htmlentities($row['updationDate']); ?></td>
												<td>
													<a href="edit-category.php?id=<?php echo $row['id'] ?>"
														class="btn btn-sm btn-outline-primary" title="Edit Category">
														<i class="fas fa-edit"></i>
													</a>
													<a href="category.php?id=<?php echo $row['id'] ?>&del=delete"
														class="btn btn-sm btn-outline-danger" title="Delete Category"
														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>
<?php } ?>
