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

			$group_no = mysqli_fetch_assoc(mysqli_query($conn, "SELECT vision FROM group_id_tbl"));
			$g_no = $group_no['vision'];

			$sqlg = mysqli_query($conn, "SELECT * FROM vision WHERE lang='1'");
			
			if($log_lang == 1) {
				if(mysqli_num_rows($sqlg) == 0) {
					$sql = "INSERT INTO vision VALUES (NULL, '$vision', '$mission', '$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 vision='$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_vision_lang', $_POST)) {
			$mission = htmlspecialchars(strip_tags(trim($_POST['mission'])), ENT_QUOTES, 'UTF-8');
			$vision = htmlspecialchars(strip_tags(trim($_POST['vision'])), 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 vision WHERE lang='$lang'");
			
			if(mysqli_num_rows($sqlg) == 0) {
				$sql = "INSERT INTO vision VALUES (NULL, '$vision', '$mission', '$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" => 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 vision, mission FROM vision 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_vision', $_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');
			
			$vision = htmlspecialchars(strip_tags(trim($_POST['vision'])), ENT_QUOTES, 'UTF-8');
			$mission = htmlspecialchars(strip_tags(trim($_POST['mission'])), ENT_QUOTES, 'UTF-8');

			$sql_up = mysqli_query($conn, "UPDATE vision SET vision='$vision', mission='$mission', 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{
		$redirectUrl = "index.php";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";	
	}
}
?>