<?php

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

$db = new database();

$id = $_GET['id'];

$sql = "SELECT * FROM `order` WHERE id=$id";

$review = false;

if ($id != '') {
	$result = $db->db_query($sql);

	while ($rows = $db->db_fetch_array($result)) {
		// if ($rows['status'] == 'belum bayar') {
		// 	header("location:index.php?menu=order");
		// } else {
			$id_user = $rows['id_user'];
			$bayar = $rows['bayar'];
			$status = $rows['status'];
			$tanggal_pesanan = $rows['tanggal_pesanan'];
			$tanggal_pengambilan = $rows['tanggal_pengambilan'];

			if ($status == 'selesai') {
				$review = true;
			}

			$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_bukti($id, $id_user, $bayar, $status, $nama, $hp, $email, $alamat, $tanggal_pesanan, $tanggal_pengambilan);
			}
		// }
	}
}

?>

<div class="detail-order-layout">
	<div class="garis"></div>
	<div class="title-tabel-detail-order">
		<div class="produk">Produk</div>
		<div class="harga">Harga</div>
		<div class="quantity">Quantity</div>
		<div class="quantity">Durasi Sewa (Hari)</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)) {
	 	list_produk_detail_order($db, $id, $row['id_barang'], $row['nama'], $row['harga'], $row['foto'], $row['quantity'], $row['lama_sewa'], $review);
	}

	?>
</div>

<?php

function print_header_bukti($id, $id_user, $bayar, $status, $nama, $hp, $email, $alamat, $tanggal_pesanan, $tanggal_pengambilan){
	echo '
		<div class="upload-bukti-header">
			<div class="upload-bukti-relative-layout" style="width:50%">
				<div>
					<div>Id Pesanan</div>
					<div>Tanggal Pesanan</div>
					<div>Tanggal Pengambilan</div>
					<div>Status Pesanan</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;'.$status.'</div>
				</div>
			</div>
			<div class="upload-bukti-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 list_produk_detail_order($db, $id_order, $id_barang, $nama, $harga, $foto, $quantity, $lama_sewa, $review){
	echo '
		<div class="title-tabel-detail-order">
			<div class="detail-order-relative-layout">
				<img src="img-barang/'.$foto.'">
				<div class="produk">'.$nama.'</div>
			</div>
			<div class="harga">Rp. '.number_format($harga, 0, ',', '.').' / Hari</div>
			<div class="quantity">'.$quantity.'</div>
			<div class="quantity">'.$lama_sewa.' Hari</div>
			<div class="subtotal">Rp. '.subtotal_keranjang($harga, $quantity, $lama_sewa).'</div>
		</div>
	';

	if ($review == true) {
		$sql = "SELECT * FROM review WHERE id_order=$id_order AND id_barang=$id_barang";

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

		if ($num_rows < 1) {
			echo '
				<div>
					<form action="function-add-review.php?id_order='.$id_order.'&id_barang='.$id_barang.'" method="post">
						<div class="form-review">
							<div class="upload-bukti-relative-layout">
								<div class="title-review">Jumlah Star</div>
								<input type="number" name="star" class="field-review" id="jml-star" placeholder="0" required max="5" min="1">
							</div>
							<div class="upload-bukti-relative-layout">
								<div class="title-review">Deskripsi</div>
								<input type="text" name="isi-review" class="field-review" placeholder="Deskripsi penilaian anda" required>
							</div>
							<div class="layout-btn-simpan-review">
								<input type="submit" value="Simpan" class="btn-simpan-review">
							</div>
						</div>
					</form>
				</div>
			';
		}
	}

	echo '
		<div class="garis"></div>
	';
}

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

?>