<?php

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

$db = new database();

$id = $_GET['id'];

?>

<div class="detail-pesanan-baru-layout">
	<?php

	if ($id != '') {
		$sql = "SELECT * FROM `order` WHERE id=$id";

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

		while ($rows = $db->db_fetch_array($result)) {
			$id_user = $rows['id_user'];
			$tanggal_pengembalian = $rows['tanggal_pengembalian'];
			$tanggal_pesanan = $rows['tanggal_pesanan'];
			$tanggal_pengambilan = $rows['tanggal_pengambilan'];
			$bukti = $rows['bukti'];

			$sql2 = "SELECT * FROM `profil_user` WHERE id_user=$id_user";
			$result2 = $db->db_query($sql2);
			while ($rows2 = $db->db_fetch_array($result2)) {
				$nama = $rows2['nama'];
				$hp = $rows2['hp'];
				$email = $rows2['email'];
				$alamat = $rows2['alamat'];

				print_header_validasi($id, $tanggal_pengembalian, $nama, $hp, $email, $alamat, $tanggal_pesanan, $tanggal_pengambilan);
			}
		}
	}

	?>
	<div class="detail-pesanan-baru-relative-layout">
		<div class="tabel-data-produk-bukti-pembayaran">
			<div class="garis"></div>
			<div class="title-tabel-list-data">
				<div class="produk">Produk</div>
				<div class="harga">Harga</div>
				<div class="quantity">Quantity</div>
				<div class="quantity">Durasi Sewa</div>
				<div class="subtotal">Subtotal</div>
			</div>
			<div class="garis"></div>
			<?php

			$sql_produk_order = "SELECT * FROM order_produk WHERE id_order=$id";

			$result_produk_order = $db->db_query($sql_produk_order);

			while ($row = $db->db_fetch_array($result_produk_order)) {
			 	validasi_bukti_list_produk($row['nama'], $row['harga'], $row['foto'], $row['quantity'], $row['lama_sewa']);
			 } 

			?>
			<div class="detail-pesanan-baru-relative-layout">
				<div class="title-total">Total Pesanan</div>
				<div class="isi-total">Rp. <?php get_subtotal_pembayaran($db, $id); ?></div>
			</div>
			<div class="garis"></div>
		</div>
		<div class="form-bukti-pembayaran-luar">
			<div class="form-bukti-pembayaran">
				<div class="title-form">Bukti Pembayaran</div>
				<img src="../img-bukti/<?php echo $bukti; ?>">
			</div>
		</div>
	</div>
</div>

<?php

function print_header_validasi($id, $tanggal_pengembalian, $nama, $hp, $email, $alamat, $tanggal_pesanan, $tanggal_pengambilan){
	echo '
		<div class="detail-pesanan-baru-header">
			<div class="detail-pesanan-baru-relative-layout" style="width:50%">
				<div>
					<div>Id Pesanan</div>
					<div>Tanggal Pesanan</div>
					<div>Tanggal Pengambilan Barang</div>
					<div>Tanggal Pengembalian Barang</div>
				</div>
				<div>
					<div>&nbsp;&nbsp;:&nbsp;&nbsp;'.$id.'</div>
					<div>&nbsp;&nbsp;:&nbsp;&nbsp;'.$tanggal_pesanan.'</div>
					<div>&nbsp;&nbsp;:&nbsp;&nbsp;'.$tanggal_pengambilan.'</div>
					<div>&nbsp;&nbsp;:&nbsp;&nbsp;'.$tanggal_pengembalian.'</div>
				</div>
			</div>
			<div class="detail-pesanan-baru-relative-layout" style="width:50%">
				<div>
					<div>Nama Pemesan</div>
					<div>Nomor Hp</div>
					<div>Email</div>
					<div>Alamat</div>
				</div>
				<div>
					<div>&nbsp;&nbsp;:&nbsp;&nbsp;'.$nama.'</div>
					<div>&nbsp;&nbsp;:&nbsp;&nbsp;'.$hp.'</div>
					<div>&nbsp;&nbsp;:&nbsp;&nbsp;'.$email.'</div>
					<div>&nbsp;&nbsp;:&nbsp;&nbsp;'.$alamat.'</div>
				</div>
			</div>
		</div>
	';
}

function validasi_bukti_list_produk($nama, $harga, $foto, $quantity, $lama_sewa){
	echo '
		<div class="title-tabel-list-data">
			<div class="detail-pesanan-baru-relative-layout" style="width:35%">
				<img class="foto-produk-list-data" src="../img-barang/'.$foto.'">
				<div>'.$nama.'</div>
			</div>
			<div class="harga">Rp. '.number_format($harga, 0, ',', '.').'</div>
			<div class="quantity">'.$quantity.'</div>
			<div class="quantity">'.$lama_sewa.'</div>
			<div class="subtotal">Rp. '.subtotal_produk($harga, $quantity, $lama_sewa).'</div>
		</div>
		<div class="garis"></div>
	';
}

function subtotal_produk($harga, $quantity, $lama_sewa){
	$subtotal = $harga * $quantity * $lama_sewa;
	return number_format($subtotal, 0, ',', '.');
}

function get_subtotal_pembayaran($db, $id){
	$subtotal_pembayaran = 0;

	$sql = "SELECT * FROM order_produk WHERE id_order=$id";

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

	if ($num_rows > 0) {
		while ($rows = $db -> db_fetch_array($result)) {
			$harga = $rows['harga'];
			$quantity = $rows['quantity'];
			$lama_sewa = $rows['lama_sewa'];

			$subtotal_pembayaran = $subtotal_pembayaran + ($harga * $quantity * $lama_sewa);
		}
		echo number_format($subtotal_pembayaran, 0, ',', '.');
	} else {
		echo "0";
	}

}

?>