<?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');
	
	$query = "SELECT e.*, es.*, p.projectName FROM estimates e LEFT JOIN estimateStatus es ON (e.estStatus = es.estStatID) LEFT JOIN projects p ON (e.projectID = p.projectID)"; 
	if (!empty($estStat))
		$query .= ' WHERE p.estStatus='. $statID; 
	$query .= ' ORDER BY e.estDisplay, e.estStatus, e.estDate DESC';
	
	$result = $con->query($query);
	if ($result) {
		$estimatesWorking = ''; 
		$estimatesSubmitted = '';
		$estimatesAccepted = ''; 
		$estimatesDeclined = ''; 
		$estimatesOnhold = '';
		$estimatesDead = '';
		$estimatesDeleted = ''; 
		
		$tableFieldNames = '<th>Client</th>
							<th>Project</th>
							<th>Estimate Name</th>
							<th>Estimate Date</th>
							<th>Estimate Time</th>
							<th>Estimate Cost</th>
							<th>Estimate Status</th>
							<th></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 = 200;
		$tabTable = true;
		
		while ($row=$result->fetch_array(MYSQLI_ASSOC)) {
			
			$adminLinks = ''; 
			
			// Client Name or Abbreviation
			if (!empty($row['clientID']))
				$client = getClientName($row['clientID'],$con);
			else
				$client = 'N/A'; 
			
			// target Completion Date
			if ($row['estDate'] == "0000-00-00")
				$estDate = "N/A";
			else
				$estDate = Date::convert($row['estDate'], 'Y-m-d', 'm/d/Y');
			
			if ($row['estDisplay'] == 1) {
				if ($row['estStatus'] == 1)
					$tableName = 'estimatesWorking';
				elseif ($row['estStatus'] == 2)
					$tableName = 'estimatesSubmitted';
				elseif ($row['estStatus'] == 3)
					$tableName = 'estimatesAccepted';
				elseif ($row['estStatus'] == 4)
					$tableName = 'estimatesDeclined';
				elseif ($row['estStatus'] == 5)
					$tableName = 'estimatesOnhold'; 
				elseif ($row['estStatus'] == 6)
					$tableName = 'estimatesDead'; 
				$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['estID'] . '" title="Delete '. ucfirst($_REQUEST['type']) .'"><i class="fa fa-trash"></i></a> '; 
			} else {  
				$tableName = 'estimatesDeleted';
				$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['estID'] . '" 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['estID'] . '" title="Edit '. ucfirst($_REQUEST['type']) .'"><i class="fa fa-pencil-square-o"></i></a> '. $adminLinks;
			
			// Project Name
			if (!empty($row['projectID'])) 
				$project = '<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['projectID'] . '" title="Edit '. ucfirst($_REQUEST['type']) .'">' . $row['projectName'] . '</a>';
			else
				$project = 'N/A';
			
			
			$$tableName .= '<tr id="record-'. $row['estID'] .'">
							<td>' .$client . '</td>
							<td>'. $project . '</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['estID'] . '" title="Edit '. ucfirst($_REQUEST['type']) .'">' . $row['estName'] . '</a></td>
							<td>' . $estDate . '</td>
							<td>' . number_format($row['estTotalHours'],2,'.','') . '</td>
							<td>$' . number_format($row['estTotalCost'],2,'.','') . '</td>
							<td>'. $row['estStatDesc'] .'</td>
							<td class="adminActions">'. $adminLinks .'</td>
						</tr>';
			if (!empty($odd))
				$odd = '';
			else  
				$odd = true;
		}
		
		// Working Estimates
		$workingEstimates = ''; 
		if (!empty($estimatesWorking)) 
			$workingEstimates .= '<div id="tabs-1">' . $tableHeader . '<tbody>'. $estimatesWorking . '</tbody>'. $tableFooter .'</table></div>';
		else
			$workingEstimates .= '<div id="tabs-1"><p class="center">There are currently no Working Estimates Listed</p></div>';
		
		// Submitted Estimates
		$submittedEstimates = '';
		if (!empty($estimatesSubmitted)) 
			$submittedEstimates .= '<div id="tabs-2">' . $tableHeader . '<tbody>'. $estimatesSubmitted . '</tbody>'. $tableFooter .'</table></div>';
		else
			$submittedEstimates .= '<div id="tabs-2"><p class="center">There are currently no Submitted Estimates Listed</p></div>';
		
		// Accepted Estimates	
		$acceptedEstimates = '';
		if (!empty($estimatesAccepted)) 
			$acceptedEstimates .= '<div id="tabs-3">' . $tableHeader . '<tbody>'. $estimatesAccepted . '</tbody>'. $tableFooter .'</table></div>';
		else
			$acceptedEstimates .= '<div id="tabs-3"><p class="center">There are currently no Accepted Estimates Listed</p></div>';
		
		// On-Hold Estimates	
		$onholdEstimates = '';
		if (!empty($estimatesOnhold)) 
			$onholdEstimates .= '<div id="tabs-4">' . $tableHeader . '<tbody>'. $estimatesOnhold . '</tbody>'. $tableFooter .'</table></div>';
		else
			$onholdEstimates .= '<div id="tabs-4"><p class="center">There are currently no On-Hold Estimates Listed</p></div>';
		
		// Declined Estimates	
		$declinedEstimates = '';
		if (!empty($estimatesDeclined)) 
			$declinedEstimates .= '<div id="tabs-5">' . $tableHeader . '<tbody>'. $estimatesDeclined . '</tbody>'. $tableFooter .'</table></div>';
		else
			$declinedEstimates .= '<div id="tabs-5"><p class="center">There are currently no Declined Estimates Listed</p></div>';
		
		// Dead Estimates	
		$deadEstimates = '';
		if (!empty($estimatesDead)) 
			$deadEstimates .= '<div id="tabs-6">' . $tableHeader . '<tbody>'. $estimatesDead . '</tbody>'. $tableFooter .'</table></div>';
		else
			$deadEstimates .= '<div id="tabs-6"><p class="center">There are currently no Declined Estimates Listed</p></div>';
		
		
		// Deleted Estimates
		$deletedEstimates = '';
		if (!empty($estimatesDeleted)) 
			$deletedEstimates .= '<div id="tabs-7">' . $tableHeader . '<tbody id="'. $tableName .'">'. $estimatesDeleted . '</tbody>'. $tableFooter .'</table></div>';
		else
			$onholdEstimates .= '<div id="tabs-7"><p class="center">There are currently no Deleted Estimates Listed</p></div>';
		
		$content .= '<div id="tabs">
						<ul>
							<li><a href="#tabs-1">Working</a></li>
							<li><a href="#tabs-2">Submitted</a></li>
							<li><a href="#tabs-3">Accepted</a></li>
							<li><a href="#tabs-4">On Hold</a></li>
							<li><a href="#tabs-5">Declined</a></li>
							<li><a href="#tabs-6">Dead</a></li>
							<li><a href="#tabs-7">Deleted</a></li>
						</ul>';
		
		$content .= $workingEstimates . "\n" . $submittedEstimates . "\n" . $acceptedEstimates . "\n" . $onholdEstimates . "\n" . $declinedEstimates . "\n" . $deadEstimates . "\n" . $deletedEstimates;
		$content .= '</div><!-- /#tabs -->';
	}
	include('inc/data-tables/dataTables.php');

?>