<?php
function getMenuActiveClass($dir)
{
	$res = ($_SESSION['current_dir']===$dir)?' class="pointer active" ':' class="pointer" ';
	return $res;
}
	
function buildMenuItem($title, $dir, $iconClass, $minAccess, $pluralSuffix='s')
{
	if (is_array($minAccess))
	{
		$minAdd 	= $minAccess[0];
		$minManage 	= $minAccess[1];
	}
	else
	{
		$minAdd 	= $minAccess;
		$minManage 	= $minAccess;
	}
	$last = substr($title, -1);
	$ttl = $title;
	if (strtolower($last)==='y') $ttl = substr($title, 0, -1);
	if (strtolower($last)==='s') $pluralSuffix = '';
	$plural = $ttl.$pluralSuffix;
	$html = '
	<!-- BEG: '.strtoupper($plural).' -->'.PHP_EOL.'
	<li '.getMenuActiveClass($dir).'>
		<a href="'.SableSystem::getSableLinkPath($dir.'/').'">'.PHP_EOL.'
			<i class="fa '.$iconClass.'"></i>
			<span class="nav-label">'.$plural.'</span> <span class="fa arrow"></span>
		</a>
		<ul class="nav nav-second-level collapse">'.
			addSubMenu('Add '.$title, SableSystem::getSableLinkPath($dir.'/add.php'), $minAdd).
			addSubMenu('Manage '.$plural, SableSystem::getSableLinkPath($dir.'/'), $minManage).'
		</ul>
	</li>'.PHP_EOL.'
	<!-- END: '.strtoupper($plural).' -->';
	echo($html);
}

function buildSubMenuItem($title, $dir, $iconClass, $minAccess, $pluralSuffix='s')
{
	if (is_array($minAccess))
	{
		$minAdd 	= $minAccess[0];
		$minManage 	= $minAccess[1];
	}
	else
	{
		$minAdd 	= $minAccess;
		$minManage 	= $minAccess;
	}
	
	$last = substr($title, -1);
	$ttl = $title;
	if (strtolower($last)==='y') $ttl = substr($title, 0, -1);
	if (strtolower($last)==='s') $pluralSuffix = '';
	$plural = $ttl.$pluralSuffix;
	
	$fls = explode('/', $dir);
	$dr = empty($fls) ? $dir : $fls[0];
	
	$i = 0;
	$flnm = '';
	foreach($fls as $fl)
	{
		if (strpos($fl, '.php')) 
		{
			$i++;
			break;
		} else $flnm .= '/'.$fl;
	}

	$fl = $i ? $fl : '';
	$flnm .= '/'.$fl;
	$flnm = str_replace('//', '/index.php', $flnm);

	$html = '
	<!-- BEG: '.strtoupper($plural).' -->'.PHP_EOL.'
	<li '.getMenuActiveClass($dir).'>
		<a href="'.SableSystem::getSableLinkPath($flnm).'">'.PHP_EOL.
			'<i class="fa '.$iconClass.'"></i>
			<label class="nav-label">'.$plural.'</label>
		</a>
	</li>'.PHP_EOL.'
	<!-- END: '.strtoupper($plural).' -->';
	echo($html);
}	

function addSubMenu($title, $url, $minAccess, $active=false)
{
	$html = '';
	if ($_SESSION['user']['access_level'] >= $minAccess)
	{
		$class = $active?' class="active" ':'';
		$fa = stripos($url, 'add.php')?'fa-plus':'fa-edit';
		$html = '<li'.$class.'><a href="'.$url.'"><i class="fa '.$fa.'"></i><label class="nav-label">'.$title.'</label></a></li>';
	}
	return $html;
}

