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

			$year = htmlspecialchars(trim($_POST['year']), ENT_QUOTES, 'UTF-8');
			$pro_type = htmlspecialchars(trim($_POST['pro_type']), ENT_QUOTES, 'UTF-8');
			$project = htmlspecialchars(trim($_POST['project']), ENT_QUOTES, 'UTF-8');
			$pro_method = htmlspecialchars(trim($_POST['pro_method']), ENT_QUOTES, 'UTF-8');
			$pro_st = htmlspecialchars(trim($_POST['pro_st']), ENT_QUOTES, 'UTF-8');
			$bid_prep = htmlspecialchars(trim($_POST['bid_prep']), ENT_QUOTES, 'UTF-8');
			$bid_app = htmlspecialchars(trim($_POST['bid_app']), ENT_QUOTES, 'UTF-8');
			$bid_invite = htmlspecialchars(trim($_POST['bid_invite']), ENT_QUOTES, 'UTF-8');
			$bid_open = htmlspecialchars(trim($_POST['bid_open']), ENT_QUOTES, 'UTF-8');
			$TEC_eval = htmlspecialchars(trim($_POST['TEC_eval']), ENT_QUOTES, 'UTF-8');
			$pc_app = htmlspecialchars(trim($_POST['pc_app']), ENT_QUOTES, 'UTF-8');
			$issue_letter = htmlspecialchars(trim($_POST['issue_letter']), ENT_QUOTES, 'UTF-8');
			$con_sign = htmlspecialchars(trim($_POST['con_sign']), ENT_QUOTES, 'UTF-8');
			$com_period = htmlspecialchars(trim($_POST['com_period']), ENT_QUOTES, 'UTF-8');
			$con_details = htmlspecialchars(trim($_POST['con_details']), ENT_QUOTES, 'UTF-8');
			$remarks = htmlspecialchars(trim($_POST['remarks']), ENT_QUOTES, 'UTF-8');

			$sqlp = mysqli_query($conn, "SELECT * FROM procurement_plan WHERE project='$project'");
	
			if(mysqli_num_rows($sqlp) == 0) {
				$sql = "INSERT INTO procurement_plan SET year='$year', pro_type='$pro_type', project='$project', pro_method='$pro_method', pro_st='$pro_st', bid_prep='$bid_prep', bid_app='$bid_app', bid_invite='$bid_invite', bid_open='$bid_open', TEC_eval='$TEC_eval', pc_app='$pc_app', issue_letter='$issue_letter', con_sign='$con_sign', com_period='$com_period', con_details='$con_details', remarks='$remarks', user='$user_id'";
				
				$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'])){
			$year = htmlspecialchars(trim($_POST['year']), ENT_QUOTES, 'UTF-8');
			$pro_type = htmlspecialchars(trim($_POST['pro_type']), ENT_QUOTES, 'UTF-8');
			$pro_method = htmlspecialchars(trim($_POST['pro_method']), ENT_QUOTES, 'UTF-8');
			$pro_st = htmlspecialchars(trim($_POST['pro_st']), ENT_QUOTES, 'UTF-8');
			$bid_prep = htmlspecialchars(trim($_POST['bid_prep']), ENT_QUOTES, 'UTF-8');
			$bid_app = htmlspecialchars(trim($_POST['bid_app']), ENT_QUOTES, 'UTF-8');
			$bid_invite = htmlspecialchars(trim($_POST['bid_invite']), ENT_QUOTES, 'UTF-8');
			$bid_open = htmlspecialchars(trim($_POST['bid_open']), ENT_QUOTES, 'UTF-8');
			$TEC_eval = htmlspecialchars(trim($_POST['TEC_eval']), ENT_QUOTES, 'UTF-8');
			$pc_app = htmlspecialchars(trim($_POST['pc_app']), ENT_QUOTES, 'UTF-8');
			$issue_letter = htmlspecialchars(trim($_POST['issue_letter']), ENT_QUOTES, 'UTF-8');
			$con_sign = htmlspecialchars(trim($_POST['con_sign']), ENT_QUOTES, 'UTF-8');
			$com_period = htmlspecialchars(trim($_POST['com_period']), ENT_QUOTES, 'UTF-8');
			$con_details = htmlspecialchars(trim($_POST['con_details']), ENT_QUOTES, 'UTF-8');
			$remarks = htmlspecialchars(trim($_POST['remarks']), ENT_QUOTES, 'UTF-8');

			$id = htmlspecialchars(trim($_POST['id_holder']), ENT_QUOTES, 'UTF-8');
			
			$sql = "UPDATE procurement_plan SET year='$year', pro_type='$pro_type', pro_method='$pro_method', pro_st='$pro_st', bid_prep='$bid_prep', bid_app='$bid_app', bid_invite='$bid_invite', bid_open='$bid_open', TEC_eval='$TEC_eval', pc_app='$pc_app', issue_letter='$issue_letter', con_sign='$con_sign', com_period='$com_period', con_details='$con_details', remarks='$remarks', user='$user_id' 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 procurement_plan 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 procurement_plan 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>";	
	}

}
?>