<?php
include '../db/db.php';
session_start();

$log = htmlspecialchars(trim($_SESSION['user']), ENT_QUOTES, 'UTF-8');
$log_lang = htmlspecialchars(trim($_SESSION['lang']), ENT_QUOTES, 'UTF-8');
$menu = htmlspecialchars(trim($_SESSION['menu']), ENT_QUOTES, 'UTF-8');
$emp_id = htmlspecialchars(trim($_SESSION['emp_id']), ENT_QUOTES, 'UTF-8');

if (empty($log)) {     
	$redirectUrl = "../";
	echo "<script type=\"text/javascript\">";
	echo "window.location.href = '$redirectUrl'";
	echo "</script>";
	 
} else {
	$sqlu = "SELECT * FROM user_priv_q WHERE emp_id='$emp_id' AND user='$log' AND menu_id='$menu'";
	$stmtu = mysqli_query($conn, $sqlu);
	$countu = mysqli_num_rows($stmtu);
	
	if($countu == 1) {
		$rowu = mysqli_fetch_assoc($stmtu);
		
		date_default_timezone_set('Asia/Colombo');
		$date = date("Y-m-d");
		
		include("resize_class.php");
		function GetImageExtension($imagetype) {
			if(empty($imagetype)) return false;
			switch($imagetype) {
				case 'image/jpeg': return '.jpg';
				case 'image/png': return '.png';
				default: return false;
			}
		}
		
		/*************************** Banner 1 ***************************/

		if(isset($_FILES["file_name1"]["name"])) {
			$bimg = strtolower(htmlspecialchars(strip_tags(trim($_FILES["file_name1"]["name"])), ENT_QUOTES, 'UTF-8'));
			$sql_bn = mysqli_query($conn, "SELECT * FROM side_banners");
				
			if (empty($bimg)) {
				echo 'empty';
			
			} else {
				$tmp1 = $_FILES["file_name1"]["tmp_name"];
				$imgtype = $_FILES["file_name1"]["type"];
				$ext1 = GetImageExtension($imgtype);
				
				if($ext1) {
					$img = 'NWPED_'.rand(1000, 9999).$ext1;
					$target_path = '../upload/side_banners/img/'.$img; // upload directory

					if(move_uploaded_file($tmp1, $target_path)) {
						/*Image thumbnail resizer with resize class*/
						$resize = new ResizeImage($target_path);
						$resize->resizeTo(350, 170, 'exact');
						$r = $resize->saveImage('../upload/side_banners/'.$img);
					
						if($r) {
							if(mysqli_num_rows($sql_bn) == 0) {
								$sql = "INSERT INTO side_banners SET banner_1='$img'";
								$old_img = '';
								
							} else {
								$res_bn = mysqli_fetch_assoc($sql_bn);
								$old_img = $res_bn['banner_1'];
								$sql = "UPDATE side_banners SET banner_1='$img'";
							}
							$stmt_log = mysqli_query($conn, $sql);

							if( $stmt_log === false ) {
								echo 'error';
							
							} else {
								if(!empty($old_img)) {
									$path_old1 = "../upload/side_banners/img/".$old_img;
									$path_old2 = "../upload/side_banners/".$old_img;
									
									if(file_exists($path_old1)) { unlink($path_old1); }
									if(file_exists($path_old2)) { unlink($path_old2); }
								}
								echo 'success';
							}
						
						} else {
							echo 'error2'; // Uploading Error
						}
					
					} else {
						echo 'error2'; // Uploading Error
					}
				
				} else {
					echo 'error3'; // Invaild File
				}
			}
		}
		
		/************************ Banner 2 ************************/
	    
		if(isset($_FILES["file_name2"]["name"])) {
			$bimg = strtolower(htmlspecialchars(strip_tags(trim($_FILES["file_name2"]["name"])), ENT_QUOTES, 'UTF-8'));
			$sql_bn = mysqli_query($conn, "SELECT * FROM side_banners");
				
			if (empty($bimg)) {
				echo 'empty';
			
			} else {
				$tmp1 = $_FILES["file_name2"]["tmp_name"];
				$imgtype = $_FILES["file_name2"]["type"];
				$ext1 = GetImageExtension($imgtype);
				
				if($ext1) {
					$img = 'NWPED_'.rand(1000, 9999).$ext1;
					$target_path = '../upload/side_banners/img/'.$img; // upload directory

					if(move_uploaded_file($tmp1, $target_path)) {
						/*Image thumbnail resizer with resize class*/
						$resize = new ResizeImage($target_path);
						$resize->resizeTo(350, 170, 'exact');
						$r = $resize->saveImage('../upload/side_banners/'.$img);
					
						if($r) {
							if(mysqli_num_rows($sql_bn) == 0) {
								$sql = "INSERT INTO side_banners SET banner_2='$img'";
								$old_img = '';
								
							} else {
								$res_bn = mysqli_fetch_assoc($sql_bn);
								$old_img = $res_bn['banner_2'];
								$sql = "UPDATE side_banners SET banner_2='$img'";
							}
							$stmt_log = mysqli_query($conn, $sql);

							if( $stmt_log === false ) {
								echo 'error';
							
							} else {
								if(!empty($old_img)) {
									$path_old1 = "../upload/side_banners/img/".$old_img;
									$path_old2 = "../upload/side_banners/".$old_img;
									
									if(file_exists($path_old1)) { unlink($path_old1); }
									if(file_exists($path_old2)) { unlink($path_old2); }
								}
								echo 'success';
							}
						
						} else {
							echo 'error2'; // Uploading Error
						}
					
					} else {
						echo 'error2'; // Uploading Error
					}
				
				} else {
					echo 'error3'; // Invaild File
				}
			}
		}
		
		/***************************** Remove ***************************/
		
	    if (array_key_exists('remove_data_bn1', $_POST)) {
			$sql_bn = mysqli_fetch_assoc(mysqli_query($conn, "SELECT banner_1 FROM side_banners"));
			$old_img = $sql_bn['banner_1'];
			
			$sql = "UPDATE side_banners SET banner_1=''";
			$stmt_log = mysqli_query($conn, $sql);
			
			if( $stmt_log === false ) {
				echo json_encode(array("msgType" => 2, "msg" => "Sorry ! not deleted"));
			
			} else {
				if(!empty($old_img)) {
					$path_old1 = "../upload/side_banners/img/".$old_img;
					$path_old2 = "../upload/side_banners/".$old_img;
					
					if(file_exists($path_old1)) { unlink($path_old1); }
					if(file_exists($path_old2)) { unlink($path_old2); }
				}
				echo json_encode(array("msgType" => 1, "msg" => "Successfully Removed"));
			}
	    }
		
		if (array_key_exists('remove_data_bn2', $_POST)) {
			$sql_bn = mysqli_fetch_assoc(mysqli_query($conn, "SELECT banner_2 FROM side_banners"));
			$old_img = $sql_bn['banner_2'];
			
			$sql = "UPDATE side_banners SET banner_2=''";
			$stmt_log = mysqli_query($conn, $sql);
			
			if( $stmt_log === false ) {
				echo json_encode(array("msgType" => 2, "msg" => "Sorry ! not deleted"));
			
			} else {
				if(!empty($old_img)) {
					$path_old1 = "../upload/side_banners/img/".$old_img;
					$path_old2 = "../upload/side_banners/".$old_img;
					
					if(file_exists($path_old1)) { unlink($path_old1); }
					if(file_exists($path_old2)) { unlink($path_old2); }
				}
				echo json_encode(array("msgType" => 1, "msg" => "Successfully Removed"));
			}
	    }
		
		/*******************************************************/
		
	} else {
		$redirectUrl = "../";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";
	}
}
?>