<?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 * FROM expenses exp LEFT JOIN clients c ON (exp.clientID = c.clientID) LEFT JOIN projects p ON (exp.projectID = p.projectID) ORDER BY c.clientName ASC, p.projectName";
	$result = $con->query($query);
	
	if ($result) {
		
		// Define Tables
		foreach ($parentClients as $key => $value) {
			$tableName = cleanClientName($value);
			$$tableName = ''; 
		} 
		
		$expensesActive = ''; 
		$expensesInactive = '';
		
		$tableFieldNames = '<th class="col-sm-3">Client</th>
							<th class="col-sm-3">Project</th>
							<th class="col-sm-2">Expense</th>
							<th class="col-sm-1">Date</th>
							<th class="col-sm-1">Amount</th>
							<th class="col-sm-1">Reimbursable</th>
							<th class="col-sm-1">&nbsp;</th>'; 
		
		include('inc/data-tables/table-top.php');
		include('inc/data-tables/table-bottom.php');
		
		$sortColumns = 7;
		$noSortColumns = 6; // Column Number from left (column 1=0)
		$scrollHeight = '500px';
		$tabTable = true;
		
		while ($row=$result->fetch_array(MYSQLI_ASSOC)) {
			
			$adminLinks = ''; 
			
			// Company Name or Abbreviation
			$client = $row['clientName'];
			if (!empty($row['clientAbbr'])) 
				$client .= ' ('. $row['clientAbbr'] . ')';

			// Expense Date
			$expDate = (($row['expDate'] == "0000-00-00") ? 'N/A' : Date::convert($row['expDate'], 'Y-m-d', 'm/d/Y'));
			
			// Reimbursable
			$expReimb = (($row['expReimbursable'] == 1) ? 'Yes' : 'No');
				
			if ($row['expDisp'] == 1) {
				
				foreach ($parentClients as $key => $value) {
					if (($row['subClientOf'] == $key) || ($row['clientID'] == $key)) 
						$tableName = cleanClientName($value);
				}
				if(empty($tableName))
					$tableName = 'other';
				$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['expID'] . '" title="Delete '. ucfirst($_REQUEST['type']) .'"><i class="fa fa-trash"></i></a> '; 
			}
			else { 
				$tableName = "expensesInactive";
				$adminLinks .= ' <a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="'. $_REQUEST['type'] .'" data-adminact="react" data-page="'. $_REQUEST['type'] .'" data-section="'. $tableName .'" data-editid="' . $row['expID'] . '" title="Reactivate '. ucfirst($_REQUEST['type']) .'"><i class="fa fa-refresh"></i></a> ';
			}
		
			$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['expID'] . '" title="Edit '. ucfirst($_REQUEST['type']) .'"><i class="fa fa-pencil-square-o"></i></a> '. $adminLinks;
			
			$$tableName .= '<tr id="record-'. $row['expID'] .'">
							<td>' . $client . '</td>
							<td>' . $row['projectName'] . '</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['expID'] . '">' . $row['expName'] . '</a></td>
							<td>' . $expDate . '</td>
							<td style="text-align: right">&#36;' . number_format($row['expAmount'],2,'.','') . '</td>
							<td style="text-align: center">'. $expReimb .'</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');
?>