<?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 = "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'];

		/****************************** Insert Eng *************************************/
		
		if (array_key_exists('add_cat_eng', $_POST)) {
			$cat_name = htmlspecialchars(strip_tags(trim($_POST['cat_name'])), ENT_QUOTES, 'UTF-8');

			$group_no = mysqli_fetch_assoc(mysqli_query($conn, "SELECT menu_cat FROM group_id_tbl"));
			$g_no = $group_no['menu_cat'];
			
			$sql_eng = mysqli_query($conn, "SELECT cat FROM menu_category WHERE cat='$cat_name' AND del=0 AND lang=1");

			if($log_lang == 1) {
				if(mysqli_num_rows($sql_eng) == 0) {
					$sql = "INSERT INTO menu_category VALUES (NULL, '$cat_name', '$user_id', '$g_no', '1', '0', '', '')";

					if(mysqli_query($conn, $sql)) {
						$g_no2 = $g_no+1;
						$group_no_up = mysqli_query($conn, "UPDATE group_id_tbl SET menu_cat='$g_no2'");
						
						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"));
				}
			}
		}
		
		/****************************** Insert Sinhala/Tamil *************************************/
		
		if (array_key_exists('add_cat_lang', $_POST)) {
			$cat_name = htmlspecialchars(strip_tags(trim($_POST['cat_name'])), ENT_QUOTES, 'UTF-8');
			
			$group_id = htmlspecialchars(strip_tags(trim($_POST['group_id'])), ENT_QUOTES, 'UTF-8');
			$lang = htmlspecialchars(strip_tags(trim($_POST['lang'])), ENT_QUOTES, 'UTF-8');

			$sqlg = mysqli_query($conn, "SELECT * FROM menu_category WHERE group_id='$group_id' AND lang='$lang'");
			
			if(mysqli_num_rows($sqlg) == 0) {
				$sql_lang = mysqli_query($conn, "SELECT cat FROM menu_category WHERE cat='$cat_name' AND del=0 AND lang='$lang'");
				
				if(mysqli_num_rows($sql_lang) == 0) {
					$sql_eng = mysqli_fetch_assoc(mysqli_query($conn, "SELECT url FROM menu_category WHERE group_id='$group_id' AND lang=1"));
					$url = $sql_eng['url'];						
					
					$sql = "INSERT INTO menu_category VALUES (NULL, '$cat_name', '$user_id', '$group_id', '$lang' , '0', '$url', '')";
				
					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" => 4, "msg" => "Data Already Exist"));
				}					
			
			} else {
				echo json_encode(array("msgType" => 3, "msg" => "Data Already Exist"));
			}
		}
		
		/****************************** Remove *************************************/
		
		if (array_key_exists('remove_cat', $_POST)) {

			if($log_lang == 1) {
			
				$group_id = htmlspecialchars(strip_tags(trim($_POST['gid'])), ENT_QUOTES, 'UTF-8');
				$sql_cat = mysqli_query($conn, "SELECT * FROM menu_subcat WHERE cat_id='$group_id'");
				
				if(mysqli_num_rows($sql_cat) == 0) {
					$sql = "UPDATE menu_category 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"));
					}
				
				} else {
					echo json_encode(array("msgType" => 3, "msg" => "Data Exist"));
				}
			}
		}		
		
		/****************************** 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 cat, url, order_id FROM menu_category WHERE group_id=$group_id AND lang='$lang'");
			
			$sql_sub = mysqli_query($conn, "SELECT * FROM menu_subcat WHERE cat_id='$group_id'");
			$count = mysqli_num_rows($sql_sub);

			while ($row = mysqli_fetch_assoc($sql)) {
				array_push($data, array('cat' => $row['cat'], 'url' => $row['url'], 'order_id' => $row['order_id'], 'count' => $count));
			}
			echo json_encode($data);
		}
		
		/****************************** Update *************************************/
		
		if (array_key_exists('update_cat', $_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');
			
			$cat = htmlspecialchars(strip_tags(trim($_POST['cat'])), ENT_QUOTES, 'UTF-8');
			$url = htmlspecialchars(strip_tags(trim($_POST['page_url'])), ENT_QUOTES, 'UTF-8');
			$oid = htmlspecialchars(strip_tags(trim($_POST['oid'])), ENT_QUOTES, 'UTF-8');
			
			$sql_lang = mysqli_query($conn, "SELECT cat FROM menu_category WHERE cat='$cat' AND del=0 AND lang='$lang' AND group_id!='$group_id'");	
			if(mysqli_num_rows($sql_lang) == 0) {

				if($log_lang == 1) {
					$sql = mysqli_query($conn, "UPDATE menu_category SET url='$url', order_id = '$oid' WHERE group_id='$group_id'");
				}					
				
				$sql_up = mysqli_query($conn, "UPDATE menu_category SET cat='$cat', 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"));
				}
			
			} else {
				echo json_encode(array("msgType" => 3, "msg" => "Data Already Exist"));
			}
		}		

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

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