function displayBox($size, $data)
{
	switch ($size)
	{
		case 1: $class = 'col-sm-12';	break;
		case 2: $class = 'col-sm-9';	break;
		case 3: $class = 'col-sm-6';	break;
		case 4: $class = 'col-sm-4';	break;
		case 5: $class = 'col-sm-3';	break;	
		default:$class = 'col-sm-2';	break;	
	}
	/*
	$data['heading']
	$data['link']
	$data['icon'] e.g "fa-building"
	$data['onclick']
	$data['items']
	$data['items'][0]['caption']
	$data['items'][0]['value']
	*/
	$rowStyleOn 	= "background-color:transparent;";
	$rowStyleOff 	= "background-color:#FFFFFF;";
	
	$onclick = isset($data['onclick']) ? 'onClick="'.$data['onclick'].'"' : '';
	
	//icons can now be stacked:
	if (is_array($data['icon']))
	{
		//we stack
		$faprefx1 = isset($data['icon-pre'][0]) ? $data['icon-pre'][0] : 'fa';
		$faprefx2 = isset($data['icon-pre'][1]) ? $data['icon-pre'][1] : 'fa';
		$icon = '<span class="fa-stack">
					  <i class="fas '.$data['icon'][0].' fa-stack-1x"></i>
					  <i class="fas '.$data['icon'][1].' fa-stack-1x fa-inverse"></i>
				  </span>';
	}
	else 
	{
		$faprefx = isset($data['icon-pre']) ? $data['icon-pre'] : 'fa';
		$icon = '<i class="'.$faprefx.' '.$data['icon'].'"></i>';
	}
	
	$html = '
	<div class="'.$class.'" '.$onclick.'>
		<div class="ibox float-e-margins">
			<div class="ibox-content text-center p-md">
				<a href="'.$data['link'].'">
					'.$icon.'
					<h4 class="m-b-xxs">'.$data['heading'].'</h4><br />
				</a>';
	if (isset($data['items']))
	{
		$flag = 0;
		$html .= '<div class="row">';
		foreach($data['items'] as $item)
		{
			$rowStyle = $flag ? $rowStyleOn : $rowStyleOff;
			//$rowStyle .= ' border-right: thick white solid;';

			$lnk = isset($item['link']) && !empty($item['link']) ? 
					'<a href="'.$item['link'].'">'.$item['caption'].'</a> :' : 
					'<h5 style="display:inline;">'.$item['caption'].'</h5><span>&nbsp;</span>';

			$html .= '	<div class="col-md-6" style="text-align:center; '.$rowStyle.'">';
			$html .= 		$lnk;
			$html .= '	</div>';
			$html .= '	<div class="col-md-6" style="text-align:center; '.$rowStyle.'">';
			$html .= '		<label style="display:inline">'.$item['value'].'</label></h6>';
			$html .= '	</div>';				
			$flag = !$flag;
		 }
		 $html .= '</div>';
	}
	$html .= '
			</div>
		</div>
	</div>';
	echo($html);
}

function displayInfoBox($size, $data)
{
	switch ($size)
	{
		case 1: $class = 'col-sm-12';	break;
		case 2: $class = 'col-sm-9';	break;
		case 3: $class = 'col-sm-6';	break;
		case 4: $class = 'col-sm-4';	break;
		case 5: $class = 'col-sm-3';	break;	
		default:$class = 'col-sm-2';	break;	
	}
	/*
	$data['heading']
	$data['link']
	$data['icon'] e.g "fa-building"
	$data['items']
	$data['items'][0]['caption']
	$data['items'][0]['value']
	$data['items'][0]['msg_type'] --> 	0 / none = info
										1 = warning
										2 = error
	*/
	$rowStyleOn 	= "background-color:#F2F2F2; height:28px; padding-top: 3px; position: absolute; bottom: 48px; left: 0;";
	$rowStyleOff 	= "background-color:#3cced6; height:28px;  padding-top: 3px; position: absolute; bottom: 48px; left: 0;";
	
	//icons can now be stacked:
	if (is_array($data['icon']))
	{
		//we stack
		$faprefx1 = isset($data['icon-pre'][0]) ? $data['icon-pre'][0] : 'fa';
		$faprefx2 = isset($data['icon-pre'][1]) ? $data['icon-pre'][1] : 'fa';
		$icon = '<span class="fa-stack fa-2x">
					  <i class="fas '.$data['icon'][0].' fa-stack-2x"></i>
					  <i class="fas '.$data['icon'][1].' fa-stack-1x"></i>
				  </span>';
	}
	else 
	{
		$faprefx = isset($data['icon-pre']) ? $data['icon-pre'] : 'fa';
		$icon = '<i class="'.$faprefx.' '.$data['icon'].'"></i>';
	};
	
	$html = '
	<div class="'.$class.'">
		<div class="ibox float-e-margins">
			<div class="ibox-content text-center p-md">
				<a href="'.$data['link'].'">
					'.$icon.'
					<h4 class="m-b-xxs">'.$data['heading'].'</h4><br />
				</a>';
	if (isset($data['items']))
	{
		$flag = 0;
		$html .= '<div class="row">';
		foreach($data['items'] as $item)
		{
			$rowStyle = $flag ? $rowStyleOn : $rowStyleOff;
			
			switch($item['msg_type'])
			{
				case 0:	{$i = 'fa-info-circle';					$c = 'lightblue'; 	break;}
				case 1:	{$i = 'fa-exclamation-triangle';		$c = 'orange'; 		break;}
				case 2:	{$i = 'fa-exclamation-circle blink';	$c = 'red'; 		break;}
				default:{$i = 'fa-info-circle';					$c = 'lightblue'; 	break;}
			}
			
			$html .= '	<div class="col-sm-12" style="height:18px;"></div>';			
			
			$html .= '	<div class="col-sm-12" style="text-align:center; '.$rowStyle.'">';
			$html .= '		<i class="fa '.$i.'" style="color:'.$c.'"></i>&nbsp;'.$item['caption'];
			$html .= '	</div>';			
			$flag = !$flag;
		 }
		 $html .= '</div>';
	}
	$html .= '
			</div>
		</div>
	</div>';
	echo($html);
}

