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

<main class="wrap">
<div class="ppob-page">
	<div class="ppob-card">
		<h4 class="mt-3 text-center">Ulasan Produk Pesanan</h4>
		<hr>

		<?php if (count($sudahUlasan) > 0): ?>
			<?php foreach ($sudahUlasan as $r):
				$produk = $func->getProduk($r->idproduk, 'semua');
			?>
				<div class="col-12">
					<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($r->idproduk, 'utama') ?>" alt="produk"></figure>
											</div>
										</div>
										<div class="col">
											<a href="<?= $produk !== null ? site_url('produk/' . $produk->url) : '#' ?>">
												<p class="text-dark mb-1 h6 d-block"><?= $produk !== null ? esc($produk->nama) : 'Produk telah dihapus' ?></p>
											</a>
											<span style="color:gold;">
												<?php for ($s = 1; $s <= 5; $s++): ?>
													<i class="fa fa-star <?= $r->nilai < $s ? 'text-secondary' : '' ?>"></i>
												<?php endfor; ?>
											</span>
											<small style="display:block;">
												<?php
												$labelNilai = [1 => '(produk sangat buruk)', 2 => '(produk kurang baik)', 3 => '(produk standar)', 4 => '(produk baik)', 5 => '(produk sangat baik)'];
												echo $labelNilai[(int) $r->nilai] ?? '';
												?>
												<br/>
												<?= esc($func->ubahTgl('d M Y H:i', $r->tgl)) ?>
											</small>
											<?php if ($r->keterangan !== ''): ?>
												<div class="row">
													<div class="col-md-6">
														<textarea class="form-control" readonly><?= esc($r->keterangan) ?></textarea>
													</div>
												</div>
											<?php endif; ?>
										</div>
									</div>
								</div>
							</li>
						</ul>
					</div>
				</div>
			<?php endforeach; ?>

		<?php elseif ((int) $usrid !== (int) $order->usrid): ?>
			<div class="m-b-60 p-lr-0-sm w-full">
				<h5 class="p-lr-14 cl1">Pembeli belum memberikan ulasan untuk pesanan ini</h5>
			</div>

		<?php else: ?>
			<div class="col-12">
				<form class="row m-lr-0" id="addreview">
					<input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>" style="display: none">
					<?php $no = 1; foreach ($produkPesanan as $t):
						$produk = $func->getProduk($t->idproduk, 'semua');
					?>
						<div class="col-12">
							<div class="bor10 p-lr-40 p-t-30 m-lr-0-xl p-lr-15-sm">
								<input type="hidden" name="orderid[]" value="<?= esc($order->orderid) ?>" />
								<input type="hidden" name="produk[]" value="<?= (int) $t->idproduk ?>" />
								<div class="px-0 col-12">
									<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($t->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' ?></p>
															<div id="ulasan_<?= $t->id ?>">
																<span class="wrap-rating rating-star pointer" style="color:gold;">
																	<?php for ($s = 1; $s <= 5; $s++): ?>
																		<i class="item-rating pointer fa fa-star text-secondary nilai_<?= $s ?>" onclick="nilai(<?= $t->id ?>,<?= $s ?>);$('#nilai_<?= $no ?>').val(<?= $s ?>);"></i>
																	<?php endfor; ?>
																	<input type="hidden" class="nilai" id="nilai_<?= $no ?>" name="nilai[]" value="0">
																</span>
															</div>
															<div class="row mt-3">
																<div class="col-md-6">
																	<textarea class="form-control" name="keterangan[]" placeholder="Beritahu yang lain tentang produk ini"></textarea>
																</div>
															</div>
														</div>
													</div>
												</div>
											</li>
										</ul>
									</div>
								</div>
							</div>
						</div>
					<?php $no++; endforeach; ?>
					<div class="col-12 p-lr-20 m-b-30">
						<button type="submit" class="submit btn btn-success btn-rounded shadow mb-2">KIRIM ULASAN</button>
						<a href="javascript:history.back()" class="submit btn btn-danger btn-rounded shadow mb-2">LAIN KALI</a>
						<h5 class="col1 loaders" style="display:none;"><i class="fa fa-spin fa-circle-o-notch"></i> mengirim ulasan</h5>
					</div>
				</form>
			</div>
		<?php endif; ?>
	</div>
</div>
</main>

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

	$(function() {
		$("#addreview").on("submit", function(e) {
			e.preventDefault();
			$(".submit").hide();
			$(".loaders").show();

			var nonempty = $('.nilai').filter(function() { return this.value == '0'; });

			if (nonempty.length == 0) {
				$.post("<?= site_url("assync/tambahulasan") ?>", $(this).serialize(), function(msg) {
					var data = eval("(" + msg + ")");
					if (data.success == true) {
						swal("Berhasil!", "berhasil mengirim ulasan", "success").then((value) => { location.reload(); });
					} else {
						swal("Gagal!", "terjadi kendala pada saat mengirim ulasan, cobalah beberapa saat lagi", "error");
						$(".submit").show();
						$(".loaders").hide();
					}
				});
			} else {
				swal("Gagal!", "Semua produk harus diberi nilai", "error").then((value) => {
					$(".submit").show();
					$(".loaders").hide();
				});
			}
		});
	});

	function nilai(trx, num) {
		for (var i = 1; i <= num; i++) {
			$("#ulasan_" + trx + " .nilai_" + i).removeClass("text-secondary");
		}
		for (var i = num + 1; i <= 5; i++) {
			$("#ulasan_" + trx + " .nilai_" + i).addClass("text-secondary");
		}
	}
</script>
