<?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_div_eng', $_POST)) {
			$div_name = htmlspecialchars(strip_tags(trim($_POST['div_name'])), ENT_QUOTES, 'UTF-8');

			$group_no = mysqli_fetch_assoc(mysqli_query($conn, "SELECT div_offices FROM group_id_tbl"));
			$g_no = $group_no['div_offices'];
			
			$sql_eng = mysqli_query($conn, "SELECT division FROM divisional_offices WHERE division='$div_name' AND del=0 AND lang=1");

			if($log_lang == 1) {
				if(mysqli_num_rows($sql_eng) == 0) {
					$sql = "INSERT INTO divisional_offices VALUES (NULL, '$div_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 div_offices='$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_div_lang', $_POST)) {
			$div_name = htmlspecialchars(strip_tags(trim($_POST['div_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 divisional_offices WHERE group_id='$group_id' AND lang='$lang'");
			
			if(mysqli_num_rows($sqlg) == 0) {
				$sql_lang = mysqli_query($conn, "SELECT division FROM divisional_offices WHERE division='$div_name' AND del=0 AND lang='$lang'");
				
				if(mysqli_num_rows($sql_lang) == 0) {
					$sql = "INSERT INTO divisional_offices VALUES (NULL, '$div_name', '$user_id', '$group_id', '$lang' , '0')";
				
					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_div', $_POST)) {

			if($log_lang == 1) {
			
				$group_id = htmlspecialchars(strip_tags(trim($_POST['gid'])), ENT_QUOTES, 'UTF-8');
				$sqld = mysqli_query($conn, "SELECT * FROM contact_details WHERE div_office='$group_id'");
				
				if(mysqli_num_rows($sqld) == 0) {
					$sql = "UPDATE divisional_offices 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 Already 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 division FROM divisional_offices WHERE group_id=$group_id AND lang='$lang'");

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

				$sql_up = mysqli_query($conn, "UPDATE divisional_offices SET division='$division', 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>";	
	}
}
?>