<?php
$rows = !empty($result) ? $result : [];
$rowCount = is_array($rows) ? count($rows) : 0;
$no = (int) $this->uri->segment(3) + 1;
$jumlah = 0;
$harga = 0;
$total = 0;
?>

<div class="supplier-detail-content">
	<div class="supplier-detail-table-wrap">
		<table class="table table-bordered table-striped supplier-detail-table" id="jsonuser">
			<thead>
				<tr>
					<th style="width:1% !important;">No.</th>
					<th>Tanggal</th>
					<th>Title</th>
					<th>Jumlah</th>
					<th>Harga</th>
					<th>Total</th>
				</tr>
			</thead>
			<tbody>
				<?php if (!empty($rows)) {
					foreach ($rows as $row) {
						$sub_total = (int) $row["jumlah"] * (int) $row["harga"];
						?>
						<tr>
							<td><?php echo $no; ?></td>
							<td><?= $row["create_date"]; ?></td>
							<td><?= htmlspecialchars($row["nama_bahan"], ENT_QUOTES, 'UTF-8'); ?></td>
							<td><?= (int) $row["jumlah"]; ?></td>
							<td><?= rp($row["harga"]); ?></td>
							<td><?= rp($sub_total); ?></td>
						</tr>
						<?php
						$jumlah += (int) $row["jumlah"];
						$harga += (int) $row["harga"];
						$total += $sub_total;
						$no++;
					}
					?>
					<tr>
						<td colspan="3" class="font-weight-bold">Total Perpage</td>
						<td class="font-weight-bold"><?= rp($jumlah); ?></td>
						<td class="font-weight-bold"><?= rp($harga); ?></td>
						<td class="font-weight-bold"><?= rp($total); ?></td>
					</tr>
				<?php } else { ?>
					<tr>
						<td colspan="6" class="supplier-detail-empty">Data belum ada</td>
					</tr>
				<?php } ?>
			</tbody>
		</table>

		<div class="supplier-detail-mobile">
			<?php if (!empty($rows)) {
				$no = (int) $this->uri->segment(3) + 1;
				$jumlah = 0;
				$harga = 0;
				$total = 0;
				foreach ($rows as $row) {
					$sub_total = (int) $row["jumlah"] * (int) $row["harga"];
					$date_mobile = date('d/m/Y H:i:s', strtotime($row["create_date"]));
					?>
					<div class="supplier-detail-mobile-card">
						<div class="supplier-detail-mobile-head">
							<div class="supplier-detail-badge"><?= $no; ?></div>
							<div class="flex-grow-1 min-w-0">
								<div class="supplier-detail-mobile-title">
									<?= htmlspecialchars($row["nama_bahan"], ENT_QUOTES, 'UTF-8'); ?>
								</div>
								<div class="supplier-detail-mobile-meta"><?= $date_mobile; ?></div>
							</div>
						</div>

						<div class="supplier-detail-mobile-divider"></div>

						<div class="supplier-detail-mobile-rows">
							<div class="supplier-detail-mobile-row">
								<span>Tanggal</span>
								<strong><?= $date_mobile; ?></strong>
							</div>
							<div class="supplier-detail-mobile-row">
								<span>Jumlah</span>
								<strong><?= (int) $row["jumlah"]; ?></strong>
							</div>
							<div class="supplier-detail-mobile-row">
								<span>Harga</span>
								<strong><?= rp($row["harga"]); ?></strong>
							</div>
							<div class="supplier-detail-mobile-row">
								<span>Total</span>
								<strong><?= rp($sub_total); ?></strong>
							</div>
						</div>
					</div>
					<?php
					$jumlah += (int) $row["jumlah"];
					$harga += (int) $row["harga"];
					$total += $sub_total;
					$no++;
				}
				?>

				<div class="supplier-detail-mobile-summary">
					<div>
						<span>Total Perpage</span>
						<strong><?= rp($total); ?></strong>
					</div>
					<div>
						<span>Jumlah</span>
						<strong><?= rp($jumlah); ?></strong>
					</div>
					<div>
						<span>Harga</span>
						<strong><?= rp($harga); ?></strong>
					</div>
				</div>
			<?php } else { ?>
				<div class="supplier-detail-mobile-empty">Data belum ada</div>
			<?php } ?>
		</div>

		<?php if (!empty($rows)) { ?>
			<div class="supplier-detail-mobile-footer">
				Menampilkan : <?= $rowCount; ?>
			</div>
		<?php } ?>

		<nav aria-label="Page navigation" class="mt-2">
			<?php echo $this->ajax_pagination->create_links(); ?>
		</nav>
	</div>
</div>
