<?php
$irTotal = 0;
$mnTotal = 0;
$crTotal = 0;
$prTotal = 0;
$spTotal = 0;
$trTotal = 0;
$rfTotal = 0;
$sumTotal = 0;
?>
<div class="card">
	<div class="card-header">
		<h4 class='text-center fw-bold mt-1'>ACTIVITIES BY TSO – THIS MONTH</h4>
	</div>
	<div class="card-body">
		<div class='row'>
			<div class="col-12">
				<table class='table small table-bordered'>
					<thead class='table-info'>
						<tr class='fw-bold text-center'>
							<th colspan=10><h5>TSO IT</h5></th>
						</tr>
						<tr style='text-align: center;'>
							<th>TSO</th>
							<th>IR</th>
							<th>MN</th>
							<th>CR</th>
							<th>PR</th>
							<th>SP</th>
							<th>TR</th>
							<th>RF</th>
							<th>Total</th>
							<th>IC</th>
						</tr>
					</thead>
					<tbody>
						<?php
						$iCounter = 0; 
						foreach($count as $row) : 
						?>
						
						<?php 
							// User 5 adalah Milik SPV TSO IT
							if($row['userid']==5 || $row['reportto']==5 ) :
								
								// Count Untuk Banyak Iterasi
								$iCounter++; 
						?>
							
							<?php // Hitung Total
								$irTotal += $row['IR'];
								$mnTotal += $row['MN'];
								$crTotal += $row['CR'];
								$prTotal += $row['PR'];
								$spTotal += $row['SP'];
								$trTotal += $row['TR'];
								$rfTotal += $row['RF'];
								$sumTotal += $row['total'];
							?>

							<tr style='text-align: center;'>
								<td style='text-align: left;'><?= $row['firstname'] . ' ' . $row['lastname'];?></td>
								<td><?= $row['IR'];?></td>
								<td><?= $row['MN'];?></td>
								<td><?= $row['CR'];?></td>
								<td><?= $row['PR'];?></td>
								<td><?= $row['SP'];?></td>
								<td><?= $row['TR'];?></td>
								<td><?= $row['RF'];?></td>
								<td class='fw-bold' style="text-align: center;"><?= $row['total']; ?></td>
								<td> - </td>
							</tr>
						
						<?php endif; ?>
						<?php endforeach; ?>
						
						<tr class='text-center fw-bold' style='color:#0C356A;'>
							<td class='text-end fs-6'>Total</td>
							<td><?=$irTotal;?></td>
							<td><?=$mnTotal;?></td>
							<td><?=$crTotal;?></td>
							<td><?=$prTotal;?></td>
							<td><?=$spTotal;?></td>
							<td><?=$trTotal;?></td>
							<td><?=$rfTotal;?></td>
							<td><?=$sumTotal;?></td>
							<td> </td>
						</tr>
						<!-- Apabila hasil 0 maka tampilkan 0, jika lebih dari 0 maka lakukan batasan koma pada hasil -->
						<tr class='text-center fw-bold' style='color:#0C356A;'>
							<td class='text-end fs-6'>Average</td>
							<td><?=($irTotal/$iCounter > 0 ) ? number_format($irTotal/$iCounter, 1, '.', '') : 0;?></td>
							<td><?=($mnTotal/$iCounter > 0 ) ? number_format($mnTotal/$iCounter, 1, '.', '') : 0;?></td>
							<td><?=($crTotal/$iCounter > 0 ) ? number_format($crTotal/$iCounter, 1, '.', '') : 0;?></td>
							<td><?=($prTotal/$iCounter > 0 ) ? number_format($prTotal/$iCounter, 1, '.', '') : 0;?></td>
							<td><?=($spTotal/$iCounter > 0 ) ? number_format($spTotal/$iCounter, 1, '.', '') : 0;?></td>
							<td><?=($trTotal/$iCounter > 0 ) ? number_format($trTotal/$iCounter, 1, '.', '') : 0;?></td>
							<td><?=($rfTotal/$iCounter > 0 ) ? number_format($rfTotal/$iCounter, 1, '.', '') : 0;?></td>
							<td><?=($sumTotal/$iCounter > 0 ) ? number_format($sumTotal/$iCounter, 1, '.', '') : 0;?></td>
							<td> </td>
						</tr>
						<?php // Reset Total
							$irTotal = 0;
							$mnTotal = 0;
							$crTotal = 0;
							$prTotal = 0;
							$spTotal = 0;
							$trTotal = 0;
							$rfTotal = 0;
							$sumTotal = 0;
						?>
					</tbody>
				</table>
			</div>
		</div>
	</div>
</div>

