<?php
SESSION_START();
include 'mysql.php';

if(isset($_GET['logout'])){
    session_destroy();
    // echo '<META HTTP-EQUIV="refresh" CONTENT="0">';
    header('Location: ../index.php');
}

if(!isset($_SESSION['admin'])):
	header('Location: ../404.html');

	

else:

foreach($_GET as $key => $id){
}

	ini_set('display_errors', 1);
	ini_set('display_startup_errors', 1);
	error_reporting(E_ALL);

	$errors = [];

	if(isset($_POST['update'])){
	if(!empty($errors)){

	}else{
		$title = mysqli_real_escape_string($dbc, $_REQUEST['title']);
		$description = mysqli_real_escape_string($dbc, $_REQUEST['description']);

		$query = "UPDATE images SET title = '$title', description = '$description' WHERE id=$id";
		if(mysqli_query($dbc, $query)){
			header('Location: admin_panel.php');
			
		}else{
			$errors = "faail";
		}
	}
}

$sql = "SELECT title, description FROM images WHERE id=$id";
$result = mysqli_query($dbc, $sql);

if($result->num_rows > 0):
	while($row = $result->fetch_assoc()):
		$title = $row['title'];
		$description = $row['description'];

include '../includes/header.html';
?>

<body>
<?php
include '../includes/navigation.php';

?>
<div class="container" style="margin-top: 4em;">
	<?php
		if(!empty($errors)){
		echo '<div class="alert alert-danger" role="alert"><li>'.$errors.'</li></div>';
	}
	?>
<div class="form-container"  style="padding-top: 66px; display: flex; align-items: center; justify-content: center; background-position: center;">
	<form method="post" action="" enctype="multipart/form-data" style="width: 56%;">
		<div class="form-group">
			<label>Title</label>
			<input class="form-control" name="title" placeholder="Title" value="<?php echo $title ?>">
		</div>
		<div class="form-group">
			<label>Description</label>
			<input class="form-control" name="description" placeholder="Description" value="<?php echo $description ?>">
		</div>
		<div class="form-group">
			<input class="form-control" type="submit" name="update" value="Update">
		</div>
	</form>
</div>
</div>

<?php
endwhile;
endif;
endif;
?>
<?php include '../includes/scripts.html'; ?>
</body>
</html>