<?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['req_no'])){

			$req_no = htmlspecialchars(trim($_POST['req_no']), ENT_QUOTES, 'UTF-8');
			$dep_date = htmlspecialchars(trim($_POST['dep_date']), ENT_QUOTES, 'UTF-8');
			$dep_head = htmlspecialchars(trim($_POST['dep_head']), ENT_QUOTES, 'UTF-8');
			$particulars = htmlspecialchars(trim($_POST['particular']), ENT_QUOTES, 'UTF-8');
			$dep_amount = htmlspecialchars(trim($_POST['dep_amount']), ENT_QUOTES, 'UTF-8');
			$pay_date = htmlspecialchars(trim($_POST['pay_date']), ENT_QUOTES, 'UTF-8');
			$pay_amount = htmlspecialchars(trim($_POST['pay_amount']), ENT_QUOTES, 'UTF-8');
			$to_paid = htmlspecialchars(trim($_POST['to_paid']), ENT_QUOTES, 'UTF-8');
			
			$sqlp = mysqli_query($conn, "SELECT * FROM requisition WHERE req_no='$req_no'");
	
			if(mysqli_num_rows($sqlp) == 0) {
				$sql = "INSERT INTO requisition SET req_no='$req_no', dep_date='$dep_date', dep_head='$dep_head', particulars='$particulars', dep_amount='$dep_amount', pay_date='$pay_date', pay_amount='$pay_amount', to_paid='$to_paid', date='$date_today'";
				
				$result = mysqli_query($conn, $sql);
	
				if($result) {
					$response['status'] = 'ok';
				
				} else {
					$response['status'] = 'error';
				}
				
			} else {
				$response['status'] = 'error2';
			}

			echo json_encode($response);

		}
		
		if(isset($_POST['id_holder'])){
			$dep_date = htmlspecialchars(trim($_POST['dep_date_up']), ENT_QUOTES, 'UTF-8');
			$dep_head = htmlspecialchars(trim($_POST['dep_head_up']), ENT_QUOTES, 'UTF-8');
			$particulars = htmlspecialchars(trim($_POST['particular_up']), ENT_QUOTES, 'UTF-8');
			$dep_amount = htmlspecialchars(trim($_POST['dep_amount_up']), ENT_QUOTES, 'UTF-8');
			$pay_date = htmlspecialchars(trim($_POST['pay_date_up']), ENT_QUOTES, 'UTF-8');
			$pay_amount = htmlspecialchars(trim($_POST['pay_amount_up']), ENT_QUOTES, 'UTF-8');
			$to_paid = htmlspecialchars(trim($_POST['to_paid_up']), ENT_QUOTES, 'UTF-8');

			$id = htmlspecialchars(trim($_POST['id_holder']), ENT_QUOTES, 'UTF-8');
			
			$sql = "UPDATE requisition SET dep_date='$dep_date', dep_head='$dep_head', particulars='$particulars', dep_amount='$dep_amount', pay_date='$pay_date', pay_amount='$pay_amount', to_paid='$to_paid' WHERE id = $id";
          	
			$result = mysqli_query($conn, $sql);

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

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

			while ($row = mysqli_fetch_assoc($sql)) {
				$data[] = $row;
			}
			echo json_encode($data);
		}

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

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

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

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

}
?>