<?php
	
	if (!empty($_REQUEST['parentClient']))
		$pclientID = $_REQUEST['parentClient'];
	else
		$pclientID =  '';	 				
	$pClients = getPclientOptions($pclientID,$con);
	
	$filters .= '<p><form action="" method="post" style="text-align: right"><select name="parentClient" onchange="this.form.submit();"><option value="">All Parent Clients</option>'. $pClients .'</select></form></p>';
	
	include('pages/inc/page-header.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.projectType=1 AND p.projectStatus=1"; 
	if (!empty($pclientID))
		$query .= ' AND c.subClientOf='. $pclientID; 
	$query .= ' ORDER BY c.clientName ASC, p.projectTargetComp DESC';
	
	$result = $con->query($query);
	if ($result) {
		$projectsActive = ''; 
		$projectsInactive = '';
		$tableHeader = '<table class="reportResults table table-condensed table-striped" cellspacing="0">
						<thead>
							<tr>
								<th>Client</th>
								<th>Project</th>
								<th>Project Amount</th>
								<th>Amount Invoiced</th>
								<th>Amount Paid</th>
								<th>&nbsp;</th>
							</tr>
						</thead>';
		
		while ($row=$result->fetch_array(MYSQLI_ASSOC)) {
		
			// Company Name or Abbreviation
			$client = $row['clientName'];
			if (!empty($row['clientAbbr'])) 
				$client .= ' ('. $row['clientAbbr'] . ')';
			
			if (empty($amtPaid))
				$amtPaid = "0.00";
			if (empty($amtInvoiced))
				$amtInvoiced = "0.00";
			
			$tableName = (($row['projectDisp'] == 1) ? 'projectsActive' : 'projectsInactive');
			
			// Get Payment History For Project
			$pmtQuery = 'SELECT paymentAmount, paymentStatus FROM payments WHERE projectID=' . $row['projectID'] .''; 
			$pmtResult = $con->query($pmtQuery);
			$amtPaid = '';
			$amtInvoiced = ''; 
			while ($pmt=$pmtResult->fetch_array(MYSQLI_ASSOC)) {
				$amtInvoiced = $amtInvoiced + $pmt['paymentAmount'];
				if ($pmt['paymentStatus'] == 1)
					$amtPaid = $amtPaid + $pmt['paymentAmount'];
			}
			if (empty($amtPaid))
				$amtPaid = "0.00";
			if (empty($amtInvoiced))
				$amtInvoiced = "0.00";

			$$tableName .= '<tr id="record-'. $row['projectID'] .'">
							<td>' .$client . '</td>
							<td>' . $row['projectName'] . '</td>
							<td>&#36;' . number_format($row['projectAmount'],2,'.','') . '</td>
							<td>&#36;' . number_format($amtInvoiced, 2,'.','') . '</td>
							<td>&#36;'. number_format($amtPaid, 2,'.','') .'</td>
							<td class="adminActions"><a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="project-payment" data-adminact="" data-editid="' . $row['projectID'] . '" title="Payments"><i class="fa fa-money"></i></a></td>
						</tr>';
		}
		
		$activeProjects = '<h3>Active Contract Projects</h3>';
		if (!empty($projectsActive)) 
			$activeProjects .= $tableHeader . '<tbody id="'. $tableName .'">'. $projectsActive . '</tbody></table>';
		else
			$activeProjects .= '<p class="center">There are currently no Active Contract Projects Listed</p>';
		
		$content .= '<div class="row"><div class="col-xs-12"><div class="table-responsive">'. $activeProjects .'</div></div></div>';
	}

?>