<?php require_once('header.php'); ?>

<?php
// if(isset($_POST['form1'])) {
// 	$valid = 1;

// 	if(empty($_POST['caption'])) {
//         $valid = 0;
//         $error_message .= "Photo Caption Name can not be empty<br>";
//     }

//     $path = $_FILES['photo']['name'];
//     $path_tmp = $_FILES['photo']['tmp_name'];

//     if($path != '') {
//     	$ext = pathinfo( $path, PATHINFO_EXTENSION );
//         $file_name = basename( $path, '.' . $ext );
//         if( $ext!='jpg' && $ext!='png' && $ext!='jpeg' && $ext!='gif' ) {
//             $valid = 0;
//             $error_message .= 'You must have to upload jpg, jpeg, gif or png file<br>';
//         }
//     }

//     if($valid == 1) {


//     		unlink('assets/images/certificate/'.$_POST['previous_photo']);

//     		$final_name = 'photo-'.$_REQUEST['id'].'.'.$ext;
//         	move_uploaded_file( $path_tmp, 'assets/images/certificate/'.$final_name );

//         	// updating into the database
// 			$statement = $pdo->prepare("UPDATE tbl_certificate SET photo=? WHERE id=?");
// 			$statement->execute(array($final_name,$_REQUEST['id']));


//     	$success_message = 'Certificate is updated successfully.';
//     }
// }
?>
<!--new-->
<?php
$error_message = '';
$success_message = '';
$photo = ''; // Fallback in case not defined earlier

// Get existing photo data
if (isset($_GET['id'])) {
	$id = $_GET['id'];

	$statement = $pdo->prepare("SELECT * FROM tbl_certificate WHERE id = ?");
	$statement->execute([$id]);
	$result = $statement->fetch(PDO::FETCH_ASSOC);

	if ($result) {
		$photo = $result['photo'];
	} else {
		$error_message = "Invalid certificate ID.";
	}
}

// Handle form submission
if (isset($_POST['form1'])) {
	$valid = 1;

	if (isset($_FILES['photo']['name']) && $_FILES['photo']['name'] != '') {
		$path = $_FILES['photo']['name'];
		$path_tmp = $_FILES['photo']['tmp_name'];
		$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));

		if (!in_array($ext, ['jpg', 'jpeg', 'png', 'gif','webp'])) {
			$valid = 0;
			$error_message .= 'Only JPG, JPEG, PNG, WEBP, and GIF files are allowed.<br>';
		} else {
			$final_name = 'photo-' . $_POST['id'] . '.' . $ext;

			// Delete old photo if exists
			if (!empty($_POST['previous_photo']) && file_exists('assets/images/certificate/' . $_POST['previous_photo'])) {
				unlink('assets/images/certificate/' . $_POST['previous_photo']);
			}

			// Move new file
			move_uploaded_file($path_tmp, 'assets/images/certificate/' . $final_name);

			// Update database
			$statement = $pdo->prepare("UPDATE tbl_certificate SET photo = ? WHERE id = ?");
			$statement->execute([$final_name, $_POST['id']]);

			$success_message = "Certificate image updated successfully.";
			$photo = $final_name;
		}
	} else {
		$error_message .= 'Please choose a file to upload.<br>';
	}
}
?>
<!--end new-->

<?php
if (!isset($_REQUEST['id'])) {
	header('location: logout.php');
	exit;
} else {
	// Check the id is valid or not
	$statement = $pdo->prepare("SELECT * FROM tbl_certificate WHERE id=?");
	$statement->execute(array($_REQUEST['id']));
	$total = $statement->rowCount();
	$result = $statement->fetchAll(PDO::FETCH_ASSOC);
	if ($total == 0) {
		header('location: logout.php');
		exit;
	}
}
?>

<section class="content-header">
	<div class="content-header-left">
		<h1>Edit Certificate</h1>
	</div>
	<div class="content-header-right">
		<a href="certificate.php" class="btn btn-primary btn-sm">View All</a>
	</div>
</section>

<?php
foreach ($result as $row) {
	$caption = $row['caption'];
	$photo = $row['photo'];
}
?>

<section class="content">

	<div class="row">
		<div class="col-md-12">

			<?php if ($error_message): ?>
				<div class="callout callout-danger">
					<p><?php echo $error_message; ?></p>
				</div>
			<?php endif; ?>

			<?php if ($success_message): ?>
				<div class="callout callout-success">
					<p><?php echo $success_message; ?></p>
				</div>
			<?php endif; ?>

			<form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
				<input type="hidden" name="form1" value="1">
				<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
				<input type="hidden" name="previous_photo" value="<?php echo $photo; ?>">

				<div class="box box-info">
					<div class="box-body">

						<div class="form-group">
							<label class="col-sm-2 control-label">Existing Photo</label>
							<div class="col-sm-6" style="padding-top:6px;">
								<?php if ($photo): ?>
									<img src="assets/images/certificate/<?php echo $photo; ?>" style="width:300px;height:300px;">
								<?php else: ?>
									<p>No photo available.</p>
								<?php endif; ?>
							</div>
						</div>

						<div class="form-group">
							<label class="col-sm-2 control-label">Upload New Photo *</label>
							<div class="col-sm-4" style="padding-top:6px;">
								<input type="file" name="photo" required>
							</div>
						</div>

						<div class="form-group">
							<label class="col-sm-2 control-label"></label>
							<div class="col-sm-6">
								<button type="submit" class="btn btn-success">Submit</button>
							</div>
						</div>
					</div>
				</div>
			</form>
		</div>
	</div>
</section>

<?php require_once('footer.php'); ?>