function addBoxDataItem($caption, $value, &$data)
{
	if (!isset($data['items'])) $data['items'] = [];
	$temp = [];
	$temp['caption'] 	= $caption;
	$temp['value'] 		= $value;
	$data['items'][] 	= $temp;
	return $data;
}

function addBoxDataHeader($heading, $path, $icon)
{
	$data = [];
//		if (!isset($data['items'])) unset($data['items']);
	$data['heading'] 	= strtoupper($heading);
	$data['link'] 		= SableSystem::getSableLinkPath($path.'/');
	$data['icon']		= $icon;
	return $data;
}

function addReportBox($reports)
{
	$html = '
		<div class="col-sm-4 col-md-4 col-lg-6">
			<div class="ibox float-e-margins">
				<div class="ibox-content text-center p-md"> <i class="fa fa-file-alt"></i>
					<h4 class="m-b-xxs"><a href="'.SableSystem::getSableLinkPath('reporting/index.php').'">REPORTS</a></h4>
					<br>';
	foreach($reports as $rpt)
	{
		if (empty($rpt))
			$html .= '<br>';
		else
			$html .= '
						<div class="row">
							<div class="col-md-12" style="text-align:left; background-color:#FFFFFF;"><a href="'.SableSystem::getSableLinkPath('reporting/'.$rpt['file']).'" title="'.$rpt['name'].'">'.$rpt['name'].'</a></div>
						</div>';
		}
		$html .= '
					</div>
				</div>
			</div>';
	echo($html);
}

function addShortcutBox($data)
{
	foreach($data as $itm)
	{
	$html .= '
		<div class="col-sm-4 col-md-3 col-lg-2">
			<div class="ibox float-e-margins">
				<div class="ibox-content text-center p-md">';		
		$html .= '  <a href="'.SableSystem::getSableLinkPath($itm['module']).'">
						<span><i class="fa '.$itm['icon'].'"></i><span>
						<div class="text-center">'.strtoupper($itm['caption']).'</div>
					</a>';
	$html .= '
				</div>
			</div>
		</div>';					
	}

	echo($html);	
}

$itm 			= 	SABLE_URL_ROOT.
					'index.php?user/edit.php+'.SableSystem::sableEncode($_SESSION['user']['user_code'].'+'.session_id());
$onclick 		= "javascript: window.location = '".$itm."';";
$usrImage 		= empty($_SESSION['user']['user_image'])?SABLE_UNKNOWN_IMAGE:$_SESSION['user']['user_image'];

$coyEditLink 	= SableSystem::getParameterLink('company', 'edit.php', $_SESSION['user']['company_id']);
$coyViewLink 	= SableSystem::getParameterLink('company', 'view.php', $_SESSION['user']['company_id']);
$usrEditLink 	= SableSystem::getParameterLink('user', 'edit.php',    $_SESSION['user']['user_code']);

$clientListLink 	= SableSystem::getSableLinkPath('client/');//clients
$buildingListLink 		= SableSystem::getSableLinkPath('building/');//buildings
$serviceListLink 	= SableSystem::getSableLinkPath('service/');//services
$bookingListLink 	= SableSystem::getSableLinkPath('booking/list.php');//bookings
$bookingAddLink 	= SableSystem::getSableLinkPath('booking/add.php');//bookings
$planningLink 		= SableSystem::getSableLinkPath('booking/');//bookings
$appLink			= SableSystem::getThemeLinkPath('dashboard/dashboard_user.php');//bookings
