<?php $set = $func->getSetting('semua'); ?>
<?= view('customer/menu', ['func' => $func, 'db' => $db]) ?>

<main class="wrap">
<div class="ppob-page">
	<form class="p-t-0 p-b-85">
		<input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>" style="display: none">
		<div class="card">
			<div class="mt-3 text-center">
				<b>Order ID <?= esc($data->invoice) ?></b>
				<hr>
			</div>
			<div class="col-12">
				<p>Terima Kasih <b><?= esc($func->getProfil($usrid->id, 'nama', 'usrid')) ?></b></p>
				<?php if ($data->transfer > 0):
					$bayartotal = $data->transfer + $data->kodebayar;
					$batasbayar = $func->ubahTgl('D, d M Y H:i:s', date('d-M-Y H:i:s', strtotime($data->kadaluarsa)));
				?>
					<div class="row">
						<div class="col-12">
							<span class="text-black">Total: <span style="color: #c0392b; font-size: 20px;"><b>Rp <?= esc($func->formUang($bayartotal)) ?></b></span></span><br>
							<span class="text-black">Batas Bayar: <span style="color: #c0392b; font-size: 20px;"><b><?= esc($batasbayar) ?></b></span></span>
						</div>
						<div class="col-md-12"><b class="text-black">Pilih Metode Pembayaran:</b></div>
						<div class="col-md-12 metode-bayar row">
							<?php if (($set->payment_transfer ?? 0) == 1): ?>
								<div class="col-md-4 mb-3">
									<div class="metode-item manual" onclick="bayarManual()">
										<img class="icon" src="<?= base_url("assets/images/transfer.png") ?>" /><br />
										Manual<br />&nbsp;
									</div>
								</div>
							<?php endif; ?>
							<?php if (($set->payment_midtrans ?? 0) == 1): ?>
								<div class="col-md-4 mb-3">
									<div class="metode-item midtrans" onclick="bayarMidtrans()">
										<img class="icon" src="<?= base_url("assets/images/midtrans.png") ?>" /><br />
										Otomatis
									</div>
								</div>
							<?php endif; ?>
						</div>
					</div>
					<div class="row p-t-5 m-t-30 bayarmanual" style="display:none;">
						<div class="col-md-12 m-b-20"><h5 class="text-black">Silahkan transfer pembayaran ke rekening berikut:</h5></div>
						<div class="col-md-12">
							<?php foreach ($bank as $bn): ?>
								<h5 class="cl2 m-t-10 m-b-10 p-t-10 p-l-10 p-b-10" style="border-left: 8px solid #C0A230;padding-left: 10px; background: #f1ecdf;">
									<b class="text-danger"><?= esc($bn->namabank) ?>: </b><b id="rek<?= $bn->id ?>" class="text-success"><?= esc($bn->norek) ?></b> <button type="button" class="btn btn-sm btn-default" onclick="copyToClipboard('#rek<?= $bn->id ?>')">Copy</button>
									<br /><span style="font-size: 90%">a/n <?= esc($bn->atasnama) ?><br />KCP <?= esc($bn->kcp) ?></span>
								</h5>
							<?php endforeach; ?>
							<p class="m-b-5 m-t-20"><b>PENTING:</b></p>
							<ul style="margin-left: 15px;">
								<li style="list-style-type: disc;">Isi ket transfer : <b id="p2"><?= esc($data->invoice) ?></b> <button type="button" class="btn btn-sm btn-default" onclick="copyToClipboard('#p2')">Copy</button> agar cepat terproses</li>
								<li style="list-style-type: disc;">Mohon lakukan pembayaran dalam <b>1x24 jam</b></li>
								<li style="list-style-type: disc;">Sistem akan otomatis mendeteksi apabila pembayaran sudah masuk</li>
								<li style="list-style-type: disc;">Apabila sudah transfer dan status pembayaran belum berubah, mohon konfirmasi pembayaran manual di bawah</li>
								<li style="list-style-type: disc;">Pesanan akan dibatalkan secara otomatis jika Anda tidak melakukan pembayaran.</li>
							</ul>
						</div>
					</div>
					<div class="mb-3 text-center">
						<?php if (($set->payment_biaya ?? 0) > 0): ?>
							<div style="display: none;" class="bayaradmin text-black"><b>Biaya Penanganan: Rp <?= esc($func->formUang($set->payment_biaya)) ?></b></div>
						<?php endif; ?>
						<?php if ($data->midtrans_id == ''): ?>
							<a href="javascript:void(0)" onclick="payMidtrans()" style="display:none;" class="btn btn-success btn-rounded shadow btn-md text-center mb-2 bayarmidtrans"><i class="fa fa-chevron-circle-right"></i> &nbsp;<b>BAYAR MIDTRANS</b></a>
						<?php else: ?>
							<a href="<?= esc($data->midtrans_pdf) ?>" style="display:none;" class="btn btn-success btn-rounded shadow btn-md text-center mb-2 bayarmidtrans"><i class="fa fa-chevron-circle-right"></i> &nbsp;<b>DOWNLOAD TAGIHAN</b></a>
						<?php endif; ?>
						<a href="<?= site_url("manage/pesanan") ?>" style="display:none;" class="btn btn-danger btn-rounded shadow btn-md text-center mb-2 bayarmidtrans"><i class="fa fa-times"></i> &nbsp;<b>BAYAR NANTI SAJA</b></a>
						<a href="<?= site_url("manage/pesanan?konfirmasi=" . $data->id) ?>" style="display:none;" class="btn btn-warning btn-rounded shadow btn-md text-center bayarmanual"><b>KONFIRMASI PEMBAYARAN</b> &nbsp;<i class="fa fa-chevron-circle-right"></i></a>
					</div>
				<?php else: ?>
					<div class="p-b-13">
						<div class="row p-t-20">
							<div class="col-md-12"><h5 class="text-black">Metode Pembayaran: <span class="cl1" style="font-size: 16px;">Saldo <?= esc($func->getSetting('nama')) ?></span></h5></div>
						</div>
						<div class="row p-t-5">
							<div class="col-md-12">
								<p>Terima kasih, saldo <b class="cl1"><?= esc($func->getSetting('nama')) ?></b> sudah terpotong sebesar
									<span style="color: #c0392b; font-size: 20px;"><b>Rp <?= esc($func->formUang($data->saldo)) ?></b></span>
									untuk pembayaran pesanan Anda.
								</p>
							</div>
						</div>
					</div>
					<a href="<?= site_url("manage/pesanan") ?>" class="cl1 text-center w-full dis-block"><b>STATUS PESANAN</b> <i class="fa fa-chevron-circle-right"></i></a>
				<?php endif; ?>
			</div>
		</div>

		<div class="card mt-1 col-12">
			<b class="mt-2 text-center">Informasi Pengiriman</b>
			<hr>
			<div class="col-12 mb-3">
				<?php if ($alamat !== null):
					$kec = $func->getKec($alamat->idkec, 'semua');
					$kab = $func->getKab($kec->idkab ?? 0, 'nama');
				?>
					<div class="p-b-13">
						<div class="row p-t-20">
							<div class="col-md-6"><b class="text-black p-b-10">Nama Penerima</b><p class="color1"><?= strtoupper($alamat->nama) ?></p></div>
							<div class="col-md-6"><b class="text-black p-b-10">No Telepon</b><p class="color1"><?= esc($alamat->nohp) ?></p></div>
						</div>
						<div class="row p-t-20">
							<div class="col-md-12">
								<b class="text-black p-b-10">Alamat Pengiriman</b>
								<p class="color1"><?= strtoupper($alamat->alamat) ?><br/><?= esc(($kec->nama ?? '-') . ', ' . $kab) ?><br/>Kodepos <?= esc($alamat->kodepos) ?></p>
							</div>
						</div>
					</div>
				<?php endif; ?>
			</div>
		</div>

		<div class="card mt-1 col-12">
			<b class="mt-2 text-center">Produk Pesanan</b>
			<div class="row">
				<div class="col-12 px-1">
					<?php
					$ongkirTotal = 0;
					foreach ($transaksi as $trx):
						$ongkirTotal += $trx->ongkir;
						foreach ($db->table('transaksiproduk')->where('idtransaksi', $trx->id)->get()->getResult() as $res):
							$produk = $func->getProduk($res->idproduk, 'semua');
							$variasiText = '';
							if ($res->variasi != 0) {
								$var = $func->getVariasi($res->variasi, 'semua');
								if ($var !== null) {
									$variasiText = "<br/><small class='text-danger'>variasi: " . esc($func->getWarna($var->warna, 'nama')) . ' size ' . esc($func->getSize($var->size, 'nama')) . '</small>';
								}
							}
					?>
						<div class="show-product">
							<ul class="list-items mb-1">
								<li>
									<div class="col-12">
										<div class="row">
											<div class="card shadow-sm border-0">
												<div class="card-body">
													<figure class="product-image"><img src="<?= $func->getFoto($res->idproduk, 'utama') ?>" alt="produk"></figure>
												</div>
											</div>
											<div class="col">
												<p class="text-dark mb-1 h6 d-block"><?= $produk !== null ? esc($produk->nama) : 'Produk telah dihapus' ?><?= $variasiText ?></p>
												<h5 class="text-success font-weight-normal mb-0">Rp <?= esc($func->formUang($res->harga)) ?> <span style="font-size:11px">x<?= (int) $res->jumlah ?></span></h5>
												<p class="text-secondary small mb-2">Total: Rp <?= esc($func->formUang($res->harga * $res->jumlah)) ?></p>
											</div>
										</div>
									</div>
								</li>
							</ul>
						</div>
					<?php
						endforeach;
					endforeach;
					?>
				</div>
				<ul class="list-items mb-1">
					<li>
						<div class="col-12">
							<div class="row">
								<div class="col"><h5 class="text-secondary">+ Ongkir Rp <?= esc($func->formUang($ongkirTotal)) ?></h5></div>
							</div>
						</div>
					</li>
				</ul>
			</div>
		</div>
	</form>
</div>
</main>

<script src="<?= base_url('assets/js/jquery-3.3.1.min.js') ?>"></script>
<script src="<?= base_url('assets/js/popper.min.js') ?>"></script>
<script src="<?= base_url('assets/js/bootstrap.min.js') ?>"></script>
<script src="<?= base_url('assets/js/jquery.cookie.js') ?>"></script>
<script src="<?= base_url('assets/js/swiper.min.js') ?>"></script>
<script src="https://kit.fontawesome.com/2baad1d54e.js" crossorigin="anonymous"></script>
<script src="<?= base_url('assets/js/main.js') ?>"></script>
<script src="<?= base_url('assets/js/jquery.countdown.min.js') ?>"></script>
<script src="<?= base_url('assets/js/chosen.jquery.min.js') ?>"></script>
<script type="text/javascript" src="<?= base_url('assets/vendor/select2/select2.min.js') ?>"></script>

<script type="text/javascript">
	var csfrData = {};
	csfrData['<?= csrf_token() ?>'] = '<?= csrf_hash() ?>';
	$.ajaxSetup({ data: csfrData });
	$(".js-select2").each(function() {
		$(this).select2({ minimumResultsForSearch: 20, dropdownParent: $(this).next('.dropDownSelect2') });
	});
	$(".chosen").chosen();
