<?php
if (!function_exists('produk_harga_decimal_format')) {
	function produk_harga_decimal_format($value) {
		$value = (float) $value;
		$formatted = rtrim(rtrim(number_format($value, 4, '.', ''), '0'), '.');
		if ($formatted === '') {
			return '0';
		}
		$parts = explode('.', $formatted);
		$whole = number_format((float) $parts[0], 0, ',', '.');
		return count($parts) > 1 ? $whole.','.$parts[1] : $whole;
	}
}
?>

<?php $product_item_image_enabled = product_item_image_enabled(); ?>

<?php if (!empty($result)) { ?>
	<div class="bahan-product-grid">
		<?php
		foreach ($result as $row) {
			$is_active = (int) $row->pub === 1;
			$status_stok = $row->status_stok === 'Y';
			$jml_masuk = stok_masuk($row->id) - stok_keluar($row->id);
			$produk_nama = nama_produk($row->id_produk);
			$foto = isset($row->foto) ? trim((string) $row->foto) : '';
			$foto_url = $foto !== '' ? base_url($foto) : '';
		?>
			<article class="bahan-product-card <?=$is_active ? 'is-active' : 'is-inactive';?><?=$product_item_image_enabled ? '' : ' without-product-image';?>">
				<?php if ($product_item_image_enabled) { ?>
				<div class="bahan-product-card__media">
					<?php if ($foto_url !== '') { ?>
						<img src="<?=htmlspecialchars($foto_url, ENT_QUOTES, 'UTF-8');?>"
						     alt="<?=htmlspecialchars($row->title, ENT_QUOTES, 'UTF-8');?>"
						     loading="lazy"
						     onerror="this.style.display='none';this.nextElementSibling.style.display='flex';">
					<?php } ?>
					<div class="bahan-product-card__placeholder"<?=$foto_url !== '' ? ' style="display:none"' : '';?>>
						<i class="fa fa-image"></i>
						<span>Belum ada foto</span>
					</div>
				</div>
				<?php } ?>

				<div class="bahan-product-card__content">
					<div class="bahan-product-card__heading">
						<a href="#" class="add_bahan bahan-product-card__title" data-id="<?=$row->id;?>">
							<?=htmlspecialchars($row->title, ENT_QUOTES, 'UTF-8');?>
						</a>
						<div class="bahan-product-card__tags">
							<span class="bahan-product-card__category"><?=htmlspecialchars($produk_nama, ENT_QUOTES, 'UTF-8');?></span>
							<span class="bahan-product-card__status <?=$is_active ? 'is-on' : 'is-off';?>">
								<i class="fa <?=$is_active ? 'fa-check-circle' : 'fa-pause-circle';?>"></i>
								<?=$is_active ? 'Aktif' : 'Nonaktif';?>
							</span>
						</div>
					</div>

					<div class="bahan-product-card__prices">
						<div>
							<span>Harga Modal</span>
							<strong>Rp <?=produk_harga_decimal_format($row->harga_modal);?></strong>
						</div>
						<div>
							<span>Harga Jual</span>
							<strong class="selling-price">Rp <?=produk_harga_decimal_format($row->harga_jual);?></strong>
						</div>
					</div>

					<div class="bahan-product-card__stock">
						<span>Stok</span>
						<strong class="<?=$status_stok ? '' : 'is-empty';?>">
							<?=rp($jml_masuk);?> <?=htmlspecialchars($row->satuan, ENT_QUOTES, 'UTF-8');?>
							<i class="fa <?=$status_stok ? 'fa-check-circle' : 'fa-times-circle';?>"></i>
						</strong>
					</div>

					<div class="bahan-product-card__actions">
						<button type="button" class="btn menu_harga bahan-btn-price" data-type="<?=$row->type_harga;?>" data-id="<?=$row->id;?>">
							<i class="fa fa-tag"></i> Atur Harga <span><?=$row->type_harga;?></span>
						</button>
						<button type="button" class="btn add_bahan bahan-btn-edit" data-id="<?=$row->id;?>">
							<i class="fa fa-pencil"></i> Edit
						</button>
						<button type="button" class="btn bahan-btn-delete" data-id="<?=$row->id;?>" data-toggle="modal" data-target="#confirm-delete">
							<i class="fa fa-trash"></i> Hapus
						</button>
					</div>
				</div>
			</article>
		<?php } ?>
	</div>
<?php } else { ?>
	<div class="bahan-product-empty">
		<div class="bahan-product-empty__icon"><i class="fa fa-cube"></i></div>
		<h5>Produk belum ditemukan</h5>
		<p>Ubah filter pencarian atau tambahkan produk baru.</p>
		<button type="button" class="btn btn-primary add_bahan" data-id="0"><i class="fa fa-plus"></i> Tambah Produk</button>
	</div>
<?php } ?>

<div class="bahan-list-footer">
	<span>Menampilkan <?=is_array($result) || $result instanceof Countable ? count($result) : 0;?> produk</span>
	<nav aria-label="Page navigation" class="bahan-pagination">
		<?php echo $this->ajax_pagination->create_links(); ?>
	</nav>
</div>

<script>
	$(function () {
		$('[data-toggle="tooltip"]').tooltip();
	});
</script>
