<?php

$path_to_root = "";
include $path_to_root . 'database.php';

$db = new database();

$result = false;
if (isset($_GET['result'])) {
	$result = true;
}

?>

<div class="beranda-layout">
	<?php
	if ($result == true && $_GET['result'] == 'success') {
		popup('green', '<i class="fas fa-check-circle"></i>', 'Sukses menambahkan barang ke dalam keranjang');
	} else if ($result == true && $_GET['result'] == 'failure') {
		popup('red', '<i class="fas fa-exclamation-circle"></i>', 'Gagal menambahkan barang ke dalam keranjang');
	} else if ($result == true && $_GET['result'] == 'already_available') {
		popup('red', '<i class="fas fa-exclamation-circle"></i>', 'Barang tersebut sudah ada didalam keranjang anda');
	}
	?>
	<div class="news-product">
		<div class="news-produst-title">Produk Terbaru</div>
		<div class="list-new-product">
			<?php

			$sql = "SELECT * FROM barang ORDER BY id_barang DESC LIMIT 5";

			$result = $db->db_query($sql);
			$num_rows = $db->db_num_rows($result);

			if ($num_rows > 0) {
				while ($rows = $db -> db_fetch_array($result)) {
					list_new_product($rows['id_barang'], $rows['foto'], $rows['nama'], $rows['harga']);
				}
			}

			?>
		</div>
	</div>
	<div class="list-product-beranda">
		<?php

		$sql2 = "SELECT * FROM barang";

		$result2 = $db->db_query($sql2);
		$num_rows2 = $db->db_num_rows($result2);

		if ($num_rows2 > 0) {
			$kolom = 5;
			$i=1;
			while ($rows2 = $db -> db_fetch_array($result2)) {
				if (($i) % $kolom== 1) {
					echo '<div class="list-product">';
				}
				list_product($rows2['id_barang'], $rows2['foto'], $rows2['nama'], $rows2['harga']);
				if (($i) % $kolom== 0) {
					echo '</div>';
					// $i = 0;
				}
				$i++;
			}
		}

		?>
	</div>
</div>

<?php

function list_new_product($id_barang, $foto, $nama, $harga){
	echo '
	<a href="index.php?menu=detail-produk&id='.$id_barang.'">
		<div class="new-product">
			<div class="img-product" ><img src="img-barang/' . $foto . '" width="100%" height="170"></div>
			<div class="vertical-data">
				<div class="data-product">
					<div class="data-nama-product">' . $nama . '</div>
					<div class="data-harga-product">Rp. ' . number_format($harga, 0, ',', '.') . '</div>
				</div>
				<div class="btn-cart-layout">
					<a href="function.php?action=addtocart&id='.$id_barang.'&quantity=1">
						<div class="btn-cart"><i class="fa fa-shopping-cart"></i></div>
					</a>
				</div>
			</div>
		</div>
	</a>';
}

function list_product($id_barang, $foto, $nama, $harga){
	echo '
	<a href="index.php?menu=detail-produk&id='.$id_barang.'">
		<div class="product">
			<div class="img-product" ><img src="img-barang/' . $foto . '" width="100%" height="170"></div>
			<div class="vertical-data">
				<div class="data-product">
					<div class="data-nama-product">' . $nama . '</div>
					<div class="data-harga-product">Rp. ' . number_format($harga, 0, ',', '.') . '</div>
				</div>
				<div class="btn-cart-layout">
					<a href="function.php?action=addtocart&id='.$id_barang.'&quantity=1">
						<div class="btn-cart"><i class="fa fa-shopping-cart"></i></div>
					</a>
				</div>
			</div>
		</div>
	</a>';
}

function popup($color, $icon, $text){
	echo '<div class="alert-beranda" style="background: '.$color.';">'.$icon. '&nbsp;&nbsp;' .$text.'</div>';
}

?>