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

			$year = htmlspecialchars(trim($_POST['year']), ENT_QUOTES, 'UTF-8');
			$type = htmlspecialchars(trim($_POST['type']), ENT_QUOTES, 'UTF-8');
			$pro_code = htmlspecialchars(trim($_POST['pro_code']), ENT_QUOTES, 'UTF-8');
			$pro_name = htmlspecialchars(trim($_POST['pro_name']), ENT_QUOTES, 'UTF-8');
			$division = htmlspecialchars(trim($_POST['division']), ENT_QUOTES, 'UTF-8');
			$pro_status = htmlspecialchars(trim($_POST['pro_status']), ENT_QUOTES, 'UTF-8');
			$pro_e_amount = htmlspecialchars(trim($_POST['pro_e_amount']), ENT_QUOTES, 'UTF-8'); // Total Estimated Amount
			$pro_a_amount = htmlspecialchars(trim($_POST['pro_a_amount']), ENT_QUOTES, 'UTF-8'); // Allocation Total
			$start_date = htmlspecialchars(trim($_POST['start_date']), ENT_QUOTES, 'UTF-8');
			//$comp_date = htmlspecialchars(trim($_POST['comp_date']), ENT_QUOTES, 'UTF-8');

			$sqlpr = mysqli_query($conn, "SELECT * FROM projects_reg WHERE pro_code='$pro_code'");
			
			if(empty($pro_code) || empty($pro_name) || empty($pro_e_amount) || empty($pro_a_amount)) {
				$response['status'] = 'error';
			
			} else {
				if($pro_e_amount > $pro_a_amount) {
					if(mysqli_num_rows($sqlpr) == 0) {
						$sql = "INSERT INTO projects_reg SET year='$year', type='$type', pro_code='$pro_code', pro_name='$pro_name', division='$division', state='$pro_status', esti_amount='$pro_e_amount',total_cost_esti='$pro_a_amount', start_date='$start_date'"; //, copm_date='$comp_date'
						$result = mysqli_query($conn, $sql);
			
						if ($result) {
							$response['status'] = 'ok';
						
						} else{
							$response['status'] = 'error';
						}
					
					} else {
						$response['status'] = 'error2';
					}
				
				} else {
					$response['status'] = 'error3';
				}
			}

			echo json_encode($response);

		}

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

			$type_pro = htmlspecialchars(trim($_POST['type_pro']), ENT_QUOTES, 'UTF-8');
			$sqlp = mysqli_query($conn, "SELECT * FROM project_type WHERE type='$type_pro'");
			
			if(mysqli_num_rows($sqlp) == 0) {			
				$sql = "INSERT INTO project_type SET type='$type_pro'";
				$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['division_name'])){

			$div = htmlspecialchars(trim($_POST['division_name']), ENT_QUOTES, 'UTF-8');
			$address = htmlspecialchars(trim($_POST['address']), ENT_QUOTES, 'UTF-8');
			$sqlp = mysqli_query($conn, "SELECT * FROM division WHERE division='$div'");
			
			if(mysqli_num_rows($sqlp) == 0) {
				$sql = "INSERT INTO division SET division='$div', address='$address'";
				$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['pro_code_up'])){

			$id = htmlspecialchars(trim($_POST['id_holder']), ENT_QUOTES, 'UTF-8');
			$year = htmlspecialchars(trim($_POST['year_up']), ENT_QUOTES, 'UTF-8');
			$type = htmlspecialchars(trim($_POST['type_up']), ENT_QUOTES, 'UTF-8');
			$pro_code = htmlspecialchars(trim($_POST['pro_code_up']), ENT_QUOTES, 'UTF-8');
			$pro_name = htmlspecialchars(trim($_POST['pro_name_up']), ENT_QUOTES, 'UTF-8');
			$division = htmlspecialchars(trim($_POST['division_up']), ENT_QUOTES, 'UTF-8');
			$pro_status = htmlspecialchars(trim($_POST['pro_status_up']), ENT_QUOTES, 'UTF-8');
			$pro_e_amount = htmlspecialchars(trim($_POST['pro_e_amount_up']), ENT_QUOTES, 'UTF-8');
			$pro_a_amount = htmlspecialchars(trim($_POST['pro_a_amount_up']), ENT_QUOTES, 'UTF-8');
			$start_date = htmlspecialchars(trim($_POST['start_date_up']), ENT_QUOTES, 'UTF-8');
			//$comp_date = htmlspecialchars(trim($_POST['comp_date_up']), ENT_QUOTES, 'UTF-8');

			$sqlpr = mysqli_query($conn, "SELECT * FROM projects_reg WHERE pro_code='$pro_code' AND id != $id");
			
			if(empty($pro_code) || empty($pro_name) || empty($pro_e_amount) || empty($pro_a_amount)) {
				$response['status'] = 'error';
			
			} else {
				if($pro_e_amount > $pro_a_amount) {
					if(mysqli_num_rows($sqlpr) == 0) {
						$sql = "UPDATE projects_reg SET year='$year', type='$type', pro_code='$pro_code', pro_name='$pro_name', division='$division', state='$pro_status', esti_amount='$pro_e_amount', total_cost_esti='$pro_a_amount', start_date='$start_date' WHERE id = $id"; // , copm_date='$comp_date'
						$result = mysqli_query($conn, $sql);
			
						if ($result) {
							$response['status'] = 'ok';
						
						} else{
							$response['status'] = 'error';
						}
					
					} else {
						$response['status'] = 'error2';
					}
				
				} else {
					$response['status'] = 'error3';
				}
			}

			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 projects_reg 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');
			$sql3 = mysqli_query($conn, "SELECT * FROM aworded_contractors WHERE project=$id");

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

			$id = htmlspecialchars(trim($_POST['reg_id']), ENT_QUOTES, 'UTF-8');
			$sql3 = mysqli_query($conn, "SELECT * FROM projects_reg WHERE type=$id");
			
			if(mysqli_num_rows($sql3) == 0) {
				$sql2 = mysqli_query($conn, "DELETE FROM project_type WHERE id=$id");
	
				if ($sql2) {
					$response['status'] = 'ok';
				}else{
					$response['status'] = 'error';
				}
			} else {
				$response['status'] = 'error2';
			}
			echo json_encode($response);
		}

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

			$id = htmlspecialchars(trim($_POST['reg_id']), ENT_QUOTES, 'UTF-8');
			$sql3 = mysqli_query($conn, "SELECT * FROM projects_reg WHERE division=$id");
			
			if(mysqli_num_rows($sql3) == 0) {
				$sql2 = mysqli_query($conn, "DELETE FROM division WHERE id=$id");
	
				if ($sql2) {
					$response['status'] = 'ok';
				}else{
					$response['status'] = 'error';
				}
				
			} else {
				$response['status'] = 'error2';
			}
			echo json_encode($response);
		}
		
		if (array_key_exists('get_dataset_div', $_POST)) {

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

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

			$id = htmlspecialchars(trim($_POST['id_holder_div']), ENT_QUOTES, 'UTF-8');
			$div_up = htmlspecialchars(trim($_POST['div_up']), ENT_QUOTES, 'UTF-8');
			$address_up = htmlspecialchars(trim($_POST['address_up']), ENT_QUOTES, 'UTF-8');

			$sqlpr = mysqli_query($conn, "SELECT * FROM division WHERE division='$div_up' AND id != $id");
			
			if(mysqli_num_rows($sqlpr) == 0) {
				$sql = "UPDATE division SET division='$div_up', address='$address_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);

		}		

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

}
?>