<?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');

$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'];

		/************************************ Load Main Cat **************************************** */
		if (array_key_exists('load_cat', $_POST)) {
			$sql = "SELECT id, cat FROM menu_category WHERE lang='1' AND del=0 AND url=''";

			$result = mysqli_query($conn, $sql);
			echo'<option value="">Select Menu Category...</option>';
			while ($row = mysqli_fetch_assoc($result)) {
				$data[] = $row;
			}

			if (!empty($data)) {
			foreach ($data AS $data) {
				echo'<option value="' . $data['id'] . '">' . $data['cat'] . '</option>';
			}
			} else {
			echo'<option value="">No Data Found</option>';
			}
			echo json_encode($data);
		}
		
		/****************************** Insert Eng *************************************/
		
		if (array_key_exists('add_subcat_eng', $_POST)) {
			$mian_cat_id = htmlspecialchars(strip_tags(trim($_POST['mian_cat_id'])), ENT_QUOTES, 'UTF-8');
			$cat_name = htmlspecialchars(strip_tags(trim($_POST['cat_name'])), ENT_QUOTES, 'UTF-8');

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

			if($log_lang == 1) {
				if(mysqli_num_rows($sql_eng) == 0) {
					$sql = "INSERT INTO menu_subcat VALUES (NULL, '$mian_cat_id', '$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_subcat='$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_subcat_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_subcat WHERE group_id='$group_id' AND lang='$lang'");
			
			if(mysqli_num_rows($sqlg) == 0) {
				$sql_lang = mysqli_query($conn, "SELECT sub_cat FROM menu_subcat WHERE sub_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 cat_id, url FROM menu_subcat WHERE group_id='$group_id' AND lang=1"));
					$mian_cat_id = $sql_eng['cat_id'];	
					$url = $sql_eng['url'];	
				
					$sql = "INSERT INTO menu_subcat VALUES (NULL, '$mian_cat_id', '$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_subcat', $_POST)) {

			if($log_lang == 1) {
			
				$group_id = htmlspecialchars(strip_tags(trim($_POST['gid'])), ENT_QUOTES, 'UTF-8');
				$sql = "UPDATE menu_subcat 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 cat_id, sub_cat, url, order_id FROM menu_subcat WHERE group_id=$group_id AND lang='$lang'");

			while ($row = mysqli_fetch_assoc($sql)) {
				$data[] = $row;
			}
			echo json_encode($data);
		}
		
		if (array_key_exists('get_cat', $_POST)) {
			$cat_id = htmlspecialchars(strip_tags(trim($_POST['cat_id'])), ENT_QUOTES, 'UTF-8');
			$sql = "SELECT id, cat FROM menu_category WHERE lang='1' AND del=0 AND url=''";

			$result = mysqli_query($conn, $sql);
			while ($row = mysqli_fetch_assoc($result)) {
				$data[] = $row;
			}

			if (!empty($data)) {
				foreach ($data AS $data) { ?><option value="<?php echo $data['id']; ?>" <?php if($data['id'] == $cat_id){ echo "selected"; } ?>><?php echo $data['cat']; ?></option>
				<?php }
			}
			echo json_encode($data);
		}	
		
		/****************************** Update *************************************/
		
		if (array_key_exists('update_subcat', $_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');
			$mian_cat_id = htmlspecialchars(strip_tags(trim($_POST['mian_cat_id'])), ENT_QUOTES, 'UTF-8');
			$oid = htmlspecialchars(strip_tags(trim($_POST['oid'])), ENT_QUOTES, 'UTF-8');
			$page_url = htmlspecialchars(strip_tags(trim($_POST['page_url'])), ENT_QUOTES, 'UTF-8');
			
			$sql_lang = mysqli_query($conn, "SELECT sub_cat FROM menu_subcat WHERE sub_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_subcat SET cat_id='$mian_cat_id', url='$page_url', order_id = '$oid' WHERE group_id='$group_id'");
				}				
				
				$sql_up = mysqli_query($conn, "UPDATE menu_subcat SET sub_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>";	
	}
}
?>