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

if(isset($_SESSION['userID'])) {
	$log = htmlspecialchars(strip_tags(trim($_SESSION['userID'])), ENT_QUOTES, 'UTF-8');
} else {
	$log = '';
}

if (empty($log)) {     
	$redirectUrl = "../";
	echo "<script type=\"text/javascript\">";
	echo "window.location.href = '$redirectUrl'";
	echo "</script>";
	 
} else {
	$sqlu = "SELECT id FROM user_acc_tbl WHERE userID='$log' and access=1 and val=1";
	$stmtu = mysqli_query($conn, $sqlu);
	$countu = mysqli_num_rows($stmtu);
	
	if($countu == 1) {
		$rowu = mysqli_fetch_assoc($stmtu);
		$uid = $rowu['id'];
		
		date_default_timezone_set('Asia/Colombo');
		$date = date("Y-m-d");
		
		/*************************** 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 prom_banner");
				
			if (empty($bimg)) {
				echo 'empty';
			
			} else {
				// get uploaded file's extension
				$ext = strtolower(pathinfo($_FILES["file_name1"]["name"], PATHINFO_EXTENSION));
				$img = 'buddhistbooks-prom-sri-lanka-'.rand(1000, 9999).'.'.$ext;
				
				$valid_extensions = array('png','jpg','jpeg'); // valid extensions
				$path = '../../image/bg-images/'; // upload directory
				$tmp = $_FILES['file_name1']['tmp_name'];
				
				// check valid format
				if(in_array($ext, $valid_extensions)) {
					$path = $path.$img;	
						
					if (file_exists($path)) {			
						echo 'exist'; // File Exists
					
					} else {		
						if(move_uploaded_file($tmp,$path)) {
							if(mysqli_num_rows($sql_bn) == 0) {
								$sql = "INSERT INTO prom_banner SET banner_1='$img'";
								$old_img = '';
								
							} else {
								$res_bn = mysqli_fetch_assoc($sql_bn);
								$old_img = $res_bn['banner_1'];
								$sql = "UPDATE prom_banner SET banner_1='$img'";
							}
							$stmt_log = mysqli_query($conn, $sql);

							if( $stmt_log === false ) {
								echo 'error';
							
							} else {
								if(!empty($old_img)) {
									$path_old = "../../image/bg-images/".$old_img;
									$files = glob($path_old);
									foreach($files as $file) {
										unlink($file);
									}
								}
								echo 'success';
							}
						
						} 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 prom_banner");
				
			if (empty($bimg)) {
				echo 'empty';
			
			} else {
				// get uploaded file's extension
				$ext = strtolower(pathinfo($_FILES["file_name2"]["name"], PATHINFO_EXTENSION));
				$img = 'buddhistbooks-prom-sri-lanka-'.rand(1000, 9999).'.'.$ext;
				
				$valid_extensions = array('png','jpg','jpeg'); // valid extensions
				$path = '../../image/bg-images/'; // upload directory
				$tmp = $_FILES['file_name2']['tmp_name'];
				
				// check valid format
				if(in_array($ext, $valid_extensions)) {
					$path = $path.$img;	
						
					if (file_exists($path)) {			
						echo 'exist'; // File Exists
					
					} else {		
						if(move_uploaded_file($tmp,$path)) {
							if(mysqli_num_rows($sql_bn) == 0) {
								$sql = "INSERT INTO prom_banner SET banner_2='$img'";
								$old_img = '';
								
							} else {
								$res_bn = mysqli_fetch_assoc($sql_bn);
								$old_img = $res_bn['banner_2'];
								$sql = "UPDATE prom_banner SET banner_2='$img'";
							}
							$stmt_log = mysqli_query($conn, $sql);

							if( $stmt_log === false ) {
								echo 'error';
							
							} else {
								if(!empty($old_img)) {
									$path_old = "../../image/bg-images/".$old_img;
									$files = glob($path_old);
									foreach($files as $file) {
										unlink($file);
									}
								}
								echo 'success';
							}
						
						} 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 prom_banner"));
			$old_img = $sql_bn['banner_1'];
			
			$sql = "UPDATE prom_banner 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_old = "../../image/bg-images/".$old_img;
					$files = glob($path_old);
					foreach($files as $file) {
						unlink($file);
					}
				}
				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 prom_banner"));
			$old_img = $sql_bn['banner_2'];
			
			$sql = "UPDATE prom_banner 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_old = "../../image/bg-images/".$old_img;
					$files = glob($path_old);
					foreach($files as $file) {
						unlink($file);
					}
				}
				echo json_encode(array("msgType" => 1, "msg" => "Successfully Removed"));
			}
	    }
		
		/*******************************************************/
		
	} else {
		$redirectUrl = "../";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";
	}
}
?>