<?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 *************************************/
		
		if (array_key_exists('add_prof_name', $_POST)) {
			$prof_name = htmlspecialchars(strip_tags(trim($_POST['prof_name'])), ENT_QUOTES, 'UTF-8');
			$sql_prof = mysqli_query($conn, "SELECT type FROM group_profile WHERE type='$prof_name' AND del=0");

			if($log_lang == 1) {
				if(mysqli_num_rows($sql_prof) == 0) {
					$sql = "INSERT INTO group_profile SET type='$prof_name', user='$user_id'";

					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_id', $_POST)) {

			if($log_lang == 1) {
			
				$gid = 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 group_profile SET del=1 WHERE id='$gid'";
					
					if(!empty($gid)) {
					
						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)) {

			$gid = htmlspecialchars(strip_tags(trim($_POST['gid'])), ENT_QUOTES, 'UTF-8');
			
			$data = array();
			$sql = mysqli_query($conn, "SELECT type FROM group_profile WHERE id='$gid'");

			while ($row = mysqli_fetch_assoc($sql)) {
				$data[] = $row;
			}
			echo json_encode($data);
		}
		
		/****************************** Update *************************************/
		
		if (array_key_exists('update_id', $_POST)) {
			$prof_name = htmlspecialchars(strip_tags(trim($_POST['prof_name'])), ENT_QUOTES, 'UTF-8');
			$gid = htmlspecialchars(strip_tags(trim($_POST['gid'])), ENT_QUOTES, 'UTF-8');
			
			$sql_lang = mysqli_query($conn, "SELECT type FROM group_profile WHERE type='$prof_name' AND id!='$gid'");	
			
			if(mysqli_num_rows($sql_lang) == 0) {

				if($log_lang == 1) {
					
					$sql_up = mysqli_query($conn, "UPDATE group_profile SET type='$prof_name', user='$user_id' WHERE id='$gid'");
					
					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>";	
	}
}
?>