<?php
header('Content-type: application/json');
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'];
		
		date_default_timezone_set('Asia/Colombo');
		$date_today = date("Y-m-d");

		$response = array();

		if(isset($_POST['desig'])){
			$desig = htmlspecialchars(trim($_POST['desig']), ENT_QUOTES, 'UTF-8');
			$sqlp = mysqli_query($conn, "SELECT * FROM designation WHERE name='$desig'");
			
			if(mysqli_num_rows($sqlp) == 0) {			
				$sql = "INSERT INTO designation SET name='$desig'";
				$result = mysqli_query($conn, $sql);
	
				if ($result) {
					$response['status'] = 'ok';
				
				}else{
					$response['status'] = 'error';
				}
			
			} else {
				$response['status'] = 'error2';
			}

			echo json_encode($response);

		}

		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 designation WHERE id=$reg_id");

			while ($row = mysqli_fetch_assoc($sql)) {
				$data[] = $row;
			}
			echo json_encode($data);
		}
		
		if(isset($_POST['id_holder'])){

			$id = htmlspecialchars(trim($_POST['id_holder']), ENT_QUOTES, 'UTF-8');
			$desig_up = htmlspecialchars(trim($_POST['desig_up']), ENT_QUOTES, 'UTF-8');

			$sqlpr = mysqli_query($conn, "SELECT * FROM designation WHERE name='$desig_up' AND id != $id");
			
			if(mysqli_num_rows($sqlpr) == 0) {
				$sql = "UPDATE designation SET name='$desig_up' WHERE id = $id";
				$result = mysqli_query($conn, $sql);
	
				if ($result) {
					$response['status'] = 'ok';
				
				} else{
					$response['status'] = 'error';
				}
			
			} else {
				$response['status'] = 'error2';
			}

			echo json_encode($response);

		}		

		if (array_key_exists('remove_reg', $_POST)) {

			$id = htmlspecialchars(trim($_POST['reg_id']), ENT_QUOTES, 'UTF-8');
			$sql_d = mysqli_fetch_assoc(mysqli_query($conn, "SELECT name FROM designation WHERE id=$id"));
			$desig = $sql_d['name'];
			
			$sql3 = mysqli_query($conn, "SELECT * FROM cader_details_tbl WHERE designation='$desig'");

			if (mysqli_num_rows($sql3)>0) {
				$response['status'] = 'error2';
			
			} else {
				$sql2 = mysqli_query($conn, "DELETE FROM designation WHERE id=$id");
				$response['status'] = 'ok';
			}
			echo json_encode($response);
		}

	} else{
		$redirectUrl = "index.php";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";	
	}

}
?>