<?php

$kadun = fetch_postget_val("kadun", $_POST, $_GET);
$id_oku = fetch_postget_val("id_oku", $_POST, $_GET);
$id_daerahOKU = fetch_postget_val("id_daerahOKU", $_POST, $_GET);
$no_oku = fetch_postget_val("no_oku", $_POST, $_GET);
$program = fetch_postget_val("program", $_POST, $_GET);
$dateTimeNow = dateTimeNow();
$current_year = date('Y');

//get program from pemohon
$indicator_Query = "SELECT *
					FROM m_pemohon
					WHERE id_pemohon = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $myID, PDO::PARAM_INT);
$PDO_stmt->execute();
$pemohon_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

try
{
	$PASS_COUNTER = 0; //COUNTER FOR DOUBLE CONFIRM
	$PASS_VALUE = 0; //NUMBER OF QUERY TO EXECUTE. IMPORTANT FOR CHECKING!!
	
	//PDO INIT
	$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$PDO->beginTransaction();
	
	//*****************************************
	//INSERT m_tukarprogram only for type 1
	//*****************************************
	if($type == 1)
	{
		$tukarprogram_Array = array();
		$tukarprogram_Array[] = array('id_pemohon', $pemohon_List['id_pemohon'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('kpbaru_pemohon', $pemohon_List['kpbaru_pemohon'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('no_siriasal', $pemohon_List['no_siri'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('id_programasal', $pemohon_List['id_program'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('id_programbaru', $program, PDO::PARAM_STR);
		$tukarprogram_Array[] = array('id_dunasal', $pemohon_List['id_dun'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('id_parlimenasal', $pemohon_List['id_parlimen'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('id_byrkepadaasal', $pemohon_List['id_byrkepada'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('id_carabayarasal', $pemohon_List['id_carabayar'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('id_bankasal', $pemohon_List['id_bank'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('no_akaunasal', $pemohon_List['no_akaun'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('id_jenisakaunasal', $pemohon_List['id_jenisakaun'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('nama_kongsiakaunasal', $pemohon_List['nama_kongsiakaun'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('id_sbbtunaiasal', $pemohon_List['id_sbbtunai'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('id_caramohonasal', $pemohon_List['id_caramohon'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('id_statusmohonasal', $pemohon_List['id_statusmohon'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('id_statushidupasal', $pemohon_List['id_statushidup'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('id_aktifmohonasal', $pemohon_List['id_aktifmohon'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('tkh_aktifmohonasal', $pemohon_List['tkh_aktifmohon'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('id_saheftasal', $pemohon_List['id_saheft'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('tkh_saheftasal', $pemohon_List['tkh_saheft'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('id_daftarasal', $pemohon_List['id_daftar'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('tkh_daftarasal', $pemohon_List['tkh_daftar'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('id_terimaasal', $pemohon_List['id_terima'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('tkh_terimaasal', $pemohon_List['tkh_terima'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('id_sahasal', $pemohon_List['id_sah'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('tkh_sahasal', $pemohon_List['tkh_sah'], PDO::PARAM_STR);
		$tukarprogram_Array[] = array('id_daftar', $myuserid, PDO::PARAM_STR);
		$tukarprogram_Array[] = array('tkh_daftar', $dateTimeNow, PDO::PARAM_STR);
		$tukarprogram_Array[] = array('id_statustukar', 1, PDO::PARAM_STR);
		$PASS_VALUE += 1;
		$PASS_COUNTER += PDO_INSERT($PDO, 'm_tukarprogram', $tukarprogram_Array);
	}

	//*****************************************
	//UPDATE m_pemohon both type 1 & 2
	//*****************************************
	//get parlimen from kadun
	$indicator_Query = "SELECT *
						FROM i_dun
						WHERE id_dun = ?";
	$PDO_stmt = $PDO->prepare($indicator_Query);
	$PDO_stmt->bindValue(1, $kadun, PDO::PARAM_INT);
	$PDO_stmt->execute();
	$kadun_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

	$id_statusmohon = 1;
	if($my_details->user_level != 10)
	{
		$id_statusmohon = 2;
	}
	
	$pengguna_Array = array();
	$pengguna_Array[] = array('no_siri', '', PDO::PARAM_STR);
	$pengguna_Array[] = array('id_program', $program, PDO::PARAM_STR);
	$pengguna_Array[] = array('id_program_ori', $pemohon_List['id_program'], PDO::PARAM_STR);
	$pengguna_Array[] = array('tahun_program_ori', $current_year, PDO::PARAM_STR);
	$pengguna_Array[] = array('id_oku', $id_oku, PDO::PARAM_STR);
	$pengguna_Array[] = array('id_daerahOKU', $id_daerahOKU, PDO::PARAM_STR);
	$pengguna_Array[] = array('no_oku', $no_oku, PDO::PARAM_STR);
	$pengguna_Array[] = array('id_dun', $kadun, PDO::PARAM_STR);
	$pengguna_Array[] = array('id_daerahOKU', $kadun, PDO::PARAM_STR);
	$pengguna_Array[] = array('id_parlimen', $kadun_List['id_parlimen'], PDO::PARAM_STR);
	$pengguna_Array[] = array('id_carabayar', 0, PDO::PARAM_STR);
	$pengguna_Array[] = array('id_bank', 0, PDO::PARAM_STR);
	$pengguna_Array[] = array('no_akaun', '', PDO::PARAM_STR);
	if($my_details->user_level == 10)
	{
		$pengguna_Array[] = array('id_caramohon', 2, PDO::PARAM_STR);
	}
	$pengguna_Array[] = array('id_daftar', $myuserid, PDO::PARAM_STR);
	$pengguna_Array[] = array('tkh_daftar', $dateTimeNow, PDO::PARAM_STR); 
	$pengguna_Array[] = array('id_terima', 0, PDO::PARAM_STR);
	$pengguna_Array[] = array('tkh_terima', '', PDO::PARAM_STR);
	$pengguna_Array[] = array('id_sah', 0, PDO::PARAM_STR);
	$pengguna_Array[] = array('tkh_sah', '', PDO::PARAM_STR);
	$pengguna_Array[] = array('flag_saheft', 0, PDO::PARAM_STR);
	$pengguna_Array[] = array('id_saheft', 0, PDO::PARAM_STR);
	$pengguna_Array[] = array('tkh_saheft', '', PDO::PARAM_STR);
	$pengguna_Array[] = array('id_statusmohon', $id_statusmohon, PDO::PARAM_STR);
	$pengguna_Array[] = array('id_aktifmohon', 1, PDO::PARAM_STR);
	
	$PASS_VALUE += 1;
	$whereCondition = "WHERE id_pemohon = ".$myID;
	//$PASS_COUNTER += PDO_UPDATE($PDO, 'm_pemohon', $pengguna_Array, $whereCondition);
	$PASS_COUNTER += PDO_UPDATE_AUDIT($PDO, 'm_pemohon', $pengguna_Array, $whereCondition, $myID, $myID, $my_details->usr_id, $dateTimeNow, 0);

	$tpengguna_Array = array();
	$tpengguna_Array[] = array('id_oku', $id_oku, PDO::PARAM_STR);
	$tpengguna_Array[] = array('id_daerahOKU', $id_daerahOKU, PDO::PARAM_STR);
	$tpengguna_Array[] = array('no_oku', $no_oku, PDO::PARAM_STR);
	$tpengguna_Array[] = array('id_dun', $kadun, PDO::PARAM_STR);
	
	$PASS_VALUE += 1;
	$whereCondition = "WHERE id_pemohonsys = ".$myID;
	$PASS_COUNTER += PDO_UPDATE($PDO, 't_pemohon', $tpengguna_Array, $whereCondition);
	
	$PASS_VALUE += 1;
	$PASS_COUNTER += PDO_INSERT_ACTION_AUDIT($PDO, $myID, $log, $myuserid, $my_details->user_level, $dateTimeNow, '', '', '', '', '', '', '', $program);
	
	//*****************************************
	//COMMIT if PASS_COUNTER == PASS_VALUE
	//*****************************************
	if ($PASS_COUNTER == $PASS_VALUE)
	{
		$PDO->commit();
		echo $PASS_COUNTER;
	}
	else
	{
		$PDO->rollBack();
	}
	
}
catch (Exception $e) 
{
	$PDO->rollBack();
	echo "Failed: " . $e->getMessage();
}
?>