<?= view('customer/menu', ['func' => $func, 'db' => $db]) ?>

<main class="wrap">
<div class="ppob-page">
<?php
$usrid = session()->get('usrid');
$ca = $db->table('transaksiproduk')->where('usrid', $usrid)->where('jasa', 1)->where('idtransaksi', 0)->get()->getResult();
$hapusProduk = '';
?>
<?php if (count($ca) > 0): ?>

	<div class="ppob-card">
		<div class="ppob-head">
			<div class="ppob-title">Keranjang Jasa</div>
		</div>

		<div class="nominal-wrap" style="padding:0">
			<?php
			$totalbayar = 0;
			foreach ($ca as $car):
				$produk = $func->getProduk($car->idproduk, 'semua');
				$var    = $car->variasi != 0 ? $func->getVariasi($car->variasi, 'semua') : null;
				$stok   = $car->variasi != 0 ? ($var->stok ?? 0) : ($produk->stok ?? 0);

				if ($produk === null) {
					$hapusProduk .= 'hapusProduk(' . $car->id . ');';
					continue;
				}
				$totalbayar += $car->harga * $car->jumlah;
			?>
				<div class="cart-item">
					<div class="cart-remove" onclick="hapus(<?= $car->id ?>)" title="Hapus">&times;</div>

					<div class="cart-thumb">
						<img src="<?= $func->getFoto($produk->id, 'utama') ?>" alt="<?= esc($produk->nama) ?>">
					</div>

					<div class="cart-info">
						<a href="<?= base_url('jasa/' . $produk->url) ?>" class="cart-name"><?= esc($produk->nama) ?></a>
						<?php if ($car->variasi > 0 && $var !== null): ?>
							<p class="cart-meta"><b><?= esc($produk->variasi) ?>:</b> <?= esc($func->getWarna($var->warna, 'nama')) ?> <?= esc($produk->subvariasi) ?> <?= esc($func->getSize($var->size, 'nama')) ?></p>
						<?php endif; ?>
						<?php if ($car->keterangan !== ''): ?>
							<p class="cart-note"><b>Note:</b> <i><?= esc($car->keterangan) ?></i></p>
						<?php endif; ?>
						<p class="cart-meta">Harga: Rp <?= esc($func->formUang($car->harga)) ?> &middot; Sisa Stok: <?= esc($func->formUang($stok)) ?></p>
						<p class="cart-total">Total: Rp <span id="totalhargauang_<?= $car->id ?>"><?= esc($func->formUang($car->harga * $car->jumlah)) ?></span></p>
						<?php if ($stok < $car->jumlah): ?>
							<p class="cart-warn">Stok kurang</p>
						<?php endif; ?>
					</div>

					<div class="cart-qty">
						<button type="button" class="btn-num-product-down">&minus;</button>
						<input class="num-product produk-jumlah" type="text" min="<?= (int) $produk->minorder ?>" id="jumlah_<?= $car->id ?>" name="jumlah[]" value="<?= (int) $car->jumlah ?>" data-proid="<?= $car->id ?>" />
						<button type="button" class="btn-num-product-up">&plus;</button>
					</div>

					<input type="hidden" id="harga_<?= $car->id ?>" value="<?= $car->harga ?>" />
					<input type="hidden" class="totalhargaproduk" id="totalharga_<?= $car->id ?>" value="<?= $car->harga * $car->jumlah ?>" />
					<input type="hidden" name="id[]" value="<?= $car->id ?>" />
				</div>
			<?php endforeach; ?>
		</div>
	</div>

	<div class="ppob-card cart-summary">
		<div class="label">Total Bayar</div>
		<div class="total">Rp <span id="totalbayar"><?= esc($func->formUang($totalbayar)) ?></span></div>
		<div class="actions">
			<a href="<?= site_url('shop') ?>" class="btn-cek">&larr; Belanja</a>
			<a href="<?= site_url('home/pembayaranjasa') ?>" class="btn-pilih">Bayar &rarr;</a>
		</div>
	</div>

<?php else: ?>
	<div class="ppob-card cart-summary">
		<div class="label">Ups, keranjang masih kosong.</div>
		<div class="total">Rp <span id="totalbayar"><?= esc($func->formUang(0)) ?></span></div>
		<div class="actions">
			<a href="<?= site_url('shop') ?>" class="btn-cek">&larr; Belanja</a>
		</div>
	</div>
<?php endif; ?>
</div>
</main>

<script>
	var csfrData = {};
	csfrData['<?= csrf_token() ?>'] = '<?= csrf_hash() ?>';
	$.ajaxSetup({ data: csfrData });

	function formUang(angka) {
		return Number(angka).toLocaleString('id-ID');
	}

	$('.btn-num-product-down').on('click', function(e) {
		e.preventDefault();
		var numProduct = Number($(this).next().val());
		if (numProduct > 1) $(this).next().val(numProduct - 1).trigger("change");
	});

	$('.btn-num-product-up').on('click', function(e) {
		e.preventDefault();
		var numProduct = Number($(this).prev().val());
		$(this).prev().val(numProduct + 1).trigger("change");
	});

	<?= $hapusProduk ?>

	$(".produk-jumlah").on('change', function() {
		var jumlah = $(this).val();
		var prodid = $(this).attr("data-proid");
		var harga = Number($("#harga_" + prodid).val());
		var hargatotal = Number(jumlah) * harga;

		if (jumlah > 0) {
			if (jumlah < Number($(this).attr("min"))) {
				$(this).val($(this).attr("min")).trigger("change");
				return;
			}

			$.post("<?= site_url("assync/updatekeranjang") ?>", { "update": prodid, "jumlah": jumlah }, function(msg) {
				var data = eval("(" + msg + ")");
				if (data.success == false) {
					swal("Gagal", data.msg, "error").then((willDelete) => { location.reload(); });
				}
			});

			$("#totalhargauang_" + prodid).html(formUang(hargatotal));
			$("#totalharga_" + prodid).val(hargatotal);
			var sum = 0;
			$(".totalhargaproduk").each(function() { sum += parseFloat($(this).val()); });
			$("#totalbayar").html(formUang(sum));
		} else {
			swal({ title: "Anda yakin?", text: "menghapus produk dari keranjang belanja", icon: "warning", buttons: true, dangerMode: true })
				.then((willDelete) => {
					if (willDelete) {
						$.post("<?= site_url("assync/hapuskeranjang") ?>", { "hapus": prodid }, function(msg) {
							var data = eval("(" + msg + ")");
							if (data.success == true) { location.reload(); } else { confirm("Gagal menghapus pesanan, silahkan ulangi beberapa saat lagi"); }
						});
					} else {
						$(this).val($(this).attr("min"));
					}
				});
		}
	});

	function hapus(id) {
		$("#jumlah_" + id).val(0).trigger("change");
	}

	function hapusProduk(id) {
		$.post("<?= site_url("assync/hapuskeranjang") ?>", { "hapus": id }, function(msg) {
			var data = eval("(" + msg + ")");
			if (data.success == true) { location.reload(); } else { confirm("Gagal menghapus pesanan, silahkan ulangi beberapa saat lagi"); }
		});
	}
</script>
