<?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 = 'ongoing';
	$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 ogf.*, (ogf.feeAmt * fr.feeRecCalc) as billAmount, fd.*, c.*, fr.* FROM ongoingFees ogf LEFT JOIN feeDesc fd ON (fd.feeDescID = ogf.feeDescID) LEFT JOIN clients c ON (ogf.clientID = c.clientID) LEFT JOIN feeRec fr ON (ogf.feeRecID = fr.feeRecID) WHERE display=1"; 
	if (!empty($pclientID))
		$query .= ' AND c.subClientOf='. $pclientID; 
		
	$query .= '  ORDER BY c.clientName,fd.feeDesc';
	$result = $con->query($query);
	
	if ($result) {
		
		// Define Tables
		foreach ($parentClients as $key => $value) {
			$tableName = cleanClientName($value);
			$$tableName = ''; 
		} 
		
		$feesActive = ''; 
		$feesInactive = '';
		
		$tableFieldNames = '<th clss="col-sm-3">Client</th>
							<th clss="col-sm-2">Fee&nbsp;Type</th>
							<th clss="col-sm-1">Start</th>
							<th clss="col-sm-1">Monthly</th>
							<th clss="col-sm-1">Billing Plan</th>
							<th clss="col-sm-1">Bill&nbsp;Amount</th>
							<th clss="col-sm-1">Next&nbsp;Bill</th>
							<th clss="col-sm-1">Last&nbsp;Bill</th>
							<th clss="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['feeID'] . '" title="Edit '. ucfirst($_REQUEST['type']) .'"><i class="fa fa-pencil-square-o"></i></a> ';			
			$inactStile = '';  				
			if ($row['feeActive'] == 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['feeID'] . '" 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['feeID'] . '" title="Delete '. ucfirst($_REQUEST['type']) .'"><i class="fa fa-trash"></i></a> ';
			}	
			
			// Update fee dates if necessary
			if (strtotime($row['feeNextRec']) < strtotime($today)) {
				$feeLast = $row['feeNextRec'];
				if (($feeLast == '0000-00-00') || empty($feeLast))
					$feeLast =  $row['feeStartDate'];
				if ($row['feeRecID'] == 6)
					$feeNext = addMonths($feeLast, 12);
				else 
					$feeNext = addMonths($feeLast, $row['feeRecCalc']); 
				$updateQuery = 'UPDATE ongoingFees SET feeLastRec="'. $feeLast .'", feeNextRec="'. $feeNext .'" WHERE feeID=' . $row['feeID'];	
				$updateResult = $con->query($updateQuery);
			} else {
				if (($row['feeLastRec'] == '0000-00-00') || empty($row['feeLastRec']))
					$feeLast =  $row['feeStartDate'];
				else 
					$feeLast = $row['feeLastRec'];
				$feeNext = $row['feeNextRec']; 
			}
			$feeNext = Date::convert($feeNext, 'Y-m-d', 'm/d/Y');
			$feeLast = Date::convert($feeLast, 'Y-m-d', 'm/d/Y');
			
			$$tableName .= '<tr id="record-'. $row['feeID'] .'" 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['feeID'] . '" title="Edit '. ucfirst($_REQUEST['type']) .'">' . $row['feeDesc'] . '</a></td>
							<td>' . Date::convert($row['feeStartDate'], 'Y-m-d', 'm/d/Y') . '</td>
							<td>&#36;' . $row['feeAmt'] . '</td>
							<td>' . $row['feeRecDesc'] . '</td>
							<td>&#36;' . number_format($row['billAmount'], 2,'.','') . '</td>
							<td>' . $feeNext . '</td>
							<td>' . $feeLast . '</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');

?>