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

			$project = htmlspecialchars(trim($_POST['project']), ENT_QUOTES, 'UTF-8');
			
			$supplier = htmlspecialchars(trim($_POST['supplier']), ENT_QUOTES, 'UTF-8');
			$con_name = htmlspecialchars(trim($_POST['con_name']), ENT_QUOTES, 'UTF-8');
			$con_type = htmlspecialchars(trim($_POST['con_type']), ENT_QUOTES, 'UTF-8');
			
			$c_amount = htmlspecialchars(trim($_POST['c_amount']), ENT_QUOTES, 'UTF-8');
			$allo_amount = htmlspecialchars(trim($_POST['allo_amount']), ENT_QUOTES, 'UTF-8');
			$c_num = htmlspecialchars(trim($_POST['c_num']), ENT_QUOTES, 'UTF-8');
			$date_aworded = htmlspecialchars(trim($_POST['date_aworded']), ENT_QUOTES, 'UTF-8');
			$date_comp = htmlspecialchars(trim($_POST['date_comp']), ENT_QUOTES, 'UTF-8');
			$date_ext = htmlspecialchars(trim($_POST['date_ext']), ENT_QUOTES, 'UTF-8');
			$date_last = htmlspecialchars(trim($_POST['date_last']), ENT_QUOTES, 'UTF-8');
			$start_date = htmlspecialchars(trim($_POST['start_date']), ENT_QUOTES, 'UTF-8');
			
			$bond_exp = htmlspecialchars(trim($_POST['bond_exp']), ENT_QUOTES, 'UTF-8');
			$sec_exp = htmlspecialchars(trim($_POST['sec_exp']), ENT_QUOTES, 'UTF-8');

			$sqlp = mysqli_query($conn, "SELECT * FROM aworded_contractors WHERE project='$project'"); //supplier='$supplier' AND 
			$sql_aw = mysqli_query($conn, "SELECT * FROM aworded_contractors WHERE c_num='$c_num'");
			
			if($c_amount >= $allo_amount) {
				$response['status'] = 'error6';
			
			} else {
				if(mysqli_num_rows($sqlp) == 0) {
					if(mysqli_num_rows($sql_aw) == 0) {
						if($c_amount > 0) {
							
							if($con_type == 1) {
								$con_t = 'reg';
								if(empty($supplier) || $supplier == 0) {
									$response['status'] = 'error5';
								
								} else {
									$sql = "INSERT INTO aworded_contractors SET project='$project', supplier='$supplier', c_amount='$c_amount', date_aworded='$date_aworded', date_comp='$date_comp', ext_date='$date_ext', com_date='$date_last', c_num='$c_num', pub_supplier='', contract_type='$con_t', bond_exp='$bond_exp', security_exp='$sec_exp', started_date='$start_date'";
									
									$result = mysqli_query($conn, $sql);
						
									if($result) {
										$response['status'] = 'ok';
									
									} else {
										$response['status'] = 'error';
									}
								}
							
							} else if($con_type == 2) {
								$con_t = 'pub';
								
								if(empty($con_name)) {
									$response['status'] = 'error5';
								
								} else {
									$sql = "INSERT INTO aworded_contractors SET project='$project', supplier='0', c_amount='$c_amount', date_aworded='$date_aworded', date_comp='$date_comp', ext_date='$date_ext', com_date='$date_last', c_num='$c_num', pub_supplier='$con_name', contract_type='$con_t', bond_exp='$bond_exp', security_exp='$sec_exp', started_date='$start_date'";
									
									$result = mysqli_query($conn, $sql);
						
									if($result) {
										$response['status'] = 'ok';
									
									} else {
										$response['status'] = 'error';
									}								
								
								}
							}
						
						} else {
							$response['status'] = 'error4';
						}
					
					} else {
						$response['status'] = 'error3';
					}
				
				} else {
					$response['status'] = 'error2';
				}
			}

			echo json_encode($response);

		}
		
		if(isset($_POST['id_holder'])){
			$project = htmlspecialchars(trim($_POST['project_up']), ENT_QUOTES, 'UTF-8');
			$supplier = htmlspecialchars(trim($_POST['supplier_up']), ENT_QUOTES, 'UTF-8');
			$c_amount = htmlspecialchars(trim($_POST['c_amount_up']), ENT_QUOTES, 'UTF-8');
			$c_num = htmlspecialchars(trim($_POST['c_num_up']), ENT_QUOTES, 'UTF-8');
			$pro_sum = htmlspecialchars(trim($_POST['pro_sum_up']), ENT_QUOTES, 'UTF-8');
			$date_aworded = htmlspecialchars(trim($_POST['date_aworded_up']), ENT_QUOTES, 'UTF-8');
			$date_comp = htmlspecialchars(trim($_POST['date_comp_up']), ENT_QUOTES, 'UTF-8');
			$date_ext = htmlspecialchars(trim($_POST['date_ext_up']), ENT_QUOTES, 'UTF-8');
			$date_last = htmlspecialchars(trim($_POST['date_last_up']), ENT_QUOTES, 'UTF-8');

			$id = htmlspecialchars(trim($_POST['id_holder']), ENT_QUOTES, 'UTF-8');
			
			//$sql = "UPDATE aworded_contractors SET project='$project', supplier='$supplier', c_amount='$c_amount', date_aworded='$date_aworded', date_comp='$date_comp', ext_date='$date_ext', com_date='$date_last', c_num='$c_num', pro_sum='$pro_sum' WHERE id = $id";
          	$result = mysqli_query($conn, $sql);

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

			echo json_encode($response);
		}

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

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

			$sql = "INSERT INTO project_type SET type='$type_pro'";
          	$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 aworded_contractors 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');
			$sqla  = mysqli_fetch_assoc(mysqli_query($conn, "SELECT project, supplier FROM aworded_contractors WHERE id=$id"));
			$sup = $sqla['supplier'];
			$proj = $sqla['project'];
			
			$sql_inv = mysqli_query($conn, "SELECT * FROM project_invoice WHERE project='$proj' AND supplier='$sup'");
			
			if(mysqli_num_rows($sql_inv) == 0) {
				$sql2 = mysqli_query($conn, "DELETE FROM aworded_contractors 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_project_data', $_POST)) {
			$pid = htmlspecialchars(strip_tags(trim($_POST['pid'])), ENT_QUOTES, 'UTF-8');
			
			//$data = array();
			$sql = mysqli_query($conn, "SELECT * FROM projects_reg WHERE id=$pid");

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

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

}
?>