<?php
header('Content-type: application/json');
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 = "index.php";
	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'";
	$resultu = mysqli_query($conn, $sqlu);
	$countu = mysqli_num_rows($resultu);

	if($countu == 1){
		$rowc = mysqli_fetch_assoc($resultu);
		$user_id = $rowc['user_id'];
		
		date_default_timezone_set('Asia/Colombo');
		$date_today = 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;
			}
		}

		/****************************** Insert Eng *************************************/

		if(isset($_FILES['img_upload'])) {	
			if($_FILES["img_upload"]["name"] != '') {
				$title = htmlspecialchars(strip_tags(trim($_POST['title'])), ENT_QUOTES, 'UTF-8');
				$date = htmlspecialchars(strip_tags(trim($_POST['date'])), ENT_QUOTES, 'UTF-8');
				$des = htmlspecialchars(strip_tags(trim($_POST['des'])), ENT_QUOTES, 'UTF-8');	
				
				$group_no = mysqli_fetch_assoc(mysqli_query($conn, "SELECT main_banner FROM group_id_tbl"));
				$g_no = $group_no['main_banner'];
				
				$tmp1 = $_FILES["img_upload"]["tmp_name"];
				$imgtype = $_FILES["img_upload"]["type"];
				$ext1 = GetImageExtension($imgtype);
				
				if($ext1) {
					$img = 'NWPED_'.rand(100000, 999999).$ext1;
					$target_path = '../upload/main_banner/img/'.$img; // upload directory

					if(move_uploaded_file($tmp1, $target_path)) {
						/*Image thumbnail resizer with resize class*/
						$resize = new ResizeImage($target_path);
						$resize->resizeTo(840, 537, 'exact');
						$r = $resize->saveImage('../upload/main_banner/'.$img);
					
						if($r) {
							if($log_lang == 1) {	
								$sql = "INSERT INTO main_banner VALUES (NULL, '$title', '$des','$date', '$img', '$user_id', '$g_no', '1', '0', '$date_today')";

								if(mysqli_query($conn, $sql)) {
									$g_no2 = $g_no+1;
									$group_no_up = mysqli_query($conn, "UPDATE group_id_tbl SET main_banner='$g_no2'");
									
									$response['status'] = '1'; // Success
								
								} else {
									$response['status'] = '3'; // Sql Error
								}
							}
						
						} else {
							echo 'error2'; // Uploading Error
						}
					
					} else {
						$response['status'] = '4'; // Uploading Error
					}
				
				} else {
					$response['status'] = '5'; // Invaild File
				}
			}
			echo json_encode($response);	
		}
		
		/****************************** Insert Sinhala/Tamil *************************************/
		
		if (array_key_exists('add_banner_lang', $_POST)) {
			$title = htmlspecialchars(strip_tags(trim($_POST['title'])), ENT_QUOTES, 'UTF-8');
			$group_id = htmlspecialchars(strip_tags(trim($_POST['group_id'])), ENT_QUOTES, 'UTF-8');
			$des = htmlspecialchars(strip_tags(trim($_POST['des'])), ENT_QUOTES, 'UTF-8');
			$lang = htmlspecialchars(strip_tags(trim($_POST['lang'])), ENT_QUOTES, 'UTF-8');

			$sqlg = mysqli_query($conn, "SELECT * FROM main_banner WHERE group_id='$group_id' AND lang='$lang'");
			
			if(mysqli_num_rows($sqlg) == 0) {
				$sql_eng = mysqli_fetch_assoc(mysqli_query($conn, "SELECT date, img FROM main_banner WHERE group_id='$group_id' AND lang=1"));
				$date = $sql_eng['date'];	
				$img = $sql_eng['img'];	
				
				$sql = "INSERT INTO main_banner VALUES (NULL, '$title', '$des','$date', '$img', '$user_id', '$group_id', '$lang', '0', '$date_today')";
			
				if(mysqli_query($conn, $sql)) {
					echo json_encode(array("msgType" => 1, "msg" => "Successfully Added"));
				
				} else {
					echo json_encode(array("msgType" => 2, "msg" => "Sorry ! Could not be Save Data"));
				}			
			
			} else {
				echo json_encode(array("msgType" => 3, "msg" => "Data Already Exist"));
			}
		}
		
		/****************************** Remove *************************************/
		
		if (array_key_exists('remove_banner', $_POST)) {

			if($log_lang == 1) {
			
				$group_id = htmlspecialchars(strip_tags(trim($_POST['gid'])), ENT_QUOTES, 'UTF-8');
				$sql = "UPDATE main_banner SET del=1 WHERE group_id='$group_id'";
				
				if(!empty($group_id)) {
				
					if(mysqli_query($conn, $sql)) {
						echo json_encode(array("msgType" => 1, "msg" => "Successfully Removed"));
					
					} else {
						echo json_encode(array("msgType" => 2, "msg" => "Sorry ! not deleted"));
					}
				
				} else {
					echo json_encode(array("msgType" => 2, "msg" => "Sorry ! empty id"));
				}
			}
		}		
		
		/****************************** Load Form Data *************************************/
		
		if (array_key_exists('get_dataset', $_POST)) {

			$group_id = htmlspecialchars(strip_tags(trim($_POST['gid'])), ENT_QUOTES, 'UTF-8');
			$lang = htmlspecialchars(strip_tags(trim($_POST['lang'])), ENT_QUOTES, 'UTF-8');
			
			$data = array();
			$sql = mysqli_query($conn, "SELECT title, description, date, img FROM main_banner WHERE group_id=$group_id AND lang='$lang'");

			while ($row = mysqli_fetch_assoc($sql)) {
				$data[] = $row;
			}
			echo json_encode($data);
		}
		
		/****************************** Update Eng/Sinhala/Tamil *************************************/
		
		if (array_key_exists('update_banner_lang', $_POST)) {
			$group_id = htmlspecialchars(strip_tags(trim($_POST['group_id'])), ENT_QUOTES, 'UTF-8');
			$lang = htmlspecialchars(strip_tags(trim($_POST['lang'])), ENT_QUOTES, 'UTF-8');
			
			$des = htmlspecialchars(strip_tags(trim($_POST['des'])), ENT_QUOTES, 'UTF-8');
			$title = htmlspecialchars(strip_tags(trim($_POST['title'])), ENT_QUOTES, 'UTF-8');
			$date = htmlspecialchars(strip_tags(trim($_POST['date'])), ENT_QUOTES, 'UTF-8');			

			if($log_lang == 1) {
				$sql = mysqli_query($conn, "UPDATE main_banner SET date='$date' WHERE group_id='$group_id'");
			}

			$sql_up = mysqli_query($conn, "UPDATE main_banner SET title='$title', description='$des', user='$user_id' WHERE group_id='$group_id' AND lang='$lang'");

			if($sql_up) {
				echo json_encode(array("msgType" => 1, "msg" => "Successfully Added"));
			
			} else {
				echo json_encode(array("msgType" => 2, "msg" => "Sorry ! Could not be Save Data"));
			}
		}

		/****************************** Update Image *************************************/

		if(isset($_FILES['img_upload_update'])) {	
			if($_FILES["img_upload_update"]["name"] != '') {
				$group_id = htmlspecialchars(strip_tags(trim($_POST['group_id'])), ENT_QUOTES, 'UTF-8');
				$des = htmlspecialchars(strip_tags(trim($_POST['des'])), ENT_QUOTES, 'UTF-8');
				$title = htmlspecialchars(strip_tags(trim($_POST['title'])), ENT_QUOTES, 'UTF-8');
				$date = htmlspecialchars(strip_tags(trim($_POST['date'])), ENT_QUOTES, 'UTF-8');
				
				$sql_eng = mysqli_fetch_assoc(mysqli_query($conn, "SELECT img FROM main_banner WHERE group_id='$group_id' AND lang=1"));
				$old_img = $sql_eng['img'];
				
				$tmp1 = $_FILES["img_upload_update"]["tmp_name"];
				$imgtype = $_FILES["img_upload_update"]["type"];
				$ext1 = GetImageExtension($imgtype);
				
				if($ext1) {
					$img = 'NWPED_'.rand(100000, 999999).$ext1;
					$target_path = '../upload/main_banner/img/'.$img; // upload directory

					if(move_uploaded_file($tmp1, $target_path)) {
						/*Image thumbnail resizer with resize class*/
						$resize = new ResizeImage($target_path);
						$resize->resizeTo(840, 537, 'exact');
						$r = $resize->saveImage('../upload/main_banner/'.$img);
					
						if($r) {
							if($log_lang == 1) {					
								$sql = "UPDATE main_banner SET img='$img', date='$date' WHERE group_id='$group_id'";
								$sql_up = "UPDATE main_banner SET title='$title', description='$des', user='$user_id' WHERE group_id='$group_id' AND lang='1'";
								
								if(mysqli_query($conn, $sql)&& mysqli_query($conn, $sql_up)) {
									if(!empty($old_img)) {
										$path_old1 = "../upload/main_banner/img/".$old_img;
										$path_old2 = "../upload/main_banner/".$old_img;
										
										if(file_exists($path_old1)) { unlink($path_old1); }
										if(file_exists($path_old2)) { unlink($path_old2); }
									}
									$response['status'] = '1'; // Success
								
								} else {
									$response['status'] = '3'; // Sql Error
								}
							}
						
						} else {
							echo 'error2'; // Uploading Error
						}
					
					} else {
						$response['status'] = '4'; // Uploading Error
					}
				
				} else {
					$response['status'] = '5'; // Invaild File
				}
			}
			echo json_encode($response);	
		}		

		/******************************************************************/		

	} else{
		$redirectUrl = "index.php";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";	
	}
}
?>