<?php
$page = request()->getGet("page") ?: 1;
$orderby = request()->getGet("orderby") ?: "id DESC";
$cari = request()->getGet("cari") ?: "";
$perpage = 13;
?>

	<?= view('customer/loader', ['func' => $func, 'db' => $db]) ?>
	<div class="sidebar">
		<?= view('customer/menu', ['func' => $func, 'db' => $db]) ?>
	</div>
	<div class="wrapper">
		<?= view('customer/menuheadtukunen', ['func' => $func, 'db' => $db]) ?>

		<div class="container">
			<form action="" method="get" class="search-header">
				<input type="hidden" name="<?= csrf_token(); ?>" value="<?= csrf_hash(); ?>" style="display: none">
				<div class="input-group mb-3">
					<input class="form-control" type="text" value="<?= htmlspecialchars($cari, ENT_QUOTES, 'UTF-8') ?>" name="cari" placeholder="Cari Produk">
					<div class="input-group-append">
						<button class="btn btn-outline-secondary" type="submit"><i class="material-icons md-18">search</i></button>
					</div>
				</div>
			</form>

			<div class="row">

				<?php
				$cariFilter = static function ($builder) use ($cari) {
					return $builder->groupStart()
						->like("nama", $cari)
						->orLike("harga", $cari)
						->orLike("hargareseller", $cari)
						->orLike("hargaagen", $cari)
						->orLike("deskripsi", $cari)
					->groupEnd()
					->where("status", 1)
					->where("preorder >", 0);
				};

				$dbs = $cariFilter($db->table("produk"))->get();
				$produkList = $cariFilter($db->table("produk"))->limit($perpage, ($page - 1) * $perpage)->orderBy($orderby)->get();
				$totalproduk = 0;

				foreach ($produkList->getResult() as $r) {
					$level = session()->get('lvl') ?? 0;
					if ($level == 5) {
						$result = $r->hargadistri;
					} elseif ($level == 4) {
						$result = $r->hargaagensp;
					} elseif ($level == 3) {
						$result = $r->hargaagen;
					} elseif ($level == 2) {
						$result = $r->hargareseller;
					} else {
						$result = $r->harga;
					}
					$ulasan = $func->getReviewProduk($r->id);

					$variasiList = $db->table("produkvariasi")->where("idproduk", $r->id)->get();
					$totalstok = ($variasiList->getNumRows() > 0) ? 0 : $r->stok;
					$hargs = 0;
					$harga = array();
					foreach ($variasiList->getResult() as $rv) {
						$totalstok += $rv->stok;
						if ($level == 5) {
							$harga[] = $rv->hargadistri;
						} elseif ($level == 4) {
							$harga[] = $rv->hargaagensp;
						} elseif ($level == 3) {
							$harga[] = $rv->hargaagen;
						} elseif ($level == 2) {
							$harga[] = $rv->hargareseller;
						} else {
							$harga[] = $rv->harga;
						}
						$hargs += $rv->harga;
					}

					if ($totalstok > 0) {
						$totalproduk += 1;
				?>

						<div class="col-6 col-md-4 col-lg-3 col-xl-2">
							<div class="card shadow-sm border-0 mb-4">
								<div class="card-body">

									<span class="badge badge-warning pl-1 pr-1"><?= $ulasan['nilai'] ?></span>
									<i class="stars" data-rating="<?= $ulasan['nilai'] ?>" data-num-stars="5"></i>

									<figure class="product-image"><img src="<?= $func->getFoto($r->id, "utama") ?>" alt="<?= $r->nama ?>" class=""></figure>
									<a href="<?php echo site_url('produk/' . $r->url); ?>" class="text-dark mb-1 mt-2 h6 tulisan-card"><?= $r->nama ?></a>
									<h5 class="text-success font-weight-normal mb-0">

										<div class="newPrice">
											<?php
											if ($hargs > 0) {
												echo "Rp. " . $func->formUang(min($harga)) . " - " . $func->formUang(max($harga));
											} else {
												echo "Rp. " . $func->formUang($result);
											}
											?>
										</div>
									</h5>
									<p class="text-secondary small text-mute mb-0"><?= $ulasan['ulasan'] ?> Ulasan</p>
								</div>
							</div>
						</div>
				<?php
					}
				}

				if ($totalproduk == 0) {
					echo "<div class='col-12 text-center m-tb-40'><button class='btn btn-lg btn-default shadow btn-rounded'>Produk Kosong</button></div>";
				}
				?>

			</div>
		</div>
		<div class="pagination justify-content-center">
			<?php
			if ($totalproduk > 0) {
				echo $func->createPagination($dbs->getNumRows(), $page, $perpage);
			}
			?>
		</div>
		<?= view('customer/footer_alamat', ['func' => $func, 'db' => $db]) ?>

	</div>

	<div class="modal fade " id="colorscheme" tabindex="-1" role="dialog" aria-hidden="true">
		<div class="modal-dialog modal-dialog-centered" role="document">
			<div class="modal-content ">
				<div class="modal-header theme-header border-0">
					<h6 class="">Setting Warna</h6>
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
				</div>
				<div class="modal-body pt-0">
					<div class="text-center theme-color">
						<button class="m-1 btn pink-theme-bg text-white btn-rounded-54 shadow-sm" data-theme="pink-theme"></button>
						<button class="m-1 btn brown-theme-bg text-white btn-rounded-54 shadow-sm" data-theme="brown-theme"></button>
						<button class="m-1 btn blue-theme-bg text-white btn-rounded-54 shadow-sm" data-theme="blue-theme"></button>
						<button class="m-1 btn purple-theme-bg text-white btn-rounded-54 shadow-sm" data-theme="purple-theme"></button>
						<button class="m-1 btn green-theme-bg text-white btn-rounded-54 shadow-sm" data-theme="green-theme"></button>
						<button class="m-1 btn grey-theme-bg text-white btn-rounded-54 shadow-sm" data-theme="grey-theme"></button>
					</div>
				</div>
				<div class="modal-footer">
					<div class="col-12 text-center w-100">
						<div class="row justify-content-center">
							<div class="col-auto text-right align-self-center"><i class="material-icons text-warning md-36 vm">wb_sunny</i></div>
							<div class="col-auto text-center align-self-center px-0">
								<div class="custom-control custom-switch float-right">
									<input type="checkbox" name="themelayout" class="custom-control-input" id="theme-dark">
									<label class="custom-control-label" for="theme-dark"></label>
								</div>
							</div>
							<div class="col-auto text-left align-self-center"><i class="material-icons text-dark md-36 vm">brightness_2</i></div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<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 type="text/javascript">
		function refreshTabel(page) {
			window.location.href = "<?= site_url("shop/preorder?cari=" . rawurlencode($cari)) ?>&page=" + page;
		}
	</script>
	<script>
		var csfrData = {};
		csfrData['<?= csrf_token(); ?>'] = '<?= csrf_hash(); ?>';
		$.ajaxSetup({
			data: csfrData
		});

		$.fn.stars = function() {
			return $(this).each(function() {

				var rating = $(this).data("rating");

				var numStars = $(this).data("numStars");

				var fullStar = new Array(Math.floor(rating + 1)).join('<i class="material-icons text-warning md-18 vm">star</i>');

				var halfStar = ((rating % 1) !== 0) ? '<i class="material-icons text-warning md-18 vm">star_half</i>' : '';

				var noStar = new Array(Math.floor(numStars + 1 - rating)).join('<i class="material-icons text-secondary md-18 vm">star_border</i>');

				$(this).html(fullStar + halfStar + noStar);

			});
		}

		$('.stars').stars();
	</script>
