<?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 *************************************/
		date_default_timezone_set('Asia/Colombo');
		$reg_date = date("Y-m-d");
		
		if (array_key_exists('add_contacts', $_POST)) {
			$type = htmlspecialchars(strip_tags(trim($_POST['type'])), ENT_QUOTES, 'UTF-8');
			$name = htmlspecialchars(strip_tags(trim($_POST['name'])), ENT_QUOTES, 'UTF-8');
			$designation = htmlspecialchars(strip_tags(trim($_POST['designation'])), ENT_QUOTES, 'UTF-8');
			$tel_office = htmlspecialchars(strip_tags(trim($_POST['tel_office'])), ENT_QUOTES, 'UTF-8');
			$fax = htmlspecialchars(strip_tags(trim($_POST['fax'])), ENT_QUOTES, 'UTF-8');
			$email = htmlspecialchars(strip_tags(trim($_POST['email'])), ENT_QUOTES, 'UTF-8');
			$division = htmlspecialchars(strip_tags(trim($_POST['division'])), ENT_QUOTES, 'UTF-8');
			$staff = htmlspecialchars(strip_tags(trim($_POST['staff'])), ENT_QUOTES, 'UTF-8');

			if($log_lang == 1) {	
				$sql = "INSERT INTO contact_details SET name='$name', designation='$designation', tel_office='$tel_office', fax='$fax', email='$email', reg_date='$reg_date', type='$type', user='$user_id', div_office='$division', no_of_staff='$staff'";

				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"));
				}
			}
		}

		/****************************** Remove *************************************/
		
		if (array_key_exists('remove_reg', $_POST)) {

			if($log_lang == 1) {
			
				$reg_id = htmlspecialchars(strip_tags(trim($_POST['reg_id'])), ENT_QUOTES, 'UTF-8');		
				$sql = "UPDATE contact_details SET del=1 WHERE id='$reg_id'";
				
				if(!empty($reg_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)) {

			$reg_id = htmlspecialchars(strip_tags(trim($_POST['reg_id'])), ENT_QUOTES, 'UTF-8');
			
			$data = array();
			$sql = mysqli_query($conn, "SELECT * FROM contact_details WHERE id=$reg_id AND del=0");

			while ($row = mysqli_fetch_assoc($sql)) {
				$data[] = $row;
			}
			echo json_encode($data);
		}
		
		/****************************** Update  *************************************/
		
		if (array_key_exists('update_reg', $_POST)) {
			$reg_id = htmlspecialchars(strip_tags(trim($_POST['reg_id'])), ENT_QUOTES, 'UTF-8');
			
			$name = htmlspecialchars(strip_tags(trim($_POST['name'])), ENT_QUOTES, 'UTF-8');
			$des = htmlspecialchars(strip_tags(trim($_POST['des'])), ENT_QUOTES, 'UTF-8');
			$tel_office = htmlspecialchars(strip_tags(trim($_POST['tel_office'])), ENT_QUOTES, 'UTF-8');
			$fax = htmlspecialchars(strip_tags(trim($_POST['fax'])), ENT_QUOTES, 'UTF-8');
			$email = htmlspecialchars(strip_tags(trim($_POST['email'])), ENT_QUOTES, 'UTF-8');
			$type = htmlspecialchars(strip_tags(trim($_POST['type'])), ENT_QUOTES, 'UTF-8');
			$division = htmlspecialchars(strip_tags(trim($_POST['division'])), ENT_QUOTES, 'UTF-8');
			$staff_up = htmlspecialchars(strip_tags(trim($_POST['staff_up'])), ENT_QUOTES, 'UTF-8');

			if($log_lang == 1) {
				if($type == 2 && empty($division)) {
					echo json_encode(array("msgType" => 3, "msg" => "empty div"));
				
				} else {			
					$sql_up = mysqli_query($conn, "UPDATE contact_details SET name='$name', designation='$des', tel_office='$tel_office', fax='$fax', email='$email', type='$type', user='$user_id', div_office='$division', no_of_staff='$staff_up' WHERE id='$reg_id'");

					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>";	
	}
}
?>