<?php

	$projType = $_REQUEST['ptype']; // Get Project Type
	if ($projType == 1)
		$pTypeTitle = 'Contract';
	else
		$pTypeTitle = 'Hourly';

	if (!empty($_REQUEST['projects'])) {
		$pageTitle = $pTypeTitle .' Project Time Report'; 
		$pageSubTitle .= '<p class="reportFilters">';
		$repFilter = ' WHERE timeDisp=1'; 
		
		if (is_array($_REQUEST['projects']))
			$projects = implode(',',$_REQUEST['projects']);
		else
			$projects = $_REQUEST['projects'];
		
		if ($projects == "all") 
			$pageSubTitle .= 'All Projects: ';
		else  
			$repFilter .= ' AND projectID IN ('. $projects .')';
		
		// Get Date Range
		if ((empty($_REQUEST['dateFrom']))&&(empty($_REQUEST['dateTo'])))
			$pageSubTitle .= ' All Time: ';
		else {
			if (!empty($_REQUEST['dateFrom'])) {
				$pageSubTitle .= ' From '. Date::convert($_REQUEST['dateFrom'], 'Y-m-d', 'M d, Y');
				$repFilter .= ' AND  timeDate>="'. $_REQUEST['dateFrom'] .'"';
			}
			if (!empty($_REQUEST['dateTo'])) {
				$pageSubTitle .= ' To '. Date::convert($_REQUEST['dateTo'], 'Y-m-d', 'M d, Y');
				$repFilter .= ' AND timeDate<="'. $_REQUEST['dateTo'] .'"';	
			}
			$pageSubTitle .= ': ';
		}
		
		include('pages/inc/page-header.php');
	
		$query = 'SELECT * FROM projectTime ' . $repFilter .' ORDER BY timeDate DESC, timeID DESC'; 
		$result = $con->query($query);
		$hoursBillable = ''; 
		$hoursTotal = ''; 
		
		$tableInfo = '';

		if ($result->num_rows>0) {
			$linkVars = ''; 
			foreach($_REQUEST as $name => $value) {
				if ($name == 'orderBy') {}
				elseif ($name == 'projects') {
					if (is_array($_REQUEST['projects']))
						$linkVars .= '&'. $name .'='. implode(',',$_REQUEST['projects']);
					else
						$linkVars .= '&'. $name .'='. $value;
				}
				else
					$linkVars .= '&'. $name .'='. $value;
			}
			$tableFieldNames = '<th>Date</th>
								<th>Time</th>
								<th>Description</th>
								<th>Billable</th>';
			
			include('inc/data-tables/table-top.php');
			include('inc/data-tables/table-bottom.php');
			
			$sortColumns = 4;
			$noSortColumns = ''; // Column Number from left (column 1=0)
			$scrollHeight = '500px';
			$tabTable = true;
			
			while ($row=$result->fetch_array(MYSQLI_ASSOC)) {
				if ($row['timeInvoiced'] == 1)
					$invCheck = ' checked';
				else 
					$invCheck = '';
					
				if ($row['billable'] == 1)
					$billCheck = 'Yes';
				else {
					if ($projType == 1)
						$billCheck = 'Contract';
					else
						$billCheck = 'No';
				}
				
				$tableInfo .= '<tr>
									<td style="white-space: nowrap">'. $row['timeDate'] .'</td>
									<td>'. $row['timeAmount'] .'</td>
									<td>'. $row['timeDesc'] .'</td>
									<td style="text-align: center">'. $billCheck .'</td>
								</tr>';	
				
				$hoursTotal = $hoursTotal + $row['timeAmount'];
				if ($row['billable'] == 1)
					$hoursBillable = $hoursBillable + $row['timeAmount'];

			}
			$content .= '<div class="row"><div class="col-sm-12">';
			$content .= $tableHeader . '<tbody>' . $tableInfo . '</tbody>'. $tableFooter;
			
			$content .= '<p><strong>Total Time: </strong>'. $hoursTotal .' hours</p>'; 
			$content .= '<p><strong>Total Billable: </strong>'. $hoursBillable .' hours</p>';
			
			$content .= '</div></div>';
			include('inc/data-tables/dataTables.php');
		}
	} else {
		$projID = '';
		// Contract Projects
		$projects = getProjects($projID,$projType,$con);
	
		$pageTitle = $pTypeTitle .' Project Billing Report';
		include('pages/inc/page-header.php');
		$content .= '<form name="generateReport" action="' . $phpself . '" method="post">
						<input type="hidden" name="op" value="' . $_REQUEST['op'] . '" />
						<input type="hidden" name="type" value="' . $_REQUEST['type'] . '" />
						<input type="hidden" name="ptype" value="' . $_REQUEST['ptype'] . '" />
						<table class="reportSelect">
							<tr>
								<th>Select Project(s):</th>
								<td colspan="2">'. $projects .'</td>
							</tr>
							<tr>
								<th>Date Range:</th>
								<td>
									<label>From Date</label><br>
									<input type="text" name="dateFrom" id="dateFrom" readonly="readonly" class="dateField datepicker" />
								</td>
								<td>
									<label>To Date</label><br>
									<input type="text" name="dateTo" id="dateTo" readonly="readonly" class="dateField datepicker" />
								</td>
							</tr>
							<tr>
								<th>Invoice Status:</th>
								<td colspan="2">
									<select name="invoiceStatus">
										<option value="all">All</option>
										<option value="0">Invoiced</option>
										<option value="1">Paid</option>
									</select>
								</td>
							</tr>
						</table>
						<input type="submit" name="Submit" value="Generate Report" />
					</form>';
	}
?>