<?php

	$adminBtns .= '<a href="#" class="admin btn btn-default" data-toggle="modal" data-target="#adminModal" data-admintype="'. $_REQUEST['type'] .'" data-adminact="new" data-editid=""><i class="fa fa-plus-square"></i> Add a NEW '. ucfirst($_REQUEST['type']) .'</a>';

	include('pages/inc/page-header.php');
	
	$parentClients = getPclientArray($con);
					
	if (!empty($_REQUEST['parentClient']))
		$pclientID = $_REQUEST['parentClient'];
	else
		$pclientID =  '';	 				
	$pClients = getPclientOptions($pclientID,$con);				
	
	$query = 'SELECT si.*, c.clientName, c.clientAbbr, c.clientID, c.subClientOf, so.*, st.*, sp.* FROM serviceInfo si LEFT JOIN clients c ON (c.clientID = si.clientID) LEFT JOIN serviceOwner so ON (si.svcOwner = so.ownerID) LEFT JOIN serviceProviders sp ON (si.svcProvider = sp.spID) LEFT JOIN serviceTypes st ON (si.svcType = st.serviceID) WHERE si.display=1 ORDER BY c.subClientOf, c.clientName';
	$result = $con->query($query);
	
	if ($result) {
		
		// Define Tables
		foreach ($parentClients as $key => $value) {
			$tableName = cleanClientName($value);
			$$tableName = ''; 
		} 
		$other = ''; 
		$clientsInactive = '';

		$tableFieldNames = '<th class="col-sm-2">Client Name</th>
							<th class="col-sm-2">Service Desc</th>
							<th class="col-sm-2">Website</th>
							<th class="col-sm-2">Provider</th>
							<th class="col-sm-1">Start Date</th>
							<th class="col-sm-1">End/Renew Date</th>
							<th class="col-sm-1">Owner</th>
							<th class="col-sm-1">&nbsp;</th>'; 
		
		include('inc/data-tables/table-top.php');
		include('inc/data-tables/table-bottom.php');
							
		$sortColumns = 8;
		$noSortColumns = 7; // Column Number from left (column 1=0)
		$scrollHeight = '500px';
		$tabTable = true;
		 
		while ($row=$result->fetch_array(MYSQLI_ASSOC)) {
			$tableName = ''; 
			foreach ($parentClients as $key => $value) {
				if (($row['subClientOf'] == $key) || ($row['clientID'] == $key)) 
					$tableName = cleanClientName($value);
			}
			if(empty($tableName))
				$tableName = 'other';
				
			// Format Website
			if (!empty($row['domainName']))
				$website = '<a href="http://' . $row['domainName'] . '" target="_blank">' . $row['domainName'] . '</a>';
			else
				$website = '';
				
			// Format Service Provider
			if (!empty($row['spName'])) {
				$spLinkStart = ''; 
				$spLinkEnd = ''; 
				if (!empty($row['spWebsite'])) {
					$spLinkStart = '<a href="http://'. $row['spWebsite'] .'" target="_blank">'; 
					$spLinkEnd = '</a>'; 
				}
				$serviceProvider = $spLinkStart . $row['spName'] . $spLinkEnd;
			}
			else
				$serviceProvider = '';	
			
			// Format Dates
			if ( (!empty($row['svcBeginDate'])) || ($row['svcBeginDate'] == '0000-00-00') ) 
				$startDate = ''; 
			else 
				$startDate = Date::convert($row['svcBeginDate'], 'Y-m-d', 'm/d/Y'); 
			if ( (empty($row['svcExpDate'])) || ($row['svcExpDate'] == '0000-00-00') ) 
				$endDate = '';
			else 
				$endDate = Date::convert($row['svcExpDate'], 'Y-m-d', 'm/d/Y');
			
			// Client Abbreviation
			if (!empty($row['clientAbbr']))
				$abbr = ' ('. $row['clientAbbr'] .')';
			else
				$abbr = '';
				
			// Admin Links			
			$adminLinks = ''; 
			
			$adminLinks .= ' <a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="'. $_REQUEST['type'] .'" data-adminact="edit" data-page="'. $_REQUEST['type'] .'" data-section="'. $tableName .'" data-editid="' . $row['svcID'] . '" title="Edit '. ucfirst($_REQUEST['type']) .'"><i class="fa fa-pencil-square-o"></i></a> ';
			
			$adminLinks .= ' <a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="'. $_REQUEST['type'] .'" data-adminact="delete" data-page="'. $_REQUEST['type'] .'" data-section="'. $tableName .'" data-editid="' . $row['svcID'] . '" title="Delete '. ucfirst($_REQUEST['type']) .'"><i class="fa fa-trash"></i></a>';
			
			$$tableName .= '<tr id="record-'. $row['svcID'] .'">
							<td>' . $row['clientName'] . $abbr . '</td>
							<td><a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="'. $_REQUEST['type'] .'" data-adminact="edit" data-page="'. $_REQUEST['type'] .'" data-section="'. $tableName .'" data-editid="' . $row['svcID'] . '" title="Edit '. ucfirst($_REQUEST['type']) .'">' . $row['serviceName'] .'</a></td>
							<td>'. $website .'</td>
							<td>'. $serviceProvider .'</td>
							<td>'. $startDate .'</td>
							<td>'. $endDate .'</td>
							<td>' . $row['ownerDesc'] . '</td>
							<td class="adminActions">'. $adminLinks .'</td>
						</tr>';
		}
		
		// Assemble Client Tables and Tabs
		$tables = ''; 
		$reporttabs = '<ul>';
		foreach ($parentClients as $key => $value) {
			$reporttabs .= '<li><a href="#tabs-'. $key .'">'. $value .'</a></li>';
			$tableName = cleanClientName($value);
			if (!empty($tableName))  
				$tables .= '<div id="tabs-'. $key .'">'. $tableHeader . '<tbody id="'. $tableName .'">' . $$tableName . '</tbody>'. $tableFooter .'</table></div>';
			else 
				$tables .= '<div id="tabs-'. $key .'"><p class="center">There are currently no Active Clients Listed</p></div>';
		}
		$reporttabs .= '</ul>';
		
		$content .= '<div id="tabs">' . $reporttabs . $tables . '</div>';
	}
	include('inc/data-tables/dataTables.php');
?>