<?php 

if($_COOKIE['sejahtera_role'] == 10)
{
	include_once ("includes/setup_pemohon.inc.php");
}
else
{
	include_once ("includes/setup.inc.php");
}

$checkInfo = fetch_postget_val('checkInfo',$_POST, $_GET);
$borangitem = fetch_postget_val('borangitem',$_POST, $_GET);
$table = fetch_postget_val('table',$_POST, $_GET);
$column = fetch_postget_val('column',$_POST, $_GET);
$nextDiv = fetch_postget_val('nextDiv',$_POST, $_GET);
$type = fetch_postget_val('type',$_POST, $_GET);

$outputResult = [];

if($type == 'pull' && $checkInfo > 0)
{
	$indicator_Query = "SELECT pullinfo_sql
					  FROM u_borangdiv
					  WHERE id_borangdiv = ?";
	$PDO_stmt = $PDO->prepare($indicator_Query);
	$PDO_stmt->bindValue(1, $nextDiv, PDO::PARAM_STR);
	$PDO_stmt->execute();
	$pullinfo_result = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
	if($pullinfo_result['pullinfo_sql'] != '')
	{
		$PDO_stmt = $PDO->prepare($pullinfo_result['pullinfo_sql']);
		$PDO_stmt->bindValue(1, $checkInfo, PDO::PARAM_STR);
		$PDO_stmt->execute();
		$checkInfoResult = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
	}
	
	//if($checkInfoResult != '') //got result
	{
		$indicator_Query = "SELECT input_name, value, id_inputtype
						  FROM u_borangitem
						  WHERE id_inputtype IN (1,2,3,4,8,11) AND id_borangdiv = ?";
		$PDO_stmt = $PDO->prepare($indicator_Query);
		$PDO_stmt->bindValue(1, $nextDiv, PDO::PARAM_INT);
		$PDO_stmt->execute();
		while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
		{
			$type = 1; //default 1
			if($result['id_inputtype'] == 2 || $result['id_inputtype'] == 3)
			{
				$type = 2; //select type, need trigger
			}
			else if ($result['id_inputtype'] == 11)
			{
				$type = 3; //datatable type, need fetch
			}
			$outputResult[] = array($result['input_name'], $checkInfoResult[$result['value']], $type);
		}
	}

}
else
{
	//get extra param from borangitem
	$indicator_Query = "SELECT *
					  FROM u_borangitem
					  WHERE id_borangitem = ?";
	$PDO_stmt = $PDO->prepare($indicator_Query);
	$PDO_stmt->bindValue(1, $borangitem, PDO::PARAM_INT);
	$PDO_stmt->execute();
	$borangItemResult = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

	if($borangItemResult['extra_param'] != '')
	{
		$info = explode('<TR>', $borangItemResult['extra_param']);
		if($type == 'blok')
		{
			$indicator_Query = "SELECT *
							  FROM {$info[0]}
							  WHERE {$info[1]} = ?";
			$PDO_stmt = $PDO->prepare($indicator_Query);
			$PDO_stmt->bindValue(1, $checkInfo, PDO::PARAM_STR);
			$PDO_stmt->execute();
			$checkInfoResult = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

			if($checkInfoResult[$info[1]] != '') //got result, set block
			{
				if($checkInfoResult['id_statusmohon'] == 0 || $checkInfoResult['id_statusmohon'] == 1) //got result, but dont want to block
				{
					$outputResult['block'] = 0;
					$outputResult['idToSet'] = $info[3];
					$outputResult['value'] = $checkInfoResult[$info[2]];
				}
				else
				{
					$outputResult['block'] = 1;
					if($checkInfoResult['id_program'] > 0)
					{
						$indicator_Query = "SELECT *
										  FROM i_program
										  WHERE id_program = ?";
						$PDO_stmt = $PDO->prepare($indicator_Query);
						$PDO_stmt->bindValue(1, $checkInfoResult['id_program'], PDO::PARAM_INT);
						$PDO_stmt->execute();
						$programInfoResult = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
					}
					$outputResult['message'] = 'Pemohon telah wujud dalam program '.$programInfoResult['desk_program'].' dan sedang dalam proses pengesahan/telah disahkan';
				}
			}
		}
		else if($type == 'check')
		{
			$indicator_Query = "SELECT *
							  FROM {$info[0]}
							  WHERE {$info[1]} = ?";
			$PDO_stmt = $PDO->prepare($indicator_Query);
			$PDO_stmt->bindValue(1, $checkInfo, PDO::PARAM_STR);
			$PDO_stmt->execute();
			$checkInfoResult = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

			if($checkInfoResult[$info[2]] != '') //got result, but dont want to block
			{
				$outputResult['block'] = 0;
				$outputResult['idToSet'] = $info[3];
				$outputResult['value'] = $checkInfoResult[$info[2]];
			}
		}
	}
}


echo json_encode($outputResult);
?>