<?php
require_once(SABLE_WORKING_PATH.'booking/booking_func.php');

$bsid = $_SESSION['caller']['args'][1];

function getServiceDetails($bsid)
{
	global $sableDB;
	//$rows	= getDayData(new DateTime('2021-08-23'));
	$rows	= getDayData(new DateTime());
	$res	= [];
	foreach($rows as $row)
	{
		$bkid = $sableDB->getValue('select booking_id from booking_schedule where bs_id = '.$bsid);
		if ($row['booking_id'] == $bkid)
		{
			$res = $row;
			break;
		}
	}
	return $res;
}

function printBuildingServiceInfo($bsid, $row)
{
	$html = '';
	$idx = 0;
	foreach($row['service_info'] as $item)
	{
		$price = $item['bs_price_override'] ? $item['bs_price_override'] : $item['service_price'];
		$idx++;
		$html .= '
		<tr>
			<td colspan="2"><h4>'.$idx.':</h4></td>
		</tr>	
		<tr>
			<td><label>Building:</label></td>
			<td><span>'.$item['building_name'].' ['.$item['area_name'].']</span></td>
		</tr>
		<tr>
			<td><label>Service:</label></td>
			<td><span>'.$item['service_name'].'</span></td>
		</tr>
		<tr>
			<td><label>Price:</label></td>
			<td><span>R'.number_format($price, 2).'</span></td>
		</tr>
		<tr>
			<td><label>Duration:</label></td>
			<td><span>'.getHumanDuration($item['service_duration']).'</span></td>
		</tr>';
		if (!$row['bs_completed'])
		{
			$html .= '<tr>
						<td></td>
						<td>
							<button type="button" id="booking_service_'.$bsid.'_'.$item['service_id'].'" 
									onClick="cancelScheduledItem('.$bsid.','.$item['service_id'].','.$item['building_id'].')">
								<i class="fa fa-ban"></i>&nbsp;Cancel
							</button>
						</td>
					</tr>';
		}
		$html .= '
		<tr>
			<td colspan="2">&nbsp;</td>
		</tr>';
	}
	echo($html);
}

function addAppTabs($buttons)
{
	$tabClass = "col-xs-3 col-sm-2 col-md-1 col-lg-1 col-xl-1 pointer no-padding text-center";
	$html = '';
	foreach($buttons as $button)
	{
		$icon = null;
		$but = $button['icon'];
		$sty = "font-size:1.5em;"; //style="'.$sty.'"
		$icon = '<i class="fa fa-2x '.$but.'" ></i>';

		$html.='<div class="'.$tabClass.'">
					<div class="sable-tab" title="'.$button['title'].'">'.$icon.'</div>
				</div>';
	}
	echo($html);
}

function getClientAddress($addressId)
{
	global $sableDB;
	return $sableDB->getValue('select address from address where address_id = '.$addressId);
}

function printClientAddress($addressId)
{
	echo(getClientAddress($addressId));
}

function getTotalDue($bsid, $clientid)
{
	global $sableDB;
	$sql = 'select SUM(coalesce(bs_price_override, service_price))
			from booking_schedule 
			inner join booking on booking.booking_id = booking_schedule.booking_id 
			inner join booking__service on booking__service.booking_id = booking_schedule.booking_id 
			inner join service on service.service_id = booking__service.service_id 
			where (booking_schedule.bs_id = '.$bsid.') and (booking__service.service_active = 1)';
	$total		= floatval($sableDB->getValue($sql));
	$balance	= floatval(getCurrentBalance($clientid));
	$ret		= 'R '.number_format($total+$balance, 2);
	return $ret;
}

function printTotalDue($bsid, $clientid)
{
	echo(getTotalDue($bsid, $clientid));
}

function getServiceComments($bsid)
{
	global $sableDB;
	$sql = 'select concat(user_name, " ", user_surname) as user_full_name, bs_timestamp, bs_comment  
			from booking_schedule__comment 
			inner join user on user.user_code = booking_schedule__comment.user_code 
			where bs_id = '.$bsid.' order by bs_timestamp DESC';
	$rows = $sableDB->getRows($sql);
	$html = '';
	$ts = null;
	if ($rows)
	{
		foreach($rows as $row)
		{
			$ts = new DateTime($row['bs_timestamp']);
			$html.="<tr>
						<td colspan='3'><h4>".$row['user_full_name']." - ".$ts->format('d F Y')." - ".$ts->format('H:i:s')."</h4></td>
					</tr>
					<tr>
						<td colspan='3'>".$row['bs_comment']."</td>
					</tr>";
			unset($ts);
		}
	}
	return $html;
}

function printServiceComments($bsid)
{
	echo(getServiceComments($bsid));
}

function getCurrentBalance($clientid)
{
	global $sableDB;
	$sql = 'select SUM(txn_amount) from finance where client_id = '.$clientid;
	$total = $sableDB->getValue($sql);
	$ret = 'R '.number_format($total, 2);
	return $ret;
}

function printCurrentBalance($clientid)
{
	echo(getCurrentBalance($clientid));
}