<?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;
}
Header("Cache-Control: no-cache");

$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);
$dateTimeNow = dateTimeNow();

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

//get data from pautan table
$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)
				  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);
$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();

	$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);
		}
		
		if($borangitem_List[$num]['value_type']==3)//date
		{
			$value = formatDate('Y-m-d', $value);
			$type = PDO::PARAM_STR;
		}
		else
		{
			$type = (($borangitem_List[$num]['value_type']==2)?(PDO::PARAM_INT):(PDO::PARAM_STR));
		}
		$indicator_Array[] = array($column, $value, $type);
	}

	if($current_id == 0)
	{
		if($program == 5)
		{
			$indicator_Array[] = array('id_statusmohon', 1, PDO::PARAM_INT);
		}
		
		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);
		}
		$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);
		}
		$whereCondition = "WHERE $id_table = $current_id";
		$PASS_COUNTER += PDO_UPDATE($PDO, $table, $indicator_Array, $whereCondition);
		
		$PASS_VALUE += 1;
		$PASS_COUNTER += PDO_UPDATE_AUDIT($PDO, $table, $indicator_Array, $whereCondition, $id_pemohon, $current_id, $myusrid, $dateTimeNow, 1);
		
	}

	//*****************************************
	//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();
}

?>