<?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'];



	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 cader_details_tbl 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');
		$designation = htmlspecialchars(trim($_POST['designation_up']), ENT_QUOTES, 'UTF-8');
		$code_no = htmlspecialchars(trim($_POST['code_no']), ENT_QUOTES, 'UTF-8');
		$service = htmlspecialchars(trim($_POST['service']), ENT_QUOTES, 'UTF-8');
		$grade_class = htmlspecialchars(trim($_POST['grade_class']), ENT_QUOTES, 'UTF-8');
		$salary_code = htmlspecialchars(trim($_POST['salary_code']), ENT_QUOTES, 'UTF-8');
		$service_level = htmlspecialchars(trim($_POST['service_level']), ENT_QUOTES, 'UTF-8');
		$amended_cadre = htmlspecialchars(trim($_POST['amended_cadre']), ENT_QUOTES, 'UTF-8');
		$no_of_emp = htmlspecialchars(trim($_POST['no_of_emp']), ENT_QUOTES, 'UTF-8');
		$transfer_in = htmlspecialchars(trim($_POST['transfer_in']), ENT_QUOTES, 'UTF-8');
		$transfer_out = htmlspecialchars(trim($_POST['transfer_out']), ENT_QUOTES, 'UTF-8');
		$retirement_resignation = htmlspecialchars(trim($_POST['retirement_resignation']), ENT_QUOTES, 'UTF-8');
		$deths = htmlspecialchars(trim($_POST['deths']), ENT_QUOTES, 'UTF-8');
		$new_appointments = htmlspecialchars(trim($_POST['new_appointments']), ENT_QUOTES, 'UTF-8');
		$permanent = htmlspecialchars(trim($_POST['permanent']), ENT_QUOTES, 'UTF-8');
		$vacancles = htmlspecialchars(trim($_POST['vacancles']), ENT_QUOTES, 'UTF-8');
		$excess = htmlspecialchars(trim($_POST['excess']), ENT_QUOTES, 'UTF-8');
		$casual = htmlspecialchars(trim($_POST['casual']), ENT_QUOTES, 'UTF-8');
		$substitute = htmlspecialchars(trim($_POST['substitute']), ENT_QUOTES, 'UTF-8');
		$contracts = htmlspecialchars(trim($_POST['contracts']), ENT_QUOTES, 'UTF-8');
		$total = htmlspecialchars(trim($_POST['total']), ENT_QUOTES, 'UTF-8');

		$sql = "UPDATE cader_details_tbl SET designation='$designation', code_no='$code_no', service='$service', grade_class='$grade_class', salary_code='$salary_code', service_level='$service_level', amended_cadre='$amended_cadre', no_of_emp='$no_of_emp', transfer_in='$transfer_in', transfer_out='$transfer_out', retirement_resignation='$retirement_resignation', deths='$deths', new_appointments='$new_appointments', permanent='$permanent', vacancles='$vacancles', excess='$excess', casual='$casual', substitute='$substitute', contracts='$contracts', total='$total' WHERE id = $id";
      	$result = mysqli_query($conn, $sql);

      	if ($result) {
			$response['status'] = 'ok';
		}else{
			$response['status'] = 'error';
		}

		echo json_encode($response);

	}

	if(isset($_POST['designation'])){

		$designation = htmlspecialchars(trim($_POST['designation']), ENT_QUOTES, 'UTF-8');
		$code_no = htmlspecialchars(trim($_POST['code_no']), ENT_QUOTES, 'UTF-8');
		$service = htmlspecialchars(trim($_POST['service']), ENT_QUOTES, 'UTF-8');
		$grade_class = htmlspecialchars(trim($_POST['grade_class']), ENT_QUOTES, 'UTF-8');
		$salary_code = htmlspecialchars(trim($_POST['salary_code']), ENT_QUOTES, 'UTF-8');
		$service_level = htmlspecialchars(trim($_POST['service_level']), ENT_QUOTES, 'UTF-8');
		$amended_cadre = htmlspecialchars(trim($_POST['amended_cadre']), ENT_QUOTES, 'UTF-8');
		$no_of_emp = htmlspecialchars(trim($_POST['no_of_emp']), ENT_QUOTES, 'UTF-8');
		$transfer_in = htmlspecialchars(trim($_POST['transfer_in']), ENT_QUOTES, 'UTF-8');
		$transfer_out = htmlspecialchars(trim($_POST['transfer_out']), ENT_QUOTES, 'UTF-8');
		$retirement_resignation = htmlspecialchars(trim($_POST['retirement_resignation']), ENT_QUOTES, 'UTF-8');
		$deths = htmlspecialchars(trim($_POST['deths']), ENT_QUOTES, 'UTF-8');
		$new_appointments = htmlspecialchars(trim($_POST['new_appointments']), ENT_QUOTES, 'UTF-8');
		$permanent = htmlspecialchars(trim($_POST['permanent']), ENT_QUOTES, 'UTF-8');
		$vacancles = htmlspecialchars(trim($_POST['vacancles']), ENT_QUOTES, 'UTF-8');
		$excess = htmlspecialchars(trim($_POST['excess']), ENT_QUOTES, 'UTF-8');
		$casual = htmlspecialchars(trim($_POST['casual']), ENT_QUOTES, 'UTF-8');
		$substitute = htmlspecialchars(trim($_POST['substitute']), ENT_QUOTES, 'UTF-8');
		$contracts = htmlspecialchars(trim($_POST['contracts']), ENT_QUOTES, 'UTF-8');
		$total = htmlspecialchars(trim($_POST['total']), ENT_QUOTES, 'UTF-8');

		$sql = "INSERT INTO cader_details_tbl SET designation='$designation', code_no='$code_no', service='$service', grade_class='$grade_class', salary_code='$salary_code', service_level='$service_level', amended_cadre='$amended_cadre', no_of_emp='$no_of_emp', transfer_in='$transfer_in', transfer_out='$transfer_out', retirement_resignation='$retirement_resignation', deths='$deths', new_appointments='$new_appointments', permanent='$permanent', vacancles='$vacancles', excess='$excess', casual='$casual', substitute='$substitute', contracts='$contracts', total='$total'";
      	$result = mysqli_query($conn, $sql);

      	if ($result) {
			$response['status'] = 'ok';
		}else{
			$response['status'] = 'error';
		}

		echo json_encode($response);

	}

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

		$id = htmlspecialchars(trim($_POST['reg_id']), ENT_QUOTES, 'UTF-8');
		$sql2 = mysqli_query($conn, "DELETE FROM cader_details_tbl WHERE id=$id");

		if ($sql2) {
			$response['status'] = 'ok';
		}else{
			$response['status'] = 'error';
		}
		echo json_encode($response);
		//echo "In";
	}

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

} ?>