<?php

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

$db = new database();

$user_id = $_SESSION['user_id'];

?>

<div class="order-layout">
	<div class="form-container">
		<div class="slide-controls">
			<input type="radio" name="slider" id="belum-bayar" checked>
			<input type="radio" name="slider" id="proses">
			<input type="radio" name="slider" id="selesai">
			<label for="belum-bayar" class="slide belum-bayar">Belum Bayar</label>
			<label for="proses" class="slide proses">Proses Order</label>
			<label for="selesai" class="slide selesai">Riwayat</label>
			<div class="slide-tab"></div>
		</div>
		<div class="form-inner">
			<div class="layout belum-bayar-layout">
				<?php

				$sql = "SELECT * FROM `order` WHERE id_user=$user_id AND (status='belum bayar' OR status='pembayaran ditolak')";

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

				if ($num_rows > 0) {
					while ($rows = $db->db_fetch_array($result)) {
						$id = $rows['id'];
						$bayar = $rows['bayar'];

						$sql2 = "SELECT * FROM `order_produk` WHERE id_order=$id LIMIT 1";

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

						while ($rows2 = $db->db_fetch_array($result2)) {
							$nama = $rows2['nama'];
							$harga = $rows2['harga'];
							$foto = $rows2['foto'];
							$quantity = $rows2['quantity'];
							$lama_sewa = $rows2['lama_sewa'];

							// list_data_belum_bayar($db, $id, $foto, $nama, $harga, $quantity, $lama_sewa, $bayar);
							list_data_belum_bayar($db, $id, $rows['tanggal_pesanan'], $rows['tanggal_pengambilan'], $rows['status'], $bayar);
						}
					}
				} else {
					echo '<div class="order-belum-bayar-kosong">Anda tidak mempunyai pesanan yang belum bayar</div>';
				}

				?>
			</div>
			<div class="layout proses-layout">
				<?php

				$sql_proses_order = "SELECT * FROM `order` WHERE id_user=$user_id AND (status='menunggu verifikasi pembayaran' OR status='barang sudah diambil' OR status='verifikasi pembayaran sukses')";

				$result_proses_order = $db->db_query($sql_proses_order);
				$num_rows_proses_order = $db->db_num_rows($result_proses_order);

				if ($num_rows_proses_order > 0) {
					while ($rows = $db->db_fetch_array($result_proses_order)) {
						list_data_proses_order($rows['id'], $rows['tanggal_pesanan'], $rows['tanggal_pengambilan'], $rows['status']);
					}
				} else {
					echo '<div class="order-belum-bayar-kosong">Anda tidak mempunyai pesanan yang sedang diproses</div>';
				}

				?>
			</div>
			<div class="layout selesai-layout">
				<?php

				$sql_proses_order = "SELECT * FROM `order` WHERE id_user=$user_id AND status='selesai'";

				$result_proses_order = $db->db_query($sql_proses_order);
				$num_rows_proses_order = $db->db_num_rows($result_proses_order);

				if ($num_rows_proses_order > 0) {
					while ($rows = $db->db_fetch_array($result_proses_order)) {
						list_data_riwayat_order($rows['id'], $rows['tanggal_pesanan'], $rows['tanggal_pengambilan'], $rows['tanggal_pengembalian']);
					}
				} else {
					echo '<div class="order-belum-bayar-kosong">Anda tidak mempunyai riwayat pesanan</div>';
				}

				?>
				<!-- <div class="list-data-selesai">
					
				</div> -->
			</div>
		</div>
	</div>
</div>

<script type="text/javascript">
	const btnBelumBayar = document.querySelector("label.belum-bayar");
	const btnProses = document.querySelector("label.proses");
	const btnSelesai = document.querySelector("label.selesai");
	const layoutBelumBayar = document.querySelector(".belum-bayar-layout");
	const layoutProses = document.querySelector(".proses-layout");
	const layoutSelesai = document.querySelector(".selesai-layout");

	btnBelumBayar.onclick = (()=>{
		layoutBelumBayar.style.marginLeft = "0%";
		layoutBelumBayar.style.borderRadius = "0px 5px 5px 5px";
	});

	btnProses.onclick = (()=>{
		layoutBelumBayar.style.marginLeft = "calc(-100% / 3)";
		layoutProses.style.borderRadius = "5px 5px 5px 5px";
	});

	btnSelesai.onclick = (()=>{
		layoutBelumBayar.style.marginLeft = "calc(-200% / 3)";
		layoutSelesai.style.borderRadius = "5px 0px 5px 5px";
	});
</script>

<?php

function list_data_riwayat_order($id, $tanggal_pesanan, $tanggal_pengambilan, $tanggal_pengembalian){
	echo '
		<div class="list-data-selesai">
			<div class="relative-list-data-order">
				<div class="data-order-proses">
					<div class="id-order">Id Pesanan</div>
					<div>Tanggal Pesanan</div>
					<div>Tanggal Pengambilan Barang</div>
					<div>Tanggal Pengembalian Barang</div>
				</div>
				<div class="data-order-proses">
					<div class="id-order">&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="button-right"><a href="?menu=detail_order&id='.$id.'">Detail Pesanan</a></div>
		</div>
	';
}

function list_data_proses_order($id, $tanggal_pesanan, $tanggal_pengambilan, $status){
	echo '
		<div class="list-data-order">
			<div class="relative-list-data-order">
				<div class="data-order-proses">
					<div class="id-order">Id Pesanan</div>
					<div>Tanggal Pesanan</div>
					<div>Tanggal Pengambilan Barang</div>
					<div>Status Pesanan</div>
				</div>
				<div class="data-order-proses">
					<div class="id-order">&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="button-right"><a href="?menu=detail_order&id='.$id.'">Detail Pesanan</a></div>
		</div>
	';
}

// function list_data_belum_bayar($db, $id, $foto, $nama, $harga, $quantity, $durasi, $total_bayar){
function list_data_belum_bayar($db, $id, $tanggal_pesanan, $tanggal_pengambilan, $status, $total_bayar){
	echo '
		<div class="list-data-order">
			<div class="relative-list-data-order">
				<div class="data-order-proses">
					<div class="id-order">Id Pesanan</div>
					<div>Tanggal Pesanan</div>
					<div>Tanggal Pengambilan Barang</div>
					<div>Status Pesanan</div>
				</div>
				<div class="data-order-proses">
					<div class="id-order">&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="relative-list-data-order">
			// 	<img src="img-barang/'.$foto.'" width="100px" height="100px">
			// 	<div>
			// 		<div class="data-belum-bayar nama-barang-belum-bayar">'.$nama.'</div>
			// 		<div class="data-belum-bayar">Harga : Rp. '.number_format($harga, 0, ',', '.').'/Hari</div>
			// 		<div class="data-belum-bayar">Quantity : '.$quantity.'</div>
			// 		<div class="data-belum-bayar">Durasi Sewa : '.$durasi.' Hari</div>
			// 		<div class="data-belum-bayar">Subtotal : Rp. '.subtotal($harga, $quantity, $durasi).'</div>
			// 	</div>
			// </div>
			cek_btn_more_produk($id, $db).'
			<div class="relative-list-data-total-bayar-belum-bayar">
				<div>&nbsp;</div>'.
				//<div>'.total_produk($db, $id).' Produk</div>
				'<div>Total Pesanan : <label>Rp. '.number_format($total_bayar, 0, ',', '.').'</label></div>
			</div>
			<div class="btn-upload-bayar-belum-layout"><a href="index.php?menu=upload_bukti&id='.$id.'">Upload Bukti Pembayaran</a></div>
		</div>
	';
}

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 cek_btn_more_produk($id, $db){
	$sql2 = "SELECT * FROM `order_produk` WHERE id_order=$id";

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

	if ($num_rows2 > 0) {
		return '<div class="btn-more-produk"><a href="?menu=detail_order&id='.$id.'">Tampilkan '.total_produk($db, $id).' produk<br><i class="fas fa-angle-down"></i></a></div>';
	}
}

function total_produk($db, $id){
	$sql = "SELECT * FROM `order_produk` WHERE id_order=$id";

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

	$jml_produk = 0;

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

		$jml_produk = $jml_produk + $produk;
	}

	return $jml_produk;
}

function subtotal($harga, $quantity, $durasi){
	$subtotal = $harga * $quantity * $durasi;

	return number_format($subtotal, 0, ',', '.');
}

?>