<?php
require_once(__DIR__.'/../includes/sysinit.php');
require_once(SABLE_WORKING_PATH.'client/client_func.php');
$_SESSION['listParams']['searchVisible'] = 1;
/*
$_SESSION['module']['back'] = 1;
$_SESSION['module']['backlink'] = SableSystem::getParameterLink('booking', 'l.php', $clientId);
*/
$_SESSION['module']['addlink'] = SableSystem::getCallPageLink('/booking/add.php');
$_SESSION['module']['addtext'] = 'Add Booking';

$clientid = 0;
if (empty($_POST))
{
	$clientid = $_SESSION['caller']['args'][1];
	$_SESSION['caller']['args'][1] = $_SESSION['caller']['args'][sizeof($_SESSION['caller']['args']) - 1];
	unset($_SESSION['caller']['args'][2]);
	unset($_SESSION['caller']['args'][3]);
	unset($_SESSION['caller']['args'][4]);
}

$_SESSION['listParams']['searchField'][0]['title'] = 'client_full_name';
$_SESSION['listParams']['searchField'][0][0] = 'client_name';
$_SESSION['listParams']['searchField'][0][1] = 'client_surname';
$_SESSION['listParams']['searchField'][1]['title'] = 'schedule_date';
$_SESSION['listParams']['searchField'][1][0] = 'booking_schedule.booking_time';
$_SESSION['listParams']['searchField'][2]['title'] = 'schedule_time';
$_SESSION['listParams']['searchField'][2][0] = 'booking_schedule.booking_time';
$_SESSION['listParams']['searchField'][3] = 'service_name';
$_SESSION['listParams']['searchField'][4] = 'service_charge';
$_SESSION['listParams']['searchField'][5] = 'area_type_name';
$_SESSION['listParams']['searchField'][6] = 'area_name';
$_SESSION['listParams']['searchField'][7] = 'building_name';
$_SESSION['listParams']['searchField'][8] = 'bs_active';
$_SESSION['listParams']['searchField'][9] = 'bs_completed';
$_SESSION['listParams']['searchField'][10] = 'service_active';

$_SESSION['listParams']['listSql'] = 	
	"select booking.booking_id, concat(client_name, ' ', client_surname) as client_full_name, client_name, client_surname, 
	DATE(booking_schedule.booking_time) as schedule_date, TIME(booking_schedule.booking_time) as schedule_time, 
	service.service_name, 
	COALESCE(booking__service.bs_price_override, service.service_price) as service_charge, area_type_name, area_name, building_name, 
	bs_active, bs_completed, service_active
	from booking 
	inner join client on client.client_id = booking.client_id 
	left join booking_schedule on booking_schedule.booking_id = booking.booking_id 
	left join booking__service on booking__service.booking_id = booking.booking_id 
	left join service on service.service_id = booking__service.service_id 
	left join building on building.building_id = booking__service.building_id 
	left join area on area.area_id = building.area_id 
	left join area_type on area_type.area_id = area.area_id 
	where ";

if ($clientid)
{
	$_SESSION['listParams']['listSql'] .= "(client.client_id = ".$clientid.") and ";
}
$_SESSION['listParams']['listSql'] .= "booking_schedule.booking_time < CURRENT_TIMESTAMP() and (bs_deleted = 0)";


$_SESSION['listParams']['module']					= "booking";
$_SESSION['listParams']['page']						= 0;
$_SESSION['listParams']['offset']					= 50; //the number of records to display per page
$_SESSION['listParams']['parentId']					= ""; //
$_SESSION['listParams']['moduleFolder']				= "booking";
$_SESSION['listParams']['moduleFolderDesc']			= "Booking History";
$_SESSION['listParams']['modulePage']				= "history.php";
$_SESSION['listParams']['moduleIdField']			= "booking_id";
$_SESSION['listParams']['moduleDescField']			= "schedule_date";
$_SESSION['listParams']['orderField']				= 2;
$_SESSION['listParams']['orderDirection']			= 1;

$_SESSION['listParams']['listHeadings'][0] = "Client";
$_SESSION['listParams']['listHeadings'][1] = "Booking Date";
$_SESSION['listParams']['listHeadings'][2] = "Booking Time";
$_SESSION['listParams']['listHeadings'][3] = "Service";
$_SESSION['listParams']['listHeadings'][4] = "Price";
$_SESSION['listParams']['listHeadings'][5] = "Area";
$_SESSION['listParams']['listHeadings'][6] = "Area";
$_SESSION['listParams']['listHeadings'][7] = "Building Name";
$_SESSION['listParams']['listHeadings'][8] = "Booking Cancelled?";
$_SESSION['listParams']['listHeadings'][9] = "Booking Completed?";
$_SESSION['listParams']['listHeadings'][10] = "Service Cancelled?";
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_USER){	$_SESSION['listParams']['listHeadings'][11] = "";} //View


$_SESSION['listParams']['listHeadingWidth'][0] = "12%";
$_SESSION['listParams']['listHeadingWidth'][1] = "10%";
$_SESSION['listParams']['listHeadingWidth'][2] = "10%";
$_SESSION['listParams']['listHeadingWidth'][3] = "20%";
$_SESSION['listParams']['listHeadingWidth'][4] = "7%";
$_SESSION['listParams']['listHeadingWidth'][5] = "12%";
$_SESSION['listParams']['listHeadingWidth'][6] = "12%";
$_SESSION['listParams']['listHeadingWidth'][7] = "12%";
$_SESSION['listParams']['listHeadingWidth'][8] = "12%";
$_SESSION['listParams']['listHeadingWidth'][9] = "12%";
$_SESSION['listParams']['listHeadingWidth'][10] = "12%";
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_USER){	$_SESSION['listParams']['listHeadingWidth'][11] = "5%";} //View


$_SESSION['listParams']['listFields'][0]['field']		= "client_full_name";
$_SESSION['listParams']['listFields'][1]['field']		= "schedule_date";
$_SESSION['listParams']['listFields'][1]['date']		= "1";
$_SESSION['listParams']['listFields'][2]['field']		= "schedule_time";
$_SESSION['listParams']['listFields'][2]['time']		= "1";
$_SESSION['listParams']['listFields'][2]['format']		= "H:i";
$_SESSION['listParams']['listFields'][3]['field']		= "service_name";
$_SESSION['listParams']['listFields'][4]['field']		= "service_charge";
$_SESSION['listParams']['listFields'][4]['currency']	= "1";
$_SESSION['listParams']['listFields'][5]['field']		= "area_type_name";
$_SESSION['listParams']['listFields'][6]['field']		= "area_name";
$_SESSION['listParams']['listFields'][7]['field']		= "building_name";

$_SESSION['listParams']['listFields'][8]['field']		= "bs_active";
$_SESSION['listParams']['listFields'][8]['yesNo']		= 1;
$_SESSION['listParams']['listFields'][8]['yesNoYes']	= 'No';
$_SESSION['listParams']['listFields'][8]['yesNoNo']		= 'Yes';

$_SESSION['listParams']['listFields'][9]['field']		= "bs_completed";
$_SESSION['listParams']['listFields'][9]['yesNo']		= 1;
$_SESSION['listParams']['listFields'][9]['yesNoYes']	= 'Yes';
$_SESSION['listParams']['listFields'][9]['yesNoNo']		= 'No';

$_SESSION['listParams']['listFields'][10]['field']		= "service_active";
$_SESSION['listParams']['listFields'][10]['yesNo']		= 1;
$_SESSION['listParams']['listFields'][10]['yesNoYes']	= 'No';
$_SESSION['listParams']['listFields'][10]['yesNoNo']	= 'Yes';

if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_USER) 
{
	$_SESSION['listParams']['listFields'][11]['field'] = "<i class='fa fa-2x ".SABLE_DATALIST_VIEW."' title='View booking on --!--'></i>";
	$_SESSION['listParams']['listFields'][11]['onClick'] = " onClick='viewBooking(\"--#--\")' ";
}
SableGeneralFunctions::callDataList();
