<?php

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

$db = new database();

$sql_barang = "SELECT * FROM barang";
$result_barang = $db->db_query($sql_barang);
$jml_barang = $db->db_num_rows($result_barang);

$sql_new_order = "SELECT * FROM `order` WHERE status='verifikasi pembayaran sukses'";
$result_new_order = $db->db_query($sql_new_order);
$jml_new_order = $db->db_num_rows($result_new_order);

$sql_validasi_pembayaran = "SELECT * FROM `order` WHERE status='menunggu verifikasi pembayaran'";
$result_validasi_pembayaran = $db->db_query($sql_validasi_pembayaran);
$jml_validasi_pembayaran = $db->db_num_rows($result_validasi_pembayaran);

$sql_pesanan_berlangsung = "SELECT * FROM `order` WHERE status='barang sudah diambil'";
$result_pesanan_berlangsung = $db->db_query($sql_pesanan_berlangsung);
$jml_pesanan_berlangsung = $db->db_num_rows($result_pesanan_berlangsung);

?>

<div class="beranda-layout">
	<div class="content-header">
		<div class="isi-header">
			<div class="title-header">Jumlah Produk</div>
			<div class="data-header"><?php echo $jml_barang; ?></div>
		</div>
		<div class="isi-header">
			<div class="title-header">Validasi Pembayaran</div>
			<div class="data-header"><?php echo $jml_validasi_pembayaran; ?></div>
		</div>
		<div class="isi-header">
			<div class="title-header">Pesanan Baru</div>
			<div class="data-header"><?php echo $jml_new_order; ?></div>
		</div>
		<div class="isi-header">
			<div class="title-header">Pesanan Berlangsung</div>
			<div class="data-header"><?php echo $jml_pesanan_berlangsung; ?></div>
		</div>
	</div>
	<div class="content-news-product">
		<div class="new-product-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>
		<br>
	</div>
	<div class="content-product">
		<div class="title-list-product">List Produk</div>
		<?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++;
			}
		}

		?>
	</div>
</div>

<?php

function list_new_product($id_barang, $foto, $nama, $harga){
	echo '
	<div class="new-product">
		<div class="img-list-new-product" ><img src="../img-barang/' . $foto . '" width="90%" height="170"></div>
		<div class="title-list-new-product">' . $nama . '</div>
		<div class="harga-list-new-product">Rp. ' . $harga . '</div>
		<div class="btn-detail">
			<a href="index.php?menu=detail produk&id='.$id_barang.'">
				<div class="btn-detail-list-new-product">Detail</div>
			</a>
		</div>
	</div>
	';
}

function list_product($id_barang, $foto, $nama, $harga){
	echo '
	<div class="product">
		<div class="img-list-new-product" ><img src="../img-barang/' . $foto . '" width="90%" height="170"></div>
		<div class="title-list-new-product">' . $nama . '</div>
		<div class="harga-list-new-product">Rp. ' . $harga . '</div>
		<div class="btn-detail">
			<a href="index.php?menu=detail produk&id='.$id_barang.'">
				<div class="btn-detail-list-new-product">Detail</div>
			</a>
		</div>
	</div>
	';
}

?>