<?php
SESSION_START();

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

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:

	// ini_set('display_errors', 1);
	// ini_set('display_startup_errors', 1);
	// error_reporting(E_ALL);
	  
	$image = ($_FILES['file1']['name']);
	$title = mysqli_real_escape_string($dbc, $_REQUEST['title']);
	$description = mysqli_real_escape_string($dbc, $_REQUEST['description']);

	$extentions = array("jpg", "jpeg", "png", "gif");
	$target_dir = "../images/work/";

	$errors = [];

	function compress($source, $destination, $quality) {

		$info = getimagesize($source);

		if ($info['mime'] == 'image/jpeg') 
			$image = imagecreatefromjpeg($source);

		elseif ($info['mime'] == 'image/gif') 
			$image = imagecreatefromgif($source);

		elseif ($info['mime'] == 'image/png') 
			$image = imagecreatefrompng($source);

		imagejpeg($image, $destination, $quality);

		return $destination;
	}

	function resize($destination){
			$thumb = new Imagick($destination);

			$thumb->resizeImage(1280,720,Imagick::FILTER_LANCZOS,1);
			$thumb->writeImage($destination);

			$thumb->destroy();

		}


		if(!empty($_FILES['file1']['name'])){
			$file_type = strtolower(pathinfo($_FILES['file1']['name'],PATHINFO_EXTENSION));
			$file_size = filesize($_FILES['file1']['tmp_name']);

			if(!in_array($file_type, $extentions)){
				$errors = "Image is not jpg, jpeg, png or gif";
			}else{
				$destination = $target_dir.htmlspecialchars($_FILES['file1']['name']);
				if ($file_size >= 1000000){
					compress($_FILES['file1']['tmp_name'], $destination, 30);
			}
			if(!resize($_FILES['file1']['tmp_name'])){
				echo $_FILES['file1']['tmp_name'];
			}
                move_uploaded_file($_FILES['file1']['tmp_name'], $target_dir.$_FILES['file1']['name']);
		}
	}else{
		$errors = "Add image Bran come on..."; 
	}


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

	}else{
		$query = "INSERT INTO images(name, title, description) VALUES('$image', '$title', '$description')";
		if(mysqli_query($dbc, $query)){
			// header('Location: admin_panel.php');
			
		}else{
			$errors = "faial";
		}
	}
}

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="">
		</div>
		<div class="form-group">
			<label>Description</label>
			<input class="form-control" name="description" placeholder="Description" value="">
		</div>
		<div class="form-group">
			<input type="file" name="file1" class="form-control">
		</div>
		<div class="form-group">
			<input class="form-control" type="submit" name="submit" value="Add">
		</div>
	</form>
</div>
</div>

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