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

$indicator_Query = "SELECT i_vatklinik.id_vatklinik, i_vatklinik.resit_counter, i_vatklinik.rawatan_fee
				  FROM m_vatklinik_appointment
				  JOIN i_vatklinik ON i_vatklinik.id_vatklinik = m_vatklinik_appointment.id_vatklinik
				  WHERE id_vatklinik_appointment = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $id, PDO::PARAM_INT);
$PDO_stmt->execute();
$appointment_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$indicator_Query = "SELECT id_appointment_ubat, i_ubat.price, m_appointment_ubat.quantity
				  FROM m_appointment_ubat
				  JOIN i_ubat ON i_ubat.id_ubat = m_appointment_ubat.id_ubat
				  WHERE id_vatklinik_appointment = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $id, PDO::PARAM_INT);
$PDO_stmt->execute();
$ubat_List = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$ubat_List[] = $result;
}

$resit_number = $appointment_List['id_vatklinik'].sprintf('%02d', (date('Y') - 2022)).sprintf('%06d', $appointment_List['resit_counter'] + 1);
try
{
	$PASS_COUNTER = 0; //COUNTER FOR DOUBLE CONFIRM
	$PASS_VALUE = 3; //NUMBER OF QUERY TO EXECUTE. IMPORTANT FOR CHECKING!!

	//PDO INIT
	$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$PDO->beginTransaction();

	$indicator_Array = array();
	$indicator_Array[] = array('resit_counter', $appointment_List['resit_counter'] + 1, PDO::PARAM_INT);
	$whereCondition = "WHERE id_vatklinik = ".$appointment_List['id_vatklinik'];
	$PASS_COUNTER += PDO_UPDATE($PDO, 'i_vatklinik', $indicator_Array, $whereCondition);
	
	$total_resit = $appointment_List['rawatan_fee'];
	for($num = 0; $num < count($ubat_List); $num++)
	{
		$indicator_Array = array();
		$indicator_Array[] = array('price_ubat', $ubat_List[$num]['price'], PDO::PARAM_STR);
		$whereCondition = "WHERE id_appointment_ubat = ".$ubat_List[$num]['id_appointment_ubat'];
		$total_resit += ($ubat_List[$num]['price'] * $ubat_List[$num]['quantity']);
		$PASS_VALUE += 1;
		$PASS_COUNTER += PDO_UPDATE($PDO, 'm_appointment_ubat', $indicator_Array, $whereCondition);
	}
	
	$indicator_Array = array();
	$indicator_Array[] = array('id_status_appointment', 5, PDO::PARAM_INT);
	$indicator_Array[] = array('resit_number', $resit_number, PDO::PARAM_STR);
	$indicator_Array[] = array('tkh_resit', $dateTimeNow, PDO::PARAM_STR);
	$indicator_Array[] = array('rawatan_fee', $appointment_List['rawatan_fee'], PDO::PARAM_STR);
	$indicator_Array[] = array('total_resit', $total_resit, PDO::PARAM_STR);
	$whereCondition = "WHERE id_vatklinik_appointment = $id";
	$PASS_COUNTER += PDO_UPDATE($PDO, 'm_vatklinik_appointment', $indicator_Array, $whereCondition);
	
	$indicator_Array = array();
	$indicator_Array[] = array('id_status_appointment', 5, PDO::PARAM_INT);
	$indicator_Array[] = array('id_vatklinik_appointment', $id, PDO::PARAM_INT);
	$indicator_Array[] = array('insert_by', $my_details->usr_id, PDO::PARAM_INT);
	$indicator_Array[] = array('tkh_insert', $dateTimeNow, PDO::PARAM_STR);
	
	$PASS_COUNTER += PDO_INSERT($PDO, 's_appointment_status_log', $indicator_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();
}

?>