<?php
session_start();
include_once('includes/config.php');
if (strlen($_SESSION["aid"]) == 0) {
	header('location:logout.php');
	exit;
} else {
	$pid = intval($_GET['id']); // product id
	if (isset($_POST['submit'])) {
		$productname = $_POST['productName'];
		$productimage1 = $_FILES["productimage1"]["name"];
		move_uploaded_file($_FILES["productimage1"]["tmp_name"], "productimages/$pid/" . $_FILES["productimage1"]["name"]);
		$sql = mysqli_query($con, "update products set productImage1='$productimage1' where id='$pid' ");
		$_SESSION['msg'] = "Product Image 1 Updated Successfully!";
	}
	?>
	<!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="Update Product Image 1 - Awesome Mall Admin" />
		<meta name="author" content="" />
		<title>Awesome Mall | Update Product Image 1</title>
		<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-image me-2"></i> Update Product Image 1</h1>
								<p>Update the primary product image</p>
							</div>
						</div>

						<!-- Success Message -->
						<?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 } ?>

						<!-- Update Form -->
						<div class="table-card">
							<div class="card-header">
								<h5 class="mb-0"><i class="fas fa-upload me-2"></i>Upload New Image</h5>
							</div>
							<div class="card-body">
								<?php
								$query = mysqli_query($con, "select productName,productImage1 from products where id='$pid'");
								$cnt = 1;
								while ($row = mysqli_fetch_array($query)) {
									?>
									<form method="post" enctype="multipart/form-data">
										<!-- Product Name (Read-only) -->
										<div class="mb-4">
											<label for="productName" class="form-label fw-semibold">Product Name</label>
											<input type="text" name="productName" id="productName" readonly
												value="<?php echo htmlentities($row['productName']); ?>"
												class="form-control bg-light">
										</div>

										<!-- Current Image Preview -->
										<div class="mb-4">
											<label class="form-label fw-semibold">Current Product Image 1</label>
											<div class="border rounded p-3 bg-light">
												<img src="productimages/<?php echo htmlentities($pid); ?>/<?php echo htmlentities($row['productImage1']); ?>"
													class="img-fluid rounded" style="max-width: 400px; max-height: 300px;"
													alt="Current Product Image">
											</div>
										</div>

										<!-- New Image Upload -->
										<div class="mb-4">
											<label for="productimage1" class="form-label fw-semibold">
												New Product Image 1 <span class="text-danger">*</span>
											</label>
											<input type="file" name="productimage1" id="productimage1" class="form-control"
												accept="image/*" required>
											<div class="form-text">
												<i class="fas fa-info-circle me-1"></i>
												Recommended: JPG, PNG or GIF (Max 5MB)
											</div>
										</div>

										<!-- Submit Button -->
										<div class="d-flex gap-2">
											<button type="submit" name="submit" class="btn btn-primary">
												<i class="fas fa-save me-2"></i>Update Image
											</button>
											<a href="manage-products.php" class="btn btn-outline-secondary">
												<i class="fas fa-arrow-left me-2"></i>Back to Products
											</a>
										</div>
									</form>
								<?php } ?>
							</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>
	</body>

	</html>
<?php } ?>
