<?php

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

$num_errors = 0;
$id_anakemas = fetch_postget_val("id_anakemas", $_POST, $_GET);
$program = 5;
$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();

	// 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_anakemas WHERE YEAR(tkh_cipta) = ?";
	$PDO_stmt = $PDO->prepare($indicator_Query);
	$PDO_stmt->bindValue(1, $year, PDO::PARAM_STR);
	$PDO_stmt->execute();
	$result = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
	$total_count = $result['total_count'];

	$indicator_Query = "SELECT A.*
					  FROM m_anakemas A
					  WHERE A.id_anakemas = ?";
	$PDO_stmt = $PDO->prepare($indicator_Query);
	$PDO_stmt->bindValue(1, $id_anakemas, PDO::PARAM_INT);
	$PDO_stmt->execute();
	$ae_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
	
	//$no_siri = $kod_program.'/'.$year.'/0'.$caramohon.'/'.sprintf('%06d', ($total_count + 1));
	
	if($ae_List['no_sirianakemas'] != '')
	{
		$no_siri = $ae_List['no_sirianakemas'];
	}
	else
	{
		do{
			$total_count += 1;
			$no_siri = $kod_program.'/'.$year.'/0'.$caramohon.'/'.sprintf('%06d', ($total_count));
			$check_siri_Query = "SELECT id_anakemas FROM m_anakemas WHERE no_sirianakemas = ?";
			$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_anakemas'] > 0;
		}
		while($is_nosiri_exist);
	}
	
	if($my_details->user_level == 10)
	{
		$log = 16;
		$myid = $my_details->usr_id_sys;
		$id_statusmohon = 2;
	}
	else
	{
		$log = 40;
		$myid = $ae_List['id_pemohon'];
		$id_statusmohon = $ae_List['id_statusmohon'];
	}
	
	$indicator_Array = array();
	$indicator_Array[] = array('id_statusmohon', $id_statusmohon, PDO::PARAM_INT);
	$indicator_Array[] = array('no_sirianakemas', $no_siri, PDO::PARAM_STR);
	$indicator_Array[] = array('id_carahantar', fetch_postget_val("borang_carahantarae", $_POST, $_GET), PDO::PARAM_INT);
	$indicator_Array[] = array('ulasan_terima', '', PDO::PARAM_INT);
	if($my_details->user_level == 10)
	{
		$indicator_Array[] = array('id_daftar', $my_details->usr_id_sys, PDO::PARAM_INT);
		$indicator_Array[] = array('tkh_daftar', $dateTimeNow, PDO::PARAM_STR);
	}
	$whereCondition = "WHERE id_anakemas = $id_anakemas";
	$PASS_COUNTER += PDO_UPDATE($PDO, 'm_anakemas', $indicator_Array, $whereCondition);
	
	$PASS_VALUE += 1;
	$PASS_COUNTER += PDO_INSERT_ACTION_AUDIT($PDO, $myid, $log, $my_details->usr_id, $my_details->user_level, $dateTimeNow, '', '', '', '', $id_anakemas, '', '', $program);

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

?>