<?php
if($_COOKIE['sejahtera_role'] == 10)
{
	include_once ("includes/setup_pemohon.inc.php");
	$myusrid = $my_details->usr_id_sys;
}
else
{
	include_once ("includes/setup.inc.php");
	$myusrid = $my_details->usr_id;
}

$num_errors = 0;
$id_pemohon = fetch_postget_val("id_pemohon", $_POST, $_GET);
$current_id = fetch_postget_val("current_id", $_POST, $_GET);
$table = fetch_postget_val("table", $_POST, $_GET);
$id_table = fetch_postget_val("id_table", $_POST, $_GET);
$id_insert = fetch_postget_val("id_insert", $_POST, $_GET);
$id_update = fetch_postget_val("id_update", $_POST, $_GET);
$type = fetch_postget_val("type", $_POST, $_GET);
$program = fetch_postget_val("program", $_POST, $_GET);
$borang_div = fetch_postget_val("borang_div", $_POST, $_GET);
$active = fetch_postget_val("active", $_POST, $_GET);
$dateTimeNow = dateTimeNow();

if($id_table == 'id_pemohon')
{
	$current_id = $id_pemohon;
}

//get data from pautan table
if($type == 2)//modal popup
{
	$indicator_Query = "SELECT B.*
					  FROM u_pautanborang A
					  LEFT JOIN u_borangitem B ON A.id_borangitem = B.id_borangitem
					  WHERE A.aktif = 1 
					  AND B.id_inputtype IN (1,2,3,4,8,12,21)
					  AND B.aktif = 1
					  AND B.linked_borangitem = ?
					  AND A.id_program = ?";
	$PDO_stmt = $PDO->prepare($indicator_Query);
	$PDO_stmt->bindValue(1, fetch_postget_val("borangitem", $_POST, $_GET), PDO::PARAM_INT);
	$PDO_stmt->bindValue(2, fetch_postget_val("program", $_POST, $_GET), PDO::PARAM_INT);
}
else //others
{
	$indicator_Query = "SELECT B.*
					  FROM u_pautanborang A
					  LEFT JOIN u_borangitem B ON A.id_borangitem = B.id_borangitem
					  WHERE A.aktif = 1 
					  AND B.id_inputtype IN (1,2,3,4,8,12,20)
				      AND B.aktif = 1
					  AND B.id_borangdiv = ?
					  AND A.id_program = ?";

	$PDO_stmt = $PDO->prepare($indicator_Query);
	$PDO_stmt->bindValue(1, fetch_postget_val("borang_div", $_POST, $_GET), PDO::PARAM_INT);
	$PDO_stmt->bindValue(2, fetch_postget_val("program", $_POST, $_GET), PDO::PARAM_INT);
}
$PDO_stmt->execute();
$borangitem_List = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$borangitem_List[] = $result;
}

