<?php

include_once ("includes/setup.inc.php");
//Header("Content-type: application/xml");
Header("Cache-Control: no-cache");

$num_errors = 0;
$klinik = fetch_postget_val("klinik_vat", $_POST, $_GET);
$appoint_date = fetch_postget_val("appoint_date", $_POST, $_GET);
$dateTimeNow = dateTimeNow();

$date = date_create($appoint_date);
$dayofweek = date_format($date, "w");

$indicator_Query = "select * 
					FROM i_vatklinik_hour
					WHERE id_vatklinik = ? AND day_of_week = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $klinik, PDO::PARAM_INT);
$PDO_stmt->bindValue(2, $dayofweek, PDO::PARAM_INT);
$PDO_stmt->execute();
$hour_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$start_datetime = date("Y-m-d H:i:s", strtotime($appoint_date.' '.$hour_List['start_hour']));
$end_datetime = date("Y-m-d H:i:s", strtotime($appoint_date.' '.$hour_List['end_hour']));
$start_break_datetime = date("Y-m-d H:i:s", strtotime($appoint_date.' '.$hour_List['start_break']));
$end_break_datetime = date("Y-m-d H:i:s", strtotime($appoint_date.' '.$hour_List['end_break']));

// Convert datetime to Unix timestamp
$start_timestamp = strtotime($start_datetime);
$end_timestamp = strtotime($end_datetime);
$start_break_timestamp = strtotime($start_break_datetime);
$end_break_timestamp = strtotime($end_break_datetime);

//no need this for klinik time slot
/*
$indicator_Query = "select appointment_time 
					FROM m_vatklinik_appointment
					WHERE is_active AND id_vatklinik = ? AND appointment_date = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $klinik, PDO::PARAM_INT);
$PDO_stmt->bindValue(2, date_format($date, "Y-m-d"), PDO::PARAM_STR);
$PDO_stmt->execute();
$appointmentTime_List = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$appointmentTime_List[$result['appointment_time']] = 1;
}
*/

$slot_seconds = $hour_List['slot_minute'] * 60;

for($timestamp = $start_timestamp; $timestamp < $end_timestamp; $timestamp += $slot_seconds)
{
	//$datetime = date("Y-m-d H:i:s", $timestamp);
	if($timestamp < $start_break_timestamp || $timestamp >= $end_break_timestamp)
	{
		$timeslot = date("H:i:s", $timestamp);
		//if($appointmentTime_List[$timeslot] != 1)
		{
			echo '<option>'.date("H:i", $timestamp).'</option>';
		}
	}
}

?>