<?php

include_once ("includes/setup.inc.php");
//Header("Content-type: application/xml");
Header("Cache-Control: no-cache");

$num_errors = 0;
$id = fetch_postget_val("id", $_POST, $_GET);
$dateTimeNow = dateTimeNow();

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();

	$program_Array = array();
	$program_Array[] = array('desk_program', fetch_postget_val("desk_program", $_POST, $_GET), PDO::PARAM_STR,1);
	$program_Array[] = array('kod_program', fetch_postget_val("kod_program", $_POST, $_GET), PDO::PARAM_STR);
	$program_Array[] = array('penerangan', htmlentities(fetch_postget_val_nocuci("penerangan_content", $_POST, $_GET)), PDO::PARAM_STR, 1);
	$program_Array[] = array('syarat', htmlentities(fetch_postget_val_nocuci("syarat_content", $_POST, $_GET)), PDO::PARAM_STR, 1);
	$program_Array[] = array('id_modborang', fetch_postget_val("id_modborang", $_POST, $_GET), PDO::PARAM_INT);
	$program_Array[] = array('jum_bayar', fetch_postget_val("jum_bayar", $_POST, $_GET), PDO::PARAM_INT);
	$program_Array[] = array('susunan', fetch_postget_val("susunan", $_POST, $_GET), PDO::PARAM_INT);
	$program_Array[] = array('aktif', fetch_postget_val("aktif", $_POST, $_GET), PDO::PARAM_INT);

	if($id == 0)
	{
		//*****************************************
		//NEW i_program
		//*****************************************
		$program_Array[] = array('id_keyin', $my_details->usr_id, PDO::PARAM_INT);
		$program_Array[] = array('tkh_keyin', $dateTimeNow, PDO::PARAM_STR);
		$PASS_COUNTER += PDO_INSERT($PDO, 'i_program', $program_Array);
		$id = $PDO->lastInsertId();
	}
	else
	{
		//*****************************************
		//UPDATE i_program
		//*****************************************
		$program_Array[] = array('id_kemaskini', $my_details->usr_id, PDO::PARAM_INT);
		$program_Array[] = array('tkh_kemaskini', $dateTimeNow, PDO::PARAM_STR);
		$whereCondition = "WHERE id_program = $id";
		//$PASS_COUNTER += PDO_UPDATE($PDO, 'i_program', $program_Array, $whereCondition);
		$PASS_COUNTER += PDO_UPDATE_INDIKATOR_AUDIT($PDO, 'i_program', $id, $program_Array, $whereCondition, $my_details->usr_id, $my_details->user_level, $dateTimeNow);
	}
	
	$PASS_VALUE += 1; //NUMBER OF QUERIES = 1
	//*****************************************
	//DELETE ALL FROM u_pautanborang
	//*****************************************
	$whereCondition = "WHERE id_program = $id";
	$PASS_COUNTER += PDO_DELETE($PDO, 'u_pautanborang', $whereCondition);
	
	//*****************************************
	//NEW u_pautanborang
	//*****************************************
	$allBorangitem = explode(',', fetch_postget_val("borangitem", $_POST, $_GET));
	$allCheckingitem = explode(',', fetch_postget_val("checkingitem", $_POST, $_GET));
	$checkingList = [];
	for($num = 0 ; $num < count($allCheckingitem) ; $num++)
	{
		$checkingList[$allCheckingitem[$num]] = 1;
	}
	for($num = 0 ; $num < count($allBorangitem) ; $num++)
	{
		if($allBorangitem[$num] != '')
		{
			$tingkap_Array = array();
			$tingkap_Array[] = array('id_program', $id, PDO::PARAM_INT);
			$tingkap_Array[] = array('id_borangitem', $allBorangitem[$num], PDO::PARAM_INT);
			$tingkap_Array[] = array('aktif', 1, PDO::PARAM_INT);
			$tingkap_Array[] = array('is_check_input', $checkingList[$allBorangitem[$num]], PDO::PARAM_INT);
			$tingkap_Array[] = array('id_kemaskini', $my_details->usr_id, PDO::PARAM_INT);
			$tingkap_Array[] = array('tkh_kemaskini', $dateTimeNow, PDO::PARAM_STR);

			$PASS_VALUE += 1; //NUMBER OF QUERIES = 1
			$PASS_COUNTER += PDO_INSERT($PDO, 'u_pautanborang', $tingkap_Array);
		}
	}
	
	//for dokumen
	$PASS_VALUE += 1; //NUMBER OF QUERIES = 1
	//*****************************************
	//DELETE ALL FROM i_dokumenprogram
	//*****************************************
	$whereCondition = "WHERE id_program = $id";
	$PASS_COUNTER += PDO_DELETE($PDO, 'i_dokumenprogram', $whereCondition);
	
	//*****************************************
	//NEW i_dokumenprogram
	//*****************************************
	$allNormalDok = explode(',', fetch_postget_val("normalDok", $_POST, $_GET));
	$allWajibDok = explode(',', fetch_postget_val("wajibDok", $_POST, $_GET));
	$checkingList = [];
	for($num = 0 ; $num < count($allWajibDok) ; $num++)
	{
		$checkingList[$allWajibDok[$num]] = 1;
	}
	for($num = 0 ; $num < count($allNormalDok) ; $num++)
	{
		if($allNormalDok[$num] != '')
		{
			$tingkap_Array = array();
			$tingkap_Array[] = array('id_program', $id, PDO::PARAM_INT);
			$tingkap_Array[] = array('id_dokumen', $allNormalDok[$num], PDO::PARAM_INT);
			$tingkap_Array[] = array('flag_wajib', $checkingList[$allNormalDok[$num]], PDO::PARAM_INT);
			$tingkap_Array[] = array('id_kemaskini', $my_details->usr_id, PDO::PARAM_INT);
			$tingkap_Array[] = array('tkh_kemaskini', $dateTimeNow, PDO::PARAM_STR);

			$PASS_VALUE += 1; //NUMBER OF QUERIES = 1
			$PASS_COUNTER += PDO_INSERT($PDO, 'i_dokumenprogram', $tingkap_Array);
		}
	}
	
	/*
	$PASS_VALUE += 1; //NUMBER OF QUERIES = 1
	//*****************************************
	//DELETE ALL FROM i_dokumenprogram
	//*****************************************
	$whereCondition = "WHERE id_program = $id";
	$PASS_COUNTER += PDO_DELETE($PDO, 'i_dokumenprogram', $whereCondition);
	
	//*****************************************
	//NEW i_dokumenprogram
	//*****************************************
	$allBorangitemdoc = explode(',', fetch_postget_val("borangitemdoc", $_POST, $_GET));
	$allCheckingitemdoc = explode(',', fetch_postget_val("checkingitemdoc", $_POST, $_GET));
	$checkingListdoc = [];
	for($num1 = 0 ; $num1 < count($allCheckingitemdoc) ; $num1++)
	{
		$checkingListdoc[$allCheckingitemdoc[$num1]] = 1;
	}
	for($num1 = 0 ; $num1 < count($allBorangitemdoc) ; $num1++)
	{
		if($allBorangitemdoc[$num1] != '')
		{
			$tingkapdoc_Array = array();
			$tingkapdoc_Array[] = array('id_program', $id, PDO::PARAM_INT);
			$tingkapdoc_Array[] = array('id_dokumen', $allBorangitemdoc[$num1], PDO::PARAM_INT);
			$tingkapdoc_Array[] = array('flag_wajib', $checkingListdoc[$allBorangitemdoc[$num1]], PDO::PARAM_INT);
			$tingkapdoc_Array[] = array('id_kemaskini', $my_details->usr_id, PDO::PARAM_INT);
			$tingkapdoc_Array[] = array('tkh_kemaskini', $dateTimeNow, PDO::PARAM_STR);

			$PASS_VALUE += 1; //NUMBER OF QUERIES = 1
			$PASS_COUNTER += PDO_INSERT($PDO, 'i_dokumenprogram', $tingkapdoc_Array);
		}
	}
	*/
	//*****************************************
	//COMMIT if PASS_COUNTER == PASS_VALUE
	//*****************************************
	if ($PASS_COUNTER == $PASS_VALUE)
	{
		$PDO->commit();
		echo $PASS_COUNTER;
	}
	else
	{
		$PDO->rollBack();
		echo -1;
	}
}
catch (Exception $e) 
{
	$PDO->rollBack();
	echo "Failed: " . $e->getMessage();
}

?> 

	





