<?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 To Do</a>';
	
	include('admin/pieces-parts/todoFilters.php'); 
	include('pages/inc/page-header.php');

	$parentClients = getPclientArray($con);

	if (!empty($_REQUEST['parentClient']))
		$pclientID = $_REQUEST['parentClient'];
	else
		$pclientID =  '';	 				
	$pClients = getPclientOptions($pclientID,$con);
	
	$query = "SELECT *, todo.clientID AS todoClientID FROM todo_list todo LEFT JOIN clients c ON (todo.clientID = c.clientID) LEFT JOIN projects p ON (todo.projectID = p.projectID) WHERE ";
	$query .= (($todoStatus == 1) ? "todo.todoDone=1" : "todo.todoDone=0 AND todo.display=1");
	$query .= " ORDER BY c.clientName ASC, p.projectName";
	$result = $con->query($query);
	$num_rows = $result->num_rows;
	if ($num_rows > 0) {
		
		// Define Tables
		foreach ($parentClients as $key => $value) {
			$tableName = cleanClientName($value);
			$$tableName = ''; 
		} 
		
		$todoActive = ''; 
		$todoInactive = '';
		
		$tableFieldNames = '<th>Client</th>
							<th>Project</th>
							<th>To-Do Item</th>
							<th>Date Created</th>
							<th>Date Due</th>
							<th>&nbsp;</th>'; 
		
		include('inc/data-tables/table-top.php');
		include('inc/data-tables/table-bottom.php');
		
		$sortColumns = 6;
		$noSortColumns = 5; // Column Number from left (column 1=0)
		$scrollHeight = '500px';
		$tabTable = true;
		
		while ($row=$result->fetch_array(MYSQLI_ASSOC)) {
			
			// Admin Links			
			$adminLinks = '';
			
			// Company Name or Abbreviation
			$client = $row['clientName'];
			if (!empty($row['clientAbbr'])) 
				$client .= ' ('. $row['clientAbbr'] . ')';

			// Created Date
			if ($row['addDate'] == "0000-00-00")
				$addDate = "N/A";
			else
				$addDate = Date::convert($row['addDate'], 'Y-m-d', 'm/d/Y');
			
			// Due Date
			if ($row['dueDate'] == "0000-00-00")
				$dueDate = "N/A";
			else
				$dueDate = Date::convert($row['dueDate'], 'Y-m-d', 'm/d/Y');
			
			foreach ($parentClients as $key => $value) {
				if (($row['subClientOf'] == $key) || ($row['todoClientID'] == $key))
					$tableName = cleanClientName($value);
			}
			if(empty($tableName))
				$tableName = 'other';
			
			if (empty($row['todoDone'])) {
				/*foreach ($parentClients as $key => $value) {
					if (($row['subClientOf'] == $key) || ($row['todoClientID'] == $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="complete" data-page="'. $_REQUEST['type'] .'" data-section="'. $tableName .'" data-editid="' . $row['todoID'] . '" title="Complete To-Do"><i class="fa fa-check-square-o"></i></a> ';
			}
			else { 
				//$tableName = "todoDone";
				$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['todoID'] . '" title="Reactivate To-Do"><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['todoID'] . '" title="Edit To-Do"><i class="fa fa-pencil-square-o"></i></a> ';
			
			$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['todoID'] . '" title="Delete '. ucfirst($_REQUEST['type']) .'"><i class="fa fa-trash"></i></a> ';
			
			$$tableName .= '<tr id="record-'. $row['todoID'] .'">
							<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['todoID'] . '" title="Edit To-Do">' . $row['todoName'] . '</a></td>
							<td>' . $addDate . '</td>
							<td>' . $dueDate . '</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');
	}
	else {
		$content .= '<h3>No To-Do Items</h3>';
	}
	

?>