<nav class="navbar fixed-top">
    <div id="specificCard" class="card col-12" style="border-radius: 20px; background: none; border: none; margin-top: 10px;">
        <div class="row">
            <div class="col-4 icon-container">
                <a href="?<?php echo base64_encode('riwayat_transaksi_pascabayar'); ?>" class="fa fa-arrow-left text-light"></a>
            </div>
            <div class="col-8 text-right">
                <h6 class="text-light mt-2 mb-2">Riwayat Transaksi <?php echo substr(formatIndo($page[1]),3); ?></h6>
            </div>
        </div>
    </div>
</nav>
<div class="card" style="border-radius: 0px; background: none; border: none; margin-top: 50px;">
  <div class="card-body">    
  </div>
</div>
<div class="card" id="2" style="border-top-right-radius: 40px; border-top-left-radius: 40px; min-height: 900px; border: none; z-index: 1;">
  	<div class="card-body m-4">
  		<?php 
			$stmt2 = $pdo->prepare("SELECT * FROM v_detail_agen_pascabayar WHERE id_agen_voucher = :id_agen AND generate_time = :gettime");
		    $stmt2->bindParam(':id_agen', $_SESSION['agen'], PDO::PARAM_INT);
		    $stmt2->bindParam(':gettime', $page[1]);
		    $stmt2->execute();
		    $rows2 = $stmt2->fetch(PDO::FETCH_ASSOC);

			$stmt1 = $pdo->prepare("SELECT * FROM transaksi_agen_voucher WHERE id_agen_voucher = :id_agen AND DATE_FORMAT(generate_time, '%Y-%m') = :gettime AND debet IS NOT NULL");
		    $stmt1->bindParam(':id_agen', $_SESSION['agen'], PDO::PARAM_INT);
		    $stmt1->bindParam(':gettime', $page[1]);
		    $stmt1->execute();
		    $rows1 = $stmt1->fetchAll(PDO::FETCH_ASSOC);
	    ?>
	  	<table class="table mt-2">
		    <tbody>
		    	<tr>
		    		<td class="p-2" style="border: none;">Total Penjualan</td>
	                <td class="p-2 text-right text-success" style="min-width: 150px; border: none;"><h5>Rp. <b><?php echo number_format($rows2['kredit']) ?></b></h5></td>
		    	</tr>
		    	<tr>
		    		<td class="p-2" style="border: none;">Jumlah Belum di Bayar</td>
	                <td class="p-2 text-right text-danger" style="min-width: 150px; border: none;"><h5>Rp. <b><?php echo number_format($rows2['kredit'] - $rows2['debet']) ?></b></h5></td>
		    	</tr>
		    	<tr>
		    		<td class="p-2" style="border: none;">Status</td>
	                <td class="p-2 text-right text-success" style="min-width: 150px; border: none;">
	                	<?php if($rows2['kredit'] != $rows2['debet']) { 
	                		echo '<a href="#" class="btn btn-danger btn-sm">Belum Lunas</a>';
	                	} else {
	                		echo '<a href="#" class="btn btn-success btn-sm">Lunas</a>';
	                	}
	                	?>
	                </td>
		    	</tr>
		    </tbody>
		</table><hr>
		<?php if($rows1) {  ?>
  		<div style="background: rgba(128, 128, 128, 0.1); padding: 20px; border-radius: 30px;">
			<center>
	  			<h6 class="mb-4">Riwayat Pembayaran</h6>
	  		</center>
	  		<table class="table mt-2">
			    <tbody>
			    	<?php foreach ($rows1 as $pecah1) { ?>
			    	<tr>
			    		<td class="p-2">
		                    <?php echo htmlspecialchars($pecah1['input_time']); ?>
		                    <p class="m-0"><?php echo htmlspecialchars($pecah1['keterangan']); ?></p>
		                </td>
		                <td class="p-2 text-right" style="min-width: 150px;">Rp. <?php echo number_format($pecah1['debet']); ?></td>
			    	</tr>
			    	<?php } ?>
			    </tbody>
			</table>
		</div>
		<?php } ?>
		<center>
  			<h6 class="mb-4 mt-4">Riwayat Penjualan Voucher</h6>
  		</center>
  		<table class="table mt-2">
		    <tbody>
		        <?php 
		        // Menetapkan batas dan offset untuk chunking
		        $limit = 10; // Jumlah data yang diambil setiap chunk
		        $offset = 0;
		        do {
		            // Mempersiapkan query menggunakan PDO dengan LIMIT dan OFFSET
		            $stmt = $pdo->prepare("SELECT * FROM transaksi_agen_voucher WHERE id_agen_voucher = :id_agen AND DATE_FORMAT(input_time, '%Y-%m') = :gettime AND kredit IS NOT NULL ORDER BY id DESC LIMIT :limit OFFSET :offset");
		            $stmt->bindParam(':id_agen', $_SESSION['agen'], PDO::PARAM_INT);
		            $stmt->bindParam(':gettime', $page[1]);
		            
		            // Mengikat limit dan offset secara langsung (menggunakan bindValue)
		            $stmt->bindValue(':limit', (int)$limit, PDO::PARAM_INT);
		            $stmt->bindValue(':offset', (int)$offset, PDO::PARAM_INT);
		            
		            $stmt->execute();

		            // Mengambil hasil
		            $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

		            // Menampilkan hasil
		            foreach ($rows as $pecah) {
		        ?>
		            <tr>
		                <td class="p-2">
		                    <?php echo htmlspecialchars($pecah['input_time']); ?>
		                    <p class="m-0"><?php echo htmlspecialchars($pecah['keterangan']); ?></p>
		                </td>
		                <td class="p-2 text-right text-success" style="min-width: 150px;">Rp. <?php echo number_format($pecah['kredit']); ?></td>
		            </tr>
		        <?php 
		            }
		            $offset += $limit; // Mengupdate offset untuk chunk berikutnya
		        } while (count($rows) === $limit); // Terus ambil data sampai tidak ada lagi yang tersisa
		        ?>
		    </tbody>
		</table>
  	</div>
</div>