<?php

	$admin = 'task'; 
	$type = $_REQUEST['type'];
	$page = (isset($_POST['page']) ? $_POST['page'] : $admin);;
	$variables = getVariables($admin,$page);
	$projPage = 'task'; 
	$parentClients = getPclientArray($con);

	$adminBtns .= '<a href="#" class="admin btn btn-default" data-toggle="modal" data-target="#adminModal" data-admintype="'. $admin .'" data-adminact="new" data-page="'. $admin .'" data-section="" data-editid=""><i class="fa fa-plus-square"></i> Add a NEW Task</a>';
	
	include('pages/inc/page-header.php');

	$query = "SELECT * FROM tasks t LEFT JOIN projects p ON (t.projectID = p.projectID) LEFT JOIN clients c ON (p.clientID = c.clientID) LEFT JOIN taskStatus ts ON (t.taskStatus = ts.taskStatID) LEFT JOIN projectStatus AS ps on (p.projectStatus = ps.pstatID) WHERE t.display=1 AND 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 t.priority ASC, t.dueDate ASC, c.clientName ASC';
	
	$result = $con->query($query);
	$task_num = mysqli_num_rows($result);
	
	if ($result) {
		
		// Define Tables
		foreach ($parentClients as $key => $value) {
			$tableName = cleanClientName($value);
			$$tableName = ''; 
		} 
		
		$tasksActive = ''; 
		$tasksInactive = '';
		
		$tableFieldNames = '<th style="col-sm-3">Task</th>
							<th style="col-sm-3">Project</th>
							<th style="col-sm-3">Client</th>
							<th style="col-sm-1">Time Est</th>
							<th style="col-sm-1">Due Date</th>
							<th style="col-sm-1">&nbsp;</th>';
		
		include('inc/data-tables/table-top.php');
		include('inc/data-tables/table-bottom.php');
		
		$sortColumns = 7;
		$noSortColumns = 5; // Column Number from left (column 1=0)
		$scrollHeight = '500px';
		$tabTable = true;
		
		while ($row=$result->fetch_array(MYSQLI_ASSOC)) {
			foreach ($variables['displayArray'] as $field) 
				$$field = $row[$field];
			include('list-item/task.php');
			$$tableName .= $listItem;	
		}
		
		// 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');
?>