<?php

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

$db = new database();

$id_barang = $_GET['id'];

if (isset($_GET['id']) && $_GET['id']!=""){
	$sql = "SELECT * FROM barang WHERE id_barang='$id_barang'";
	$result = $db->db_query($sql);
	$num_rows = $db->db_num_rows($result);

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

?>

<div class="detail-page">
	<div class="data-detail">
		<img src="../img-barang/<?php echo $foto; ?>" width="40%">
		<div class="isi-data-detail">
			<div class="detail-nama"><?php echo $nama; ?></div>

			<?php

			$sql = "SELECT * FROM review WHERE id_barang=$id_barang";

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

			$sum_star = 0;

			if ($num_rows > 0) {
				while ($rows = $db->db_fetch_array($result)) {
					$sum_star = $sum_star + $rows['bintang'];
				}

				$average_star = ($sum_star / $num_rows);
			} else {
				$average_star = 0;
			}

			?>

			
			<div class="detail-star-review">
				<?php

					for ($i=0; $i < floor($average_star); $i++) { 
						echo '<i class="fa fa-star"></i>';
					}

					$star_i = $average_star - floor($average_star);
					if ($star_i > 0) {
						echo '<i class="fas fa-star-half-alt"></i>';
					}

					$star_o = 5 - $average_star;
					if ($star_o >= 1) {
						for ($i=0; $i < floor($star_o); $i++) { 
							echo '<i class="far fa-star"></i>';
						}
					}

				?>
			</div>
			<div class="detail-harga"><?php echo 'Rp. '.$harga; ?></div>
			<div class="detail-deskripsi"><?php echo $deskripsi; ?></div>
		</div>
	</div>
	<div class="content-reviews">
		<div class="title-reviews">
			<div class="garis-title-reviews"></div>
			<div class="review-title">Reveiws</div>
			<div class="garis-title-reviews"></div>
		</div>
		<div class="isi-review">
			<div class="star-review">
				<div class="title-star-review">
					<div class="jml-star-review"><?php echo number_format($average_star,2); ?></div>
					<div class="data-jml-star-review">
						<?php

						for ($i=0; $i < floor($average_star); $i++) { 
							echo '<i class="fa fa-star"></i>';
						}

						$star_i = $average_star - floor($average_star);
						if ($star_i > 0) {
							echo '<i class="fas fa-star-half-alt"></i>';
						}

						$star_o = 5 - $average_star;
						if ($star_o >= 1) {
							for ($i=0; $i < floor($star_o); $i++) { 
								echo '<i class="far fa-star"></i>';
							}
						}

						?>
					</div>
				</div>

				<?php

				$sql = "SELECT * FROM review WHERE bintang=5 AND id_barang=$id_barang";

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

				?>
				
				<div class="isi-title-star-review">
					<div class="isi-data-jml-star-review">
						<i class="fa fa-star"></i>
						<i class="fa fa-star"></i>
						<i class="fa fa-star"></i>
						<i class="fa fa-star"></i>
						<i class="fa fa-star"></i>
					</div>
					<div class="isi-jml-star-review"><?php echo $star_5; ?></div>
				</div>

				<?php

				$sql = "SELECT * FROM review WHERE bintang=4 AND id_barang=$id_barang";

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

				?>

				<div class="isi-title-star-review">
					<div class="isi-data-jml-star-review">
						<i class="fa fa-star"></i>
						<i class="fa fa-star"></i>
						<i class="fa fa-star"></i>
						<i class="fa fa-star"></i>
						<i class="fa fa-star-o"></i>
					</div>
					<div class="isi-jml-star-review"><?php echo $star_4; ?></div>
				</div>

				<?php

				$sql = "SELECT * FROM review WHERE bintang=3 AND id_barang=$id_barang";

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

				?>
				<div class="isi-title-star-review">
					<div class="isi-data-jml-star-review">
						<i class="fa fa-star"></i>
						<i class="fa fa-star"></i>
						<i class="fa fa-star"></i>
						<i class="fa fa-star-o"></i>
						<i class="fa fa-star-o"></i>
					</div>
					<div class="isi-jml-star-review"><?php echo $star_3; ?></div>
				</div>

				<?php

				$sql = "SELECT * FROM review WHERE bintang=2 AND id_barang=$id_barang";

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

				?>

				<div class="isi-title-star-review">
					<div class="isi-data-jml-star-review">
						<i class="fa fa-star"></i>
						<i class="fa fa-star"></i>
						<i class="fa fa-star-o"></i>
						<i class="fa fa-star-o"></i>
						<i class="fa fa-star-o"></i>
					</div>
					<div class="isi-jml-star-review"><?php echo $star_2; ?></div>
				</div>

				<?php

				$sql = "SELECT * FROM review WHERE bintang=1 AND id_barang=$id_barang";

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

				?>

				<div class="isi-title-star-review">
					<div class="isi-data-jml-star-review">
						<i class="fa fa-star"></i>
						<i class="fa fa-star-o"></i>
						<i class="fa fa-star-o"></i>
						<i class="fa fa-star-o"></i>
						<i class="fa fa-star-o"></i>
					</div>
					<div class="isi-jml-star-review"><?php echo $star_1; ?></div>
				</div>
			</div>
			<div class="data-review">

				<?php

				$sql = "SELECT * FROM review WHERE id_barang=$id_barang";

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

				if ($num_rows > 0) {
					while ($rows = $db->db_fetch_array($result)) {
						list_data_review_user($db, $rows['id_user'], $rows['tanggal'], $rows['bintang'], $rows['deskripsi']);
					}
				}

				?>
				
			</div>
		</div>
	</div>
</div>

<?php

function list_data_review_user($db, $id_user, $tanggal, $bintang, $deskripsi){
	$sql = "SELECT `nama` FROM profil_user WHERE id_user=$id_user";

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

	while ($rows = $db->db_fetch_array($result)) {
		$nama = $rows['nama'];
	}

	echo '
		<div class="context-data-review">
			<div class="data-user-review">
				<div class="nama-user-review">'.$nama.'</div>
				<div class="tanggal-review">'.$tanggal.'</div>
				<div class="jml-star-review-user">';

				for ($i=0; $i < $bintang; $i++) { 
					echo '<i class="fa fa-star"></i>';
				}

				$star_o = 5 - $bintang;

				if ($star_o > 0) {
					for ($i=0; $i < $star_o; $i++) { 
						echo '<i class="far fa-star"></i>';
					}
				}
	echo '
				</div>
			</div>
			<div class="isi-review-user">'.$deskripsi.'</div>
		</div>
	';
}

?>