<?php
require_once(__DIR__.'/../includes/sysinit.php');
require(SABLE_WORKING_PATH.'building/building_func.php');
//**********************************************************************************

$clientId = 0;

if (sizeof($_SESSION['caller']['args']) > 2)
{
	$clientId = intval($_SESSION['caller']['args'][1]);
	$_SESSION['caller']['args'][1] = $_SESSION['caller']['args'][2];
	unset($_SESSION['caller']['args'][2]);
	
	$_SESSION['module']['back'] = 1;
	$_SESSION['module']['backlink'] = SableSystem::getCallPageLink('/client/');

	$_SESSION['module']['add'] = 1;
	$_SESSION['module']['addlink'] = SableSystem::getParameterLink('building', 'add.php', $clientId);
}


$_SESSION['listParams']['searchVisible'] = 1;
if (isset($_POST))
{
	$_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] = 'area_type_name';
	$_SESSION['listParams']['searchField'][2] = 'area_name';
	$_SESSION['listParams']['searchField'][3] = 'building_name';
	$_SESSION['listParams']['searchField'][4] = 'building_notes';
}

$_SESSION['listParams']['listSql'] = "select building_id, concat(client_name, ' ', client_surname) as client_full_name, 
									COALESCE(area_type_name, 'Unknown') as area_type_name, 
									COALESCE(area_name, 'Unknown') as area_name, building_name, building_notes
									  from building
									  inner join client on client.client_id = building.client_id
									  left join area on area.area_id = building.area_id 
									  left join area_type on area_type.area_id = area.area_id ";
if ($clientId) $_SESSION['listParams']['listSql'] .= "where client.client_id = ".$clientId;

$_SESSION['listParams']['module']					= "building";
$_SESSION['listParams']['page']						= 0;
$_SESSION['listParams']['offset']					= 50; //the number of records to display per page
$_SESSION['listParams']['parentId']					= ""; //
$_SESSION['listParams']['moduleFolder']				= "building";
$_SESSION['listParams']['moduleFolderDesc']			= "Building";
$_SESSION['listParams']['modulePage']				= "index.php";
$_SESSION['listParams']['moduleIdField']			= "building_id";
$_SESSION['listParams']['moduleDescField']			= "building_name";
$_SESSION['listParams']['orderField']				= 1;
$_SESSION['listParams']['orderDirection']			= 0;

$_SESSION['listParams']['listHeadings'][0] = "Client";
$_SESSION['listParams']['listHeadings'][1] = "Area";
$_SESSION['listParams']['listHeadings'][2] = "Area";
$_SESSION['listParams']['listHeadings'][3] = "Name";
$_SESSION['listParams']['listHeadings'][4] = "Birthday";
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_USER){	$_SESSION['listParams']['listHeadings'][5] = "";} //Add
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_CLERK){	$_SESSION['listParams']['listHeadings'][6] = "";} //Edit
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_CLERK){	$_SESSION['listParams']['listHeadings'][7] = "";} //Client Info
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_ADMIN){	$_SESSION['listParams']['listHeadings'][8] = "";} //Delete

$_SESSION['listParams']['listHeadingWidth'][0] = "15%";
$_SESSION['listParams']['listHeadingWidth'][1] = "15%";
$_SESSION['listParams']['listHeadingWidth'][2] = "12%";
$_SESSION['listParams']['listHeadingWidth'][3] = "12%";
$_SESSION['listParams']['listHeadingWidth'][4] = "21%";
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_USER){	$_SESSION['listParams']['listHeadingWidth'][5] = "5%";} //View
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_CLERK){	$_SESSION['listParams']['listHeadingWidth'][6] = "5%";} //Edit
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_CLERK){	$_SESSION['listParams']['listHeadingWidth'][7] = "5%";} //Client Info
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_ADMIN){	$_SESSION['listParams']['listHeadingWidth'][8] = "95%";} //Delete

$_SESSION['listParams']['listFields'][0]['field']			= "client_full_name";
$_SESSION['listParams']['listFields'][1]['field']			= "area_name";
$_SESSION['listParams']['listFields'][2]['field']			= "area_type_name";
$_SESSION['listParams']['listFields'][3]['field']			= "building_name";
$_SESSION['listParams']['listFields'][4]['field']			= "building_notes";
$_SESSION['listParams']['listFields'][4]['date']			= "1";
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_USER) 
{
	$_SESSION['listParams']['listFields'][5]['field'] = "<i class='fa fa-2x ".SABLE_DATALIST_VIEW."' title='View --!--'></i>";
	$_SESSION['listParams']['listFields'][5]['onClick'] = " onClick='viewBuilding(\"--#--\")' ";
}
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_CLERK) 
{
	$_SESSION['listParams']['listFields'][6]['field'] = "<i class='fa fa-2x ".SABLE_DATALIST_EDIT."' title='Modify --!--'></i>";
	$_SESSION['listParams']['listFields'][6]['onClick'] = " onClick='editBuilding(\"--#--\")' ";
	
	$_SESSION['listParams']['listFields'][7]['field'] = "<i class='fa fa-2x fa-address-card' title='Owner of --!--'></i>";
	$_SESSION['listParams']['listFields'][7]['onClick'] = " onClick='viewClient(\"--#--\")' ";	
}
if ($_SESSION['user']['access_level'] >= SABLE_ACCESS_ADMIN) 
{
	$_SESSION['listParams']['listFields'][8]['field'] = "<i class='fa fa-2x ".SABLE_DATALIST_DELETE."' title='Delete --!--'></i>";
	$_SESSION['listParams']['listFields'][8]['onClick'] = " onClick='deleteBuilding(\"--#--\",\"--!--\")' ";
}
SableGeneralFunctions::callDataList();
$_SESSION['module']['back'] = 0; //reset