</script>

<div id="tokenGenerated" style="display:none;"></div>
<script type="text/javascript" src="<?= esc($set->midtrans_snap ?? '') ?>" data-client-key="<?= esc($set->midtrans_client ?? '') ?>"></script>
<script type="text/javascript">
	function bayarManual() {
		$(".metode-item").removeClass("active");
		$(".metode-item.manual").addClass("active");
		$(".bayarmanual").show();
		$(".bayarmidtrans").hide();
		$(".bayaradmin").hide();
	}

	function bayarMidtrans() {
		$(".metode-item").removeClass("active");
		$(".metode-item.midtrans").addClass("active");
		$(".bayarmanual").hide();
		$(".bayarmidtrans").show();
		$(".bayaradmin").show();
	}

	function payMidtrans() {
		$.ajax({
			type: "POST",
			url: "<?= site_url("cekout/midtranstoken") ?>",
			data: { "invoice": "<?= esc($data->invoice) ?>" },
			statusCode: {
				200: function(responseObject) {
					$("#tokenGenerated").html(responseObject);
					payMidtransNow();
				},
				404: function() {
					swal("Gagal", "Pembayaran gagal diproses, silahkan hubungi admin toko.", "error");
				}
			}
		});
	}

	function payMidtransNow() {
		snap.pay($("#tokenGenerated").html(), {
			onSuccess: function(result) {
				swal("Berhasil!", "Pembayaran diterima, status pesanan akan diperbarui otomatis dalam beberapa saat.", "success").then(() => {
					window.location.href = "<?= site_url("manage/pesanan") ?>";
				});
			},
			onPending: function(result) {
				swal("Menunggu Pembayaran", "Selesaikan pembayaran sesuai instruksi, status pesanan akan diperbarui otomatis setelah pembayaran diterima.", "warning").then(() => {
					window.location.href = "<?= site_url("manage/pesanan") ?>";
				});
			},
			onError: function(result) {
				swal("Gagal", "Pembayaran gagal diproses, silahkan ulangi.", "error");
			},
			onClose: function() {}
		});
	}

	function copyToClipboard(element) {
		var $temp = $("<input>");
		$("body").append($temp);
		$temp.val($(element).text()).select();
		document.execCommand("copy");
		$temp.remove();
	}
</script>
