<script type="text/javascript">

$(document).ready( function () {
    $('#dataTable').DataTable();
} );

$(document).ready(function () {
    $('#example').DataTable();
});
</script>


<div class="card shadow mb-4">
	<div class="card-header py-3">
		<h6 class="m-0 font-weight-bold text-primary">Water</h6>
	</div>
	<div class="card-body">
		<div class="table-responsive">
			<!--<table class="table table-bordered" id="dataTable" width="70%" cellspacing="0" style="font-size: 12px">-->
			<table class="table table-bordered table-hover table-sm" id="dataTable" width="70%" cellspacing="0" style="font-size: 12px">
					<tr >
						<th valign="middle" rowspan="4">BIL</th>
						<th valign="middle" rowspan="4">NAMA</th>
						<th valign="middle" rowspan="4">NO UNIT</th>
						<th rowspan="4">NO RESIT</th>
						<th rowspan="4">TARIKH / MASA</th>
						<th align = "center" colspan="7">KUTIPAN AIR</th>
					</tr>
					<tr>
						<th rowspan="3">SEMASA (RM) </th>
						<th colspan="2">TUNGGAKAN </th>
						<th rowspan="2" colspan="2">BAYARAN BULAN HADAPAN </th>
					</tr>
					<tr>
						<th colspan="2">PHSSB </th>
					</tr>
					<tr>
						<th>BULAN / TAHUN</th>
						<th>RM</th>
						<th>BULAN / TAHUN</th>
						<th>RM</th>
						
					</tr>
				<?PHP 
				$no = 1;
				$sumCurrentMonth = 0.00;
				$sumOutstandingNotPHSSB = 0.00;
				$sumOutstandingPHSSB = 0.00;
				$sumAdvance = 0.00;
				$sumALL = 0.00;
				while($resultWater = mysqli_fetch_array($runWater)){
					$tenant_name = $resultWater["tenant_name"];
					$unit_no = $resultWater["unit_no"];
					$receipt_no = $resultWater["receipt_no"];
					$tenant_id = $resultWater["tenant_id"];
					$payment_id = $resultWater["id"];
					$amount_paid = $resultWater["amount_paid"];
					$bill_type_id = $resultWater["bill_type_id"];
					
					
					//set payment type = Outstanding / Normal / Advance
					$payment_type = "";
					$issue_date = date("Y-m", strtotime($resultWater["issue_date"]));
					$created_at = date("Y-m", strtotime($resultWater["created_at"]));
					
					//if bill_type_id not in (1,2), payment type = Outstanding (what ever reason), 1 = rental, 2 = water
					//if($bill_type_id != 1 && $bill_type_id != 2)
						//$payment_type = "Outstanding";
					//else{
						//1. Outstanding
						if($created_at > $issue_date){
							 $payment_type = "Outstanding";
						}
						//2. Normal
						else if($created_at == $issue_date){
							 $payment_type = "Normal";
						}
						//3. Advance
						else if($created_at < $issue_date){
							 $payment_type = "Advance";
						}
					//}
					
					
				?>
				 <tbody>
				<tr>
					<td><?php echo $no; ?></td>
					<td><a href="viewTenantDashboard.php?tenant_id=<?php echo $tenant_id?>"><?php echo strtoupper($tenant_name); ?></a></td>
					<td><?php echo $unit_no; ?></td>
					<td><a href="receipt/print.php?report_type=<?php echo $report_type?>&tenant_id=<?php echo $tenant_id?>&id=<?php echo $payment_id?>"><?php echo $receipt_no; ?></a></td>
					<td><?php echo date("d-m-Y h:i:s A",strtotime($resultWater["created_at"])); ?></td>
					<?php if($created_at == $issue_date && $bill_type_id == 2){ ?>
					<td><?php echo number_format($amount_paid,2,".",","); ?></td>
					
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<?php 
						$sumCurrentMonth += $amount_paid;
					} 
					?>
					
					<?php if($created_at > $issue_date && $bill_type_id == 2){ ?>
					<td>&nbsp;</td>
					
					<td><?php echo date("M / Y",strtotime($issue_date)); ?></td>
					<td><?php echo number_format($amount_paid,2,".",","); ?></td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<?php 
						$sumOutstandingPHSSB += $amount_paid;
						} ?>
					<?php if($created_at < $issue_date && $bill_type_id == 2){ ?>
					<td>&nbsp;</td>
					
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td><?php echo date("M / Y",strtotime($issue_date)); ?></td>
					<td><?php echo number_format($amount_paid,2,".",","); ?></td>
					<?php 
						$sumAdvance += $amount_paid;
					} 
					?>
				</tr>
				
				<?php 
				
				$no++;
				} 
				?>
				 </tbody>
				<tfoot>
				<tr>
					<td colspan="5" align="center"><b>TOTAL (RM)</b></td>
					<td align="center"><b><?php echo number_format($sumCurrentMonth,2,".",",");?></b></td>
					
					<td ></td>
					<td align="center"><b><?php echo number_format($sumOutstandingPHSSB,2,".",",");?></b></td>
					<td></td>
					<td align="center"><b><?php echo number_format($sumAdvance,2,".",",");?></b></td>
				</tr>
				<?PHP 
				$sumALL  = $sumCurrentMonth  + $sumOutstandingPHSSB + $sumAdvance;
				?>
				<tr align="center">
					<td colspan="5" align="center"><b>TOTAL COLLECTIONS(RM)</b></td>
					<td colspan="5"  align="center"><b><?php echo number_format($sumALL,2,".",",");?></b></td>
				</tr>
				</tfoot>
				
			</table>
		</div>
	</div>
</div>