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

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

<div class="daftar-harga-list<?=$product_item_image_enabled ? '' : ' without-product-image';?>">
	<div class="daftar-harga-list__head">
		<span>Produk</span>
		<span>Nama bahan</span>
		<span>Harga</span>
		<span aria-hidden="true"></span>
	</div>

	<?php if (!empty($result)) {
		foreach ($result as $row) {
			$id_satuan = isset($row->id_satuan) ? (int) $row->id_satuan : 0;
			$harga_jual = harga_jual($row->type_harga, $row->id, isset($id_member) ? $id_member : 0, 1, $id_satuan);
			$can_add = $harga_jual > 0;
			$foto = isset($row->foto) ? trim((string) $row->foto) : '';
			$foto_url = $foto !== '' ? base_url($foto) : '';
			$title = htmlspecialchars($row->title, ENT_QUOTES, 'UTF-8');
			$product = htmlspecialchars(nama_produk($row->id_produk), ENT_QUOTES, 'UTF-8');
			$barcode = htmlspecialchars((string) $row->barcode, ENT_QUOTES, 'UTF-8');
	?>
		<div class="daftar-harga-row<?=$can_add ? '' : ' is-disabled';?>">
			<div class="daftar-harga-product">
				<?php if ($product_item_image_enabled) { ?>
				<div class="daftar-harga-product__photo">
					<?php if ($foto_url !== '') { ?>
						<img src="<?=htmlspecialchars($foto_url, ENT_QUOTES, 'UTF-8');?>"
						     alt="<?=$title;?>"
						     loading="lazy"
						     onerror="this.style.display='none';this.nextElementSibling.style.display='flex';">
					<?php } ?>
					<div class="daftar-harga-product__placeholder"<?=$foto_url !== '' ? ' style="display:none"' : '';?>>
						<i class="fa fa-image"></i>
					</div>
				</div>
				<?php } ?>
				<div class="daftar-harga-product__copy">
					<strong><?=$title;?></strong>
					<span>Barcode:
						<?php if ($can_add) { ?>
							<a href="javascript:void(0)" class="add_to" data-id="<?=$barcode;?>"><?=$barcode;?></a>
						<?php } else { ?>
							<?=$barcode;?>
						<?php } ?>
					</span>
				</div>
			</div>
			<div class="daftar-harga-material"><?=$product;?></div>
			<div class="daftar-harga-price">
				<strong><?=daftar_harga_decimal_rp($harga_jual);?></strong>
				<?php if (!$can_add) { ?><small>Harga belum tersedia</small><?php } ?>
			</div>
			<div class="daftar-harga-add">
				<button type="button"
				        class="btn add_to"
				        data-id="<?=$barcode;?>"
				        <?=$can_add ? '' : 'disabled';?>
				        title="<?=$can_add ? 'Tambahkan ke order' : 'Harga belum tersedia';?>">
					<i class="fa fa-plus"></i>
					<span>Tambah</span>
				</button>
			</div>
		</div>
	<?php } } else { ?>
		<div class="daftar-harga-empty">
			<i class="fa fa-search"></i>
			<h6>Produk tidak ditemukan</h6>
			<p>Coba ubah kata kunci atau kategori pencarian.</p>
		</div>
	<?php } ?>
</div>

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

<style>
#show-harga .daftar-harga-list {
	min-height: 0;
	flex: 1 1 auto;
	overflow: hidden;
	border: 1px solid #e2e7ef;
	border-radius: 10px;
	background: #fff;
}
#show-harga .daftar-harga-results > .daftar-harga-list {
	overflow-y: auto;
	scrollbar-width: thin;
	scrollbar-color: #c5cfdd transparent;
}
#show-harga .daftar-harga-results > .daftar-harga-list::-webkit-scrollbar {
	width: 7px;
}
#show-harga .daftar-harga-results > .daftar-harga-list::-webkit-scrollbar-thumb {
	border-radius: 20px;
	background: #c5cfdd;
}
#show-harga .daftar-harga-list__head,
#show-harga .daftar-harga-row {
	display: grid;
	grid-template-columns: minmax(330px, 1.55fr) minmax(190px, 1fr) 150px 72px;
	align-items: center;
}
#show-harga .daftar-harga-list__head {
	position: sticky;
	top: 0;
	z-index: 2;
	min-height: 42px;
	padding: 0 16px;
	border-bottom: 1px solid #e5eaf1;
	background: #f8fafc;
	color: #354052;
	font-size: 10px;
	font-weight: 700;
	letter-spacing: .025em;
	text-transform: uppercase;
}
#show-harga .daftar-harga-row {
	min-height: 76px;
	padding: 10px 16px;
	border-bottom: 1px solid #e9edf3;
	transition: background .15s ease, transform .15s ease;
}
#show-harga .daftar-harga-row:last-child {
	border-bottom: 0;
}
#show-harga .daftar-harga-row:hover {
	background: #f8fbff;
}
#show-harga .daftar-harga-row.is-disabled {
	background: #fafafa;
	opacity: .65;
}
#show-harga .daftar-harga-product {
	display: flex;
	min-width: 0;
	align-items: center;
	gap: 13px;
}
#show-harga .daftar-harga-product__photo {
	width: 64px;
	height: 58px;
	flex: 0 0 64px;
	overflow: hidden;
	border: 1px solid #e6eaf0;
	border-radius: 8px;
	background: #f3f5f8;
}
#show-harga .daftar-harga-product__photo img {
	width: 100%;
	height: 100%;
	object-fit: cover;
	transition: transform .2s ease;
}
#show-harga .daftar-harga-row:hover .daftar-harga-product__photo img {
	transform: scale(1.04);
}
#show-harga .daftar-harga-product__placeholder {
	width: 100%;
	height: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
	color: #bdc5d1;
	font-size: 20px;
}
#show-harga .daftar-harga-product__copy {
	min-width: 0;
}
#show-harga .daftar-harga-list.without-product-image .daftar-harga-product {
	gap: 0;
}
#show-harga .daftar-harga-product__copy strong {
	display: block;
	margin-bottom: 5px;
	overflow: hidden;
	color: #172033;
	font-size: 12px;
	font-weight: 700;
	text-overflow: ellipsis;
	white-space: nowrap;
}
#show-harga .daftar-harga-product__copy span {
	color: #7f899a;
	font-size: 10px;
}
#show-harga .daftar-harga-product__copy a {
	color: #3278ee;
	font-weight: 600;
}
#show-harga .daftar-harga-material {
	padding-right: 12px;
	overflow: hidden;
	color: #606b7e;
	font-size: 11px;
	text-overflow: ellipsis;
	white-space: nowrap;
}
#show-harga .daftar-harga-price strong {
	display: block;
	color: #202c42;
	font-size: 12px;
	font-weight: 700;
}
#show-harga .daftar-harga-price small {
	display: block;
	margin-top: 3px;
	color: #d34a59;
	font-size: 8px;
}
#show-harga .daftar-harga-add {
	text-align: right;
}
#show-harga .daftar-harga-add .btn {
	display: inline-flex;
	width: 38px;
	height: 38px;
	padding: 0;
	align-items: center;
	justify-content: center;
	border: 1px solid #1769e8;
	border-radius: 8px;
	background: #fff;
	color: #1769e8;
	transition: background .15s ease, color .15s ease, transform .15s ease;
}
#show-harga .daftar-harga-add .btn span {
	display: none;
}
#show-harga .daftar-harga-add .btn:hover {
	background: #1769e8;
	color: #fff;
	transform: translateY(-1px);
}
#show-harga .daftar-harga-add .btn:disabled {
	border-color: #d2d7df;
	background: #f5f6f8;
	color: #aab1bd;
	transform: none;
}
#show-harga .daftar-harga-footer {
	flex: 0 0 auto;
	display: flex;
	min-height: 58px;
	padding: 13px 2px 0;
	align-items: center;
	justify-content: space-between;
	color: #697386;
	font-size: 11px;
}
#show-harga .daftar-harga-footer .pagination {
	margin: 0;
}
#show-harga .daftar-harga-footer .page-link {
	min-width: 34px;
	height: 34px;
	display: inline-flex;
	align-items: center;
	justify-content: center;
	border-color: #e0e5ed;
	color: #4f5d73;
}
#show-harga .daftar-harga-footer .page-item.active .page-link {
	border-color: #1769e8;
	background: #1769e8;
	color: #fff;
}
#show-harga .daftar-harga-empty {
	padding: 55px 20px;
	color: #7e899b;
	text-align: center;
}
#show-harga .daftar-harga-empty i {
	margin-bottom: 12px;
	color: #b8c2d0;
	font-size: 28px;
}
#show-harga .daftar-harga-empty h6 {
	margin-bottom: 5px;
	color: #344054;
	font-weight: 700;
}
#show-harga .daftar-harga-empty p {
	margin: 0;
	font-size: 11px;
}
@media (max-width: 767.98px) {
	#show-harga .daftar-harga-list__head {
		display: none;
	}
	#show-harga .daftar-harga-row {
		position: relative;
		grid-template-columns: 1fr auto;
		gap: 8px 12px;
		padding: 13px;
	}
	#show-harga .daftar-harga-product {
		grid-column: 1 / -1;
	}
	#show-harga .daftar-harga-material {
		padding-left: 77px;
	}
	#show-harga .daftar-harga-list.without-product-image .daftar-harga-material {
		padding-left: 0;
	}
	#show-harga .daftar-harga-add {
		grid-row: 2;
		grid-column: 2;
	}
}
</style>
