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

if($id == 0)
{
	$indicator_Query = "SELECT id_pemilik
					  FROM m_pemilik
					  WHERE id_jenispemilik = 1 AND kpbaru_pemilik = ?";
	$PDO_stmt = $PDO->prepare($indicator_Query);
	$PDO_stmt->bindValue(1, fetch_postget_val("kpbaru_pemilik", $_POST, $_GET), PDO::PARAM_STR);
	$PDO_stmt->execute();
	$result = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

	if($result['id_pemilik'] > 0)
	{
		echo 'Pemilik telah wujud dalam sistem';
		exit(0);
	}
}

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

	$kpbaru = fetch_postget_val("kpbaru_pemilik", $_POST, $_GET);
	$year = ((substr($kpbaru, 0, 2) <= date("y"))?'20':'19').substr($kpbaru, 0, 2);
	$tkh_lahir = $year."-".substr($kpbaru, 2, 2)."-".substr($kpbaru, 4, 2);
	$id_jantina = (substr($kpbaru, -1) % 2 == 0 ? 2 : 1);
	
	$indicator_Array = array();
	$indicator_Array[] = array('nama_pemilik', fetch_postget_val("nama_pemilik", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('kpbaru_pemilik', fetch_postget_val("kpbaru_pemilik", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('tkhlahir_pemilik', $tkh_lahir, PDO::PARAM_STR);
	$indicator_Array[] = array('id_jantina', $id_jantina, PDO::PARAM_STR);
	$indicator_Array[] = array('id_bangsa', fetch_postget_val("bangsa_pemilik", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('tel_pemilik', fetch_postget_val("tel_pemilik", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('alamat_pemilik', fetch_postget_val("alamat_pemilik", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('id_jenispemilik', 1, PDO::PARAM_INT);
	
	if($id == 0)
	{
		$indicator_Array[] = array('id_insert', $my_details->usr_id, PDO::PARAM_INT);
		$indicator_Array[] = array('tkh_insert', $dateTimeNow, PDO::PARAM_STR);
		$PASS_COUNTER += PDO_INSERT($PDO, 'm_pemilik', $indicator_Array);
		$pemilik_id = $PDO->lastInsertId();
		
		$indicator_Array2 = array();
		$indicator_Array2[] = array('id_pemohonsys', $pemilik_id, PDO::PARAM_INT);
		$indicator_Array2[] = array('id_peranan', 11, PDO::PARAM_INT);
		$indicator_Array2[] = array('login', fetch_postget_val("kpbaru_pemilik", $_POST, $_GET), PDO::PARAM_STR);
		$indicator_Array2[] = array('katalaluan', md5(fetch_postget_val("kpbaru_pemilik", $_POST, $_GET)), PDO::PARAM_STR);
		$indicator_Array2[] = array('nama', fetch_postget_val("nama_pemilik", $_POST, $_GET), PDO::PARAM_STR);
		$indicator_Array2[] = array('kpbaru', fetch_postget_val("kpbaru_pemilik", $_POST, $_GET), PDO::PARAM_STR);
		$indicator_Array2[] = array('tkh_lahir', $tkh_lahir, PDO::PARAM_STR);
		$indicator_Array2[] = array('id_cipta', $my_details->usr_id, PDO::PARAM_INT);
		$indicator_Array2[] = array('tkh_cipta', $dateTimeNow, PDO::PARAM_STR);
		$PASS_VALUE += 1;
		$PASS_COUNTER += PDO_INSERT($PDO, 't_pemohon', $indicator_Array2);
	}
	else
	{
		$indicator_Array[] = array('id_kemaskini', $my_details->usr_id, PDO::PARAM_INT);
		$indicator_Array[] = array('tkh_kemaskini', $dateTimeNow, PDO::PARAM_STR);
		$whereCondition = "WHERE id_pemilik = $id";
		$PASS_COUNTER += PDO_UPDATE($PDO, 'm_pemilik', $indicator_Array, $whereCondition);
	}

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

?>