<?php

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

$db = new database();

$user_id = $_SESSION['user_id'];

if (isset($_GET['delete']) && $_GET['delete']!="") {
	$id = $_GET['delete'];

	$sql = "DELETE FROM `keranjang` WHERE id=$id";

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

	if ($result) {
		echo '<div class="alert-keranjang"><i class="fas fa-check-circle"></i>&nbsp;&nbsp;Sukses Hapus Data</div>';
	}
}

if (isset($_GET['plus']) && $_GET['plus']!="") {
	$id = $_GET['plus'];

	$sql = "SELECT a.quantity, a.id_barang, b.stok
		FROM `keranjang` a
		LEFT JOIN `barang` b ON b.id_barang = a.id_barang
		WHERE a.id = $id";

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

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

		$sisa_stok = cek_sisa_stok($db, $id_barang, $stok);

		if ($quantity < $sisa_stok) {
			$new_quantity = $quantity + 1;

			$sql = "UPDATE `keranjang` SET quantity=$new_quantity WHERE id=$id";

			$db->db_query($sql);
		}
	}
}

if (isset($_GET['minus']) && $_GET['minus']!="") {
	$id = $_GET['minus'];

	$sql = "SELECT * FROM keranjang WHERE id=$id";

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

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

		if ($quantity > 1) {
			$new_quantity = $quantity - 1;

			$sql = "UPDATE keranjang SET quantity=$new_quantity WHERE id=$id";

			$db->db_query($sql);
		}
	}
}

if (isset($_GET['tambah']) && $_GET['tambah']!="") {
	$id = $_GET['tambah'];

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

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

	while ($rows = $db->db_fetch_array($result)) {
		$lama_sewa = $rows['lama_sewa'];
		$new_lama_sewa = $lama_sewa + 1;

		$sql = "UPDATE `keranjang` SET lama_sewa=$new_lama_sewa WHERE id=$id";

		$db->db_query($sql);
	}
}

if (isset($_GET['kurang']) && $_GET['kurang']!="") {
	$id = $_GET['kurang'];

	$sql = "SELECT * FROM keranjang WHERE id=$id";

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

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

		if ($lama_sewa > 1) {
			$new_lama_sewa = $lama_sewa - 1;

			$sql = "UPDATE keranjang SET lama_sewa=$new_lama_sewa WHERE id=$id";

			$db->db_query($sql);
		}
	}
}

if (isset($_GET['alert']) && $_GET['alert'] != '') {
	if ($_GET['alert'] == 2) {
		echo '
			<div class="alert-profil" style="background: red;">
				<i class="fas fa-exclamation-triangle"></i>
				<div>Jumlah quantity barang yang anda order telah melebihi sisa stok barang kami</div>
			</div>
		';
	} else {
		echo '
			<div class="alert-profil" style="background: red;">
				<i class="fas fa-exclamation-triangle"></i>
				<div>Silahkan pilih tanggal pengambilan barang anda</div>
			</div>
		';
	}
}

function cek_sisa_stok($db, $id_produk, $stok) {
	$sql = 'SELECT b.quantity FROM `barang` a
		LEFT JOIN `order_produk` b ON b.id_barang = a.id_barang
		LEFT JOIN `order` c ON c.id = b.id_order
		WHERE a.id_barang = '.$id_produk.' AND (c.status = \'barang sudah diambil\' OR c.status = \'verifikasi pembayaran sukses\')';

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

	if ($num_rows > 0) {
		while ($sewa = $db -> db_fetch_array($result)) {
			$jumlah_sewa = $jumlah_sewa + $sewa['quantity'];
		}
		$sisa_stok = $stok - $jumlah_sewa;

		 return $sisa_stok;
	} else {
		return $stok;
	}
}

?>

<div class="cart-layout">
	<div class="list-barang-keranjang">
		<div class="list-barang-keranjang-title">Keranjang</div>
		<div class="title-tabel-list-keranjang">
			<div class="data-list-keranjang-produk">Produk</div>
			<div class="data-list-keranjang-harga">Harga</div>
			<div class="data-list-keranjang-quantity">Quantity</div>
			<div class="data-list-keranjang-quantity">Durasi Sewa (Hari)</div>
			<div class="data-list-keranjang-subtotal">Subtotal</div>
		</div>
		<div class="garis"></div>
		<div class="data-produk-keranjang">
			<?php

			$sql = "SELECT * FROM keranjang WHERE id_user=$user_id";

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

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

					$sql2 = "SELECT * FROM barang WHERE id_barang=$id_barang";

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

					if ($num_rows2 > 0) {
						while ($rows2 = $db->db_fetch_array($result2)) {
							$nama = $rows2['nama'];
							$harga = $rows2['harga'];
							$foto = $rows2['foto'];
							list_data_keranjang($id_keranjang, $nama, $harga, $foto, $quantity, $lama_sewa);
						}
					}
				}
			} else {
				echo '<div class="keranjang-kosong">Tidak ada barang di keranjang anda</div>';
			}
			
			?>
		</div>
	</div>
	<div class="total-pembayaran-keranjang">
		<div class="total-bayar-layout">
			<form action="<?php cek_profil_user($db, $user_id); ?>" method="post" class="keranjang-form">
				<div class="total-bayar-title">Total Pembayaran</div>
				<div class="title">Tanggal Pengambilan Barang</div>
				<input type="date" name="tanggal" id ="date_input" dateformat="d M y" required>
				<div class="relative">
					<div class="subtotal-bayar">Subtotal</div>
					<div class="subtotal-bayar">Rp. <?php get_subtotal_pembayaran($db, $user_id); ?></div>
				</div>
				<div class="garis"></div>
				<div class="relative">
					<div class="total-bayar">Total</div>
					<div class="total-bayar">Rp. <?php get_subtotal_pembayaran($db, $user_id); ?></div>
				</div>
				<?php

				if ($num_rows > 0) {
					echo '<input type="submit" value="CHECKOUT SEKARANG">';
				}

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

<script type="text/javascript">
	$("#date_input").on("change", function () {
		($.datepicker.formatDate($(this).attr("dateformat"), new Date($(this).val())));
	});
</script>

<?php

function cek_profil_user($db, $user_id){
	$sql = "SELECT * FROM profil_user WHERE id_user=$user_id";

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

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

		if ($nama != 'kosong' && $hp != 'kosong' && $alamat != 'kosong') {
			echo 'checkout.php?total='.get_subtotal_pembayaran_btn($db, $user_id);
		} else {
			echo 'index.php?menu=profil&alert=1';
		}
	}
}

function list_data_keranjang($id_keranjang, $nama, $harga, $foto, $quantity, $lama_sewa){
	echo '
	<div class="data-tabel-list-keranjang">
		<div class="data-list-keranjang-produk">
			<div class="data-keranjang-produk">
				<a href="?menu=keranjang&delete='.$id_keranjang.'" class="hapus-data-keranjang">Hapus</a>
				<img src="img-barang/'.$foto.'">
				<div>'.$nama.'</div>
			</div>
		</div>
		<div class="data-list-keranjang-harga">Rp. '.number_format($harga, 0, ',', '.').'</div>
		<div class="data-list-keranjang-quantity">
			<div class="quantity-form">
				<a href="?menu=keranjang&minus='.$id_keranjang.'" class="minus"><i class="fas fa-minus"></i></a>
				<div>'.$quantity.'</div>
				<a href="?menu=keranjang&plus='.$id_keranjang.'" class="plus"><i class="fas fa-plus"></i></a>
			</div>
		</div>
		<div class="data-list-keranjang-quantity">
			<div class="quantity-form">
				<a href="?menu=keranjang&kurang='.$id_keranjang.'" class="minus"><i class="fas fa-minus"></i></a>
				<div>'.$lama_sewa.'</div>
				<a href="?menu=keranjang&tambah='.$id_keranjang.'" class="plus"><i class="fas fa-plus"></i></a>
			</div>
		</div>
		<div class="data-list-keranjang-subtotal">Rp. '.subtotal_keranjang($harga, $quantity, $lama_sewa).'</div>
	</div>
	<div class="garis"></div>
	';
}

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

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

	$sql = "SELECT * FROM keranjang WHERE id_user=$user_id";

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

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

			$sql2 = "SELECT * FROM barang WHERE id_barang=$id_barang";

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

			if ($num_rows2 > 0) {
				while ($rows2 = $db->db_fetch_array($result2)) {
					$harga = $rows2['harga'];
					
					$subtotal_pembayaran = $subtotal_pembayaran + ($harga * $quantity * $lama_sewa);
				}
			}
		}
		echo number_format($subtotal_pembayaran, 0, ',', '.');
	} else {
		echo 0;
	}

}

function get_subtotal_pembayaran_btn($db, $user_id){
	$subtotal_pembayaran = 0;

	$sql = "SELECT * FROM keranjang WHERE id_user=$user_id";

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

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

			$sql2 = "SELECT * FROM barang WHERE id_barang=$id_barang";

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

			if ($num_rows2 > 0) {
				while ($rows2 = $db->db_fetch_array($result2)) {
					$harga = $rows2['harga'];
					
					$subtotal_pembayaran = $subtotal_pembayaran + ($harga * $quantity * $lama_sewa);
				}
			}
		}
		return $subtotal_pembayaran;
	} else {
		return "0";
	}

}

?>