<?php

if($_COOKIE['sejahtera_role'] == 10)
{
	include_once ("includes/setup_pemohon.inc.php");
	$log = 1;
	$caramohon = 2;
	$myusrid = $my_details->usr_id_sys;
}
else
{
	include_once ("includes/setup.inc.php");
	$log = 2;
	$caramohon = 1;
	$myusrid = $my_details->usr_id;
}
Header("Cache-Control: no-cache");

$num_errors = 0;
$id_pemohon = fetch_postget_val("id_pemohon", $_POST, $_GET);
$program = fetch_postget_val("program", $_POST, $_GET);
$dateTimeNow = dateTimeNow();
$current_year = date('Y');

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

	// m_pemohon no_siri
	// WE/2021/01/000001
	// We program
	// 2021 thn mohon
	// 01 online
	// 000001 siri program thn semasa
	
	$year = date('Y');
	
	$indicator_Query = "SELECT kod_program FROM i_program WHERE id_program = ?";
	$PDO_stmt = $PDO->prepare($indicator_Query);
	$PDO_stmt->bindValue(1, $program, PDO::PARAM_INT);
	$PDO_stmt->execute();
	$result = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
	$kod_program = $result['kod_program'];
	
	$indicator_Query = "SELECT COUNT(*) AS total_count FROM m_pemohon WHERE id_program = ? AND YEAR(tkh_daftar) = ?";
	$PDO_stmt = $PDO->prepare($indicator_Query);
	$PDO_stmt->bindValue(1, $program, PDO::PARAM_INT);
	$PDO_stmt->bindValue(2, $year, PDO::PARAM_STR);
	$PDO_stmt->execute();
	$result = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
	$total_count = $result['total_count'];
	
	do{
		$total_count += 1;
		$no_siri = $kod_program.'/'.$year.'/0'.$caramohon.'/'.sprintf('%06d', ($total_count));
		$check_siri_Query = "SELECT id_pemohon FROM m_pemohon WHERE no_siri = ?";
		$PDO_stmt = $PDO->prepare($check_siri_Query);
		$PDO_stmt->bindValue(1, $no_siri, PDO::PARAM_STR);
		$PDO_stmt->execute();
		$result = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
		$is_nosiri_exist = $result['id_pemohon'] > 0;
	}
	while($is_nosiri_exist);
	
	$indicator_Array = array();
	$indicator_Array[] = array('id_statusmohon', 2, PDO::PARAM_INT);
	$indicator_Array[] = array('no_siri', $no_siri, PDO::PARAM_STR);
	$indicator_Array[] = array('id_caramohon', $caramohon, PDO::PARAM_INT);
	$indicator_Array[] = array('id_carahantar', fetch_postget_val("borang_carahantar", $_POST, $_GET), PDO::PARAM_INT);
	$indicator_Array[] = array('id_program', $program, PDO::PARAM_STR);
	$indicator_Array[] = array('id_program_ori', $program, PDO::PARAM_STR);
	$indicator_Array[] = array('tahun_program_ori', $current_year, PDO::PARAM_STR);
	$indicator_Array[] = array('tkh_daftar', $dateTimeNow, PDO::PARAM_STR);
	$indicator_Array[] = array('id_kemaskini', $myusrid, PDO::PARAM_INT);
	$indicator_Array[] = array('tkh_kemaskini', $dateTimeNow, PDO::PARAM_STR);
	$whereCondition = "WHERE id_pemohon = $id_pemohon";
	$PASS_COUNTER += PDO_UPDATE($PDO, 'm_pemohon', $indicator_Array, $whereCondition);

	$PASS_VALUE += 1;
	$PASS_COUNTER += PDO_INSERT_ACTION_AUDIT($PDO, $id_pemohon, $log, $myusrid, $my_details->user_level, $dateTimeNow, $my_details->user_jab, $my_details->user_dun, $my_details->user_daerah, '', '', '', $my_details->user_parlimen, $program);

	//*****************************************
	//COMMIT if PASS_COUNTER == PASS_VALUE
	//*****************************************
	if ($PASS_COUNTER == $PASS_VALUE)
	{
		$PDO->commit();
		if($_COOKIE['sejahtera_role'] == 10)
		{
			echo 999;
		}
		else
		{
			echo 888;
		}
		//echo $PASS_COUNTER;
	}
	else
	{
		$PDO->rollBack();
		echo -1;
	}
}
catch (Exception $e) 
{
	$PDO->rollBack();
	echo "Failed: " . $e->getMessage();
}

?>