<?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 Ongoing Fee</a>';

	include('pages/inc/page-header.php');
	
	$parentClients = getPclientArray($con);
	
	$admin = 'rate';
	$type = $_REQUEST['type'];
	$page = (isset($_POST['page']) ? $_POST['page'] : '');
	$variables = getVariables($admin,$page);

	if (!empty($_REQUEST['parentClient']))
		$pclientID = $_REQUEST['parentClient'];
	else
		$pclientID =  '';	 				
	$pClients = getPclientOptions($pclientID,$con);
	
	$query = "SELECT r.* FROM clientRates r LEFT JOIN clients c ON (c.clientID = r.clientID) WHERE display=1"; 
	if (!empty($pclientID))
		$query .= ' AND c.subClientOf='. $pclientID; 
		
	$query .= '  ORDER BY c.clientName,r.rateName';
	$result = $con->query($query);
	
	if ($result) {
		
		// Define Tables
		foreach ($parentClients as $key => $value) {
			$tableName = cleanClientName($value);
			$$tableName = ''; 
		} 
		
		$feesActive = ''; 
		$feesInactive = '';
		
		$tableFieldNames = '<th class="col-sm-4">Client</th>
							<th class="col-sm-3">Rate&nbsp;Name</th>
							<th class="col-sm-1">Amount</th>
							<th class="col-sm-1">Comp%</th>
							<th class="col-sm-1">Effective</th>
							<th class="col-sm-1">Expires</th>
							<th class="col-sm-1">&nbsp;</th>'; 
		
		include('inc/data-tables/table-top.php');
		include('inc/data-tables/table-bottom.php');
		
		$sortColumns = 9;
		$noSortColumns = 8; // Column Number from left (column 1=0)
		$scrollHeight = '500px';
		$tabTable = true;
		
		while ($row=$result->fetch_array(MYSQLI_ASSOC)) {
			
			foreach ($parentClients as $key => $value) {
				if (($row['subClientOf'] == $key) || ($row['clientID'] == $key)) 
					$tableName = cleanClientName($value);
			}
			if(empty($tableName))
				$tableName = 'other';
			
			$adminLinks = '';
			$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['rateID'] . '" title="Edit '. ucfirst($_REQUEST['type']) .'"><i class="fa fa-pencil-square-o"></i></a> ';			
			$inactStile = '';  				
			if ($row['active'] == 0)  {
				$inactStile = ' inactive';
				$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['rateID'] . '" title="Reactivate '. ucfirst($_REQUEST['type']) .'"><i class="fa fa-refresh"></i></a> '; 
			} else {
				$inactStile = '';
				$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['rateID'] . '" title="Delete '. ucfirst($_REQUEST['type']) .'"><i class="fa fa-trash"></i></a> ';
			}	
			
			$$tableName .= '<tr id="record-'. $row['rateID'] .'" class="'. $inactStile .'">
							<td>' . $row['clientName'] . '</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['rateID'] . '" title="Edit '. ucfirst($_REQUEST['type']) .'">' . $row['rateName'] . '</a></td>
							<td>&#36;' . $row['rateAmt'] . '</td>
							<td>' . $row['compPercent'] . '&#37;</td>
							<td>' . Date::convert($row['rateEffective'], 'Y-m-d', 'm/d/Y') . '</td>
							<td>' . Date::convert($row['rateExpires'], 'Y-m-d', 'm/d/Y') . '</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 Rates Listed</p></div>';
		}
		$reporttabs .= '</ul>';
		$content .= '<div id="tabs">' . $reporttabs . $tables . '</div>';
	}
	include('inc/data-tables/dataTables.php');

?>