<?php

	$projPage = 'time'; 
	$parentClients = getPclientArray($con);

	//$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-user-plus"></i> Add a NEW '. ucfirst($_REQUEST['type']) .'</a>';
	
	include('pages/inc/page-header.php');
	include('admin/pieces-parts/projectFilters.php'); 

	$query = "SELECT * FROM projects AS p LEFT JOIN clients AS c ON (p.clientID = c.clientID) LEFT JOIN projectStatus AS ps on (p.projectStatus = ps.pstatID) LEFT JOIN projectType AS pt ON (p.projectType = pt.ptypeID) WHERE p.projectDisp=1"; 
	if (!empty($statID))
		$query .= ' AND p.projectStatus='. $statID; 
	if (!empty($pclientID))
		$query .= ' AND c.subClientOf='. $pclientID; 
	if (!empty($pCurrentStat))
		$query .= ' AND p.projectCurrent='. $pCurrentStat;
	$query .= ' ORDER BY c.clientName ASC, p.projectTargetComp DESC';
	$result = $con->query($query);
	if ($result) {
		
		// Define Tables
		foreach ($parentClients as $key => $value) {
			$tableName = cleanClientName($value);
			$$tableName = ''; 
		} 
		
		$projectsActive = ''; 
		$projectsInactive = '';
		
		$tableFieldNames = '<th style="col-sm-4">Client</th>
							<th style="col-sm-3">Project</th>
							<th style="col-sm-1">Type</th>
							<th style="col-sm-1">Estimate</th>
							<th style="col-sm-1">Time to Date</th>
							<th style="col-sm-1">Billable</th>
							<th style="col-sm-1">&nbsp;</th>';
		
		include('inc/data-tables/table-top.php');
		include('inc/data-tables/table-bottom.php');
		
		$sortColumns = 5;
		$noSortColumns = 6; // Column Number from left (column 1=0)
		$scrollHeight = 500;
		$tabTable = true;
		
		while ($row=$result->fetch_array(MYSQLI_ASSOC)) {
			// Company Name or Abbreviation
			$client = $row['clientName'];
			if (!empty($row['clientAbbr'])) 
				$client .= ' ('. $row['clientAbbr'] . ')';
			
			
			// Get Hours History For Project
			$hrsBillable = getHours($row['projectID'],'billable',$con);
			$hrsTotal = getHours($row['projectID'],'',$con);
			
			if (empty($amtPaid))
				$amtPaid = "0.00";
			if (empty($amtInvoiced))
				$amtInvoiced = "0.00";
			
			$adminLinks = ''; 
			if ($row['projectDisp'] == 1) {
				
				foreach ($parentClients as $key => $value) {
					if (($row['subClientOf'] == $key) || ($row['clientID'] == $key)) 
						$tableName = cleanClientName($value);
				}
				if(empty($tableName))
					$tableName = 'other';
			} else { 
				$tableName = "projectsInactive";
			}
			
			$adminLinks .= ' <a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="project-time" data-adminact="" data-editid="' . $row['projectID'] . '" title="Hours"><i class="fa fa-clock-o"></i></a> ';
			
			$estimate = (($row['projectTimeEstimate'] > 0) ? number_format($row['projectTimeEstimate'],2,'.','') .' hours' : 'N/A'); 

			$ttdateBillable = (($hrsBillable['timeTotal'] > 0) ? number_format($hrsBillable['timeTotal'],2,'.','') .' hours' : 'N/A');
			$ttdateTotal = (($hrsTotal['timeTotal'] > 0) ? number_format($hrsTotal['timeTotal'],2,'.','') .' hours' : 'N/A');	
			
			$timeStyle = ((($row['projectTimeEstimate']>0) && ($hrsBillable['timeTotal']>$row['projectTimeEstimate'])) ? ' style="color: red; font-weight: bold"' : '');
			
			$$tableName .= '<tr id="record-'. $row['projectID'] .'">
							<td>' .$client . '</td>
							<td>' . $row['projectName'] . '</td>
							<td>'. $row['ptypeDesc'] .'</td>
							<td>' . $estimate . '</td>
							<td>'. $ttdateTotal .'</td>
							<td'. $timeStyle .'>' . $ttdateBillable . '</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');

?>