try
{
	$PASS_COUNTER = 0; //COUNTER FOR DOUBLE CONFIRM
	$PASS_VALUE = 1; //NUMBER OF QUERY TO EXECUTE. IMPORTANT FOR CHECKING!!

	//PDO INIT
	$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$PDO->beginTransaction();

	$value_List = array();
	$indicator_Array = array();
	for($num = 0; $num < count($borangitem_List); $num++)
	{
		$column = $borangitem_List[$num]['value'];
		if($column == 'id_pemohon'){
			$value = $id_pemohon;
		}else{
			$value = fetch_postget_val($borangitem_List[$num]['input_name'], $_POST, $_GET);
		}
		
		$ignore_capital = 0;
		if($borangitem_List[$num]['value_type'] == 2)//INT
		{
			$type = PDO::PARAM_INT;
		}
		else
		{
			$type = PDO::PARAM_STR;
			if($borangitem_List[$num]['value_type'] == 3)//No Capital
			{
				$ignore_capital = 1;
			}
			else if($borangitem_List[$num]['value_type'] == 4)//format date
			{
				$value = date("Y-m-d", strtotime($value));
			}
		}
		
		if($borangitem_List[$num]['id_inputtype'] == 20)//DATE
		{
			$type = PDO::PARAM_STR;
			$value = formatDate($value, 'Y-m-d');
		}
		
		//special case for pemohon DUN
		if($table == 'm_pemohon' && $column == 'id_dun')
		{
			$special_Query = "SELECT id_parlimen
							  FROM i_dun
							  WHERE id_dun = $value";
			$special_Result = $PDO->prepare($special_Query);
			$special_Result->execute();
			$special_List = $special_Result->fetch(PDO::FETCH_ASSOC);
			$indicator_Array[] = array('id_parlimen', $special_List['id_parlimen'], PDO::PARAM_INT);
		}
		
		$value_List[$column] = $value;
		
		$indicator_Array[] = array($column, $value, $type, $ignore_capital);
	}

	if($current_id == 0)
	{
		if($id_insert != '')
		{
			$tkh = 'tkh_'.end(explode('_', $id_insert));
			$indicator_Array[] = array($id_insert, $myusrid, PDO::PARAM_INT);
			$indicator_Array[] = array($tkh, $dateTimeNow, PDO::PARAM_STR);
		}
		
		//special case for m_waris
		if($table == 'm_waris')
		{
			$indicator_Array[] = array('status', 1, PDO::PARAM_INT);
		}
		
		//special case for m_anakemas
		if($table == 'm_anakemas')
		{
			$indicator_Array[] = array('id_statusmohon', 1, PDO::PARAM_INT);
		}
		
		$PASS_COUNTER += PDO_INSERT($PDO, $table, $indicator_Array);
		$current_id = $PDO->lastInsertId();
		
		$PASS_VALUE += 1;
		$PASS_COUNTER += PDO_INSERT_AUDIT($PDO, $table, $indicator_Array, $id_pemohon, $current_id, $myusrid, $dateTimeNow, 1);
		
	}
	else
	{
		//special case for m_waris
		if($table == 'm_waris')
		{
			$kpbaru_waris = fetch_postget_val("borang_kpbaru_waris", $_POST, $_GET);
			if($kpbaru_waris != '')
			{	
				$indicator_Query = "SELECT *
									FROM m_waris
									WHERE id_pemohon = ?
									AND status = 1";
				$PDO_stmt = $PDO->prepare($indicator_Query);
				$PDO_stmt->bindValue(1, $id_pemohon, PDO::PARAM_INT);
				$PDO_stmt->execute();
				$waris_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
				$id_waris = $waris_List['id_waris'];
				
				if($id_waris > 0)
				{	
					if($waris_List['kpbaru_waris'] == $kpbaru_waris)
					{
						if($id_update != '')
						{
							$tkh = 'tkh_'.end(explode('_', $id_update));
							$indicator_Array[] = array($id_update, $myusrid, PDO::PARAM_INT);
							$indicator_Array[] = array($tkh, $dateTimeNow, PDO::PARAM_STR);
						}
						
						$whereCondition = "WHERE id_waris = $id_waris";
						$PASS_COUNTER += PDO_UPDATE_AUDIT($PDO, $table, $indicator_Array, $whereCondition, $id_pemohon, $current_id, $myusrid, $dateTimeNow, 0);
					}
					else
					{
						$existWaris_Array = [];
						if($id_update != '')
						{
							$tkh = 'tkh_'.end(explode('_', $id_update));
							$existWaris_Array[] = array($id_update, $myusrid, PDO::PARAM_INT);
							$existWaris_Array[] = array($tkh, $dateTimeNow, PDO::PARAM_STR);
						}
						$existWaris_Array[] = array('status', 0, PDO::PARAM_INT);
						$PASS_VALUE += 1;
						$whereCondition = "WHERE id_waris = $id_waris";
						$PASS_COUNTER += PDO_UPDATE_AUDIT($PDO, $table, $existWaris_Array, $whereCondition, $id_pemohon, $current_id, $myusrid, $dateTimeNow, 0);
						
						if($id_insert != '')
						{
							$tkh = 'tkh_'.end(explode('_', $id_insert));
							$indicator_Array[] = array($id_insert, $myusrid, PDO::PARAM_INT);
							$indicator_Array[] = array($tkh, $dateTimeNow, PDO::PARAM_STR);
						}
						$indicator_Array[] = array('status', 1, PDO::PARAM_STR);
						$PASS_COUNTER += PDO_INSERT($PDO, $table, $indicator_Array);
						$current_id = $PDO->lastInsertId();
						
						$PASS_VALUE += 1;
						$PASS_COUNTER += PDO_INSERT_AUDIT($PDO, $table, $indicator_Array, $id_pemohon, $current_id, $myusrid, $dateTimeNow, 1);
					}
				}
				else
				{
					if($id_insert != '')
					{
						$tkh = 'tkh_'.end(explode('_', $id_insert));
						$indicator_Array[] = array($id_insert, $myusrid, PDO::PARAM_INT);
						$indicator_Array[] = array($tkh, $dateTimeNow, PDO::PARAM_STR);
					}
					$indicator_Array[] = array('status', 1, PDO::PARAM_STR);
					$PASS_COUNTER += PDO_INSERT($PDO, $table, $indicator_Array);
					$current_id = $PDO->lastInsertId();
					
					$PASS_VALUE += 1;
					$PASS_COUNTER += PDO_INSERT_AUDIT($PDO, $table, $indicator_Array, $id_pemohon, $current_id, $myusrid, $dateTimeNow, 1);
				}
			}
		}
		else
		{
			if($id_update != '')
			{
				$tkh = 'tkh_'.end(explode('_', $id_update));
				$indicator_Array[] = array($id_update, $myusrid, PDO::PARAM_INT);
				$indicator_Array[] = array($tkh, $dateTimeNow, PDO::PARAM_STR);
			}
			
			if($borang_div == 3 && $active == 1)
			{
				$indicator_Array[] = array('id_aktifmohon', 1, PDO::PARAM_STR);				
				$indicator_Array[] = array('tkh_aktifmohon', $dateTimeNow, PDO::PARAM_STR);				
					
				$PASS_VALUE += 1;
				$PASS_COUNTER += PDO_INSERT_ACTION_AUDIT($PDO, $id_pemohon, 36, $my_details->usr_id, $my_details->user_level, $dateTimeNow, '', $my_details->user_dun, '', '', '', '', '', $program);			
			}
			
			//special case for bank
			//if($borang_div == 7 && $value_List['id_carabayar'] == 2)
			if($borang_div == 7)
			{
				//get existing bank and akaun
				$special_Query = "SELECT id_bank, no_akaun, id_aktifmohon, flag_saheft, id_byrkepada
								  FROM m_pemohon
								  WHERE id_pemohon = $id_pemohon";
				$special_Result = $PDO->prepare($special_Query);
				$special_Result->execute();
				$special_List = $special_Result->fetch(PDO::FETCH_ASSOC);
				
				//if((($special_List['id_bank'] != $value_List['id_bank']) || ($special_List['no_akaun'] != $value_List['no_akaun'])) && $special_List['flag_saheft'] == 1)
				if(($special_List['no_akaun'] != $value_List['no_akaun'] || $special_List['id_byrkepada'] != $value_List['id_byrkepada']) && $special_List['flag_saheft'] == 1)
				{
					$indicator_Array[] = array('flag_saheft', 0, PDO::PARAM_INT);
					$indicator_Array[] = array('id_saheft', 0, PDO::PARAM_INT);
					$indicator_Array[] = array('tkh_saheft', '0000-00-00 00:00:00', PDO::PARAM_STR);	

					if($special_List['id_aktifmohon'] != 1)
					{
						$indicator_Array[] = array('id_aktifmohon', 1, PDO::PARAM_INT);
						$indicator_Array[] = array('tkh_aktifmohon', $dateTimeNow, PDO::PARAM_STR);					
					}
					
					$PASS_VALUE += 1;
					$PASS_COUNTER += PDO_INSERT_ACTION_AUDIT($PDO, $id_pemohon, 35, $my_details->usr_id, $my_details->user_level, $dateTimeNow, $my_details->user_jab, $my_details->user_dun, $my_details->user_daerah, 'PENGEMASKINIAN MAKLUMAT BANK/PEMBAYARAN', '', '', $my_details->user_parlimen, $program);
				}
			}
			
			$whereCondition = "WHERE $id_table = $current_id";
			$PASS_COUNTER += PDO_UPDATE_AUDIT($PDO, $table, $indicator_Array, $whereCondition, $id_pemohon, $current_id, $myusrid, $dateTimeNow, 0);
		}
	}

	//*****************************************
	//COMMIT if PASS_COUNTER == PASS_VALUE
	//*****************************************
	if ($PASS_COUNTER == $PASS_VALUE)
	{
		$PDO->commit();
		echo $current_id;
	}
	else
	{
		$PDO->rollBack();
		echo -1;
	}
}
catch (Exception $e) 
{
	$PDO->rollBack();
	echo "Failed: " . $e->getMessage();
}

?>