<?php 
include 'header.php';
$kd = mysqli_real_escape_string($conn,$_GET['kode_cs']);
$cs = mysqli_query($conn, "SELECT * FROM customer WHERE kode_customer = '$kd'");
$rows = mysqli_fetch_assoc($cs);
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard - UMKM Galeri 3D</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
<style>
body {
    background: #f3f4f6;
    font-family: 'Poppins', sans-serif;
    margin: 0;
    padding: 0;
}
.container {
    max-width: 1200px;
    margin: 5px auto;
    padding: 0 30px;
}
</style>
</head>
<body>

<div class="container" style="padding-bottom: 200px">
	<h2 style=" width: 100%; border-bottom: 4px solid #000"><b>Checkout</b></h2>
	<div class="row">
		<div class="col-md-6">
			<h4>Daftar Pesanan</h4>
			<table class="table table-stripped">
				<tr>
					<th>No</th>
					<th>Nama</th>
					<th>Harga</th>
					<th>Qty</th>
					<th>Disc</th>
					<th>Sub Total</th>
				</tr>
				<?php 
				$result = mysqli_query($conn, "SELECT k.*, p.harga, p.diskon FROM keranjang k JOIN produk p ON k.kode_produk=p.kode_produk WHERE kode_customer='$kd'");
				$no = 1;
				$grand_total = 0;

				while($row = mysqli_fetch_assoc($result)){
					$harga_asli = $row['harga'];
					$diskon = $row['diskon'];
					$harga_setelah_diskon = $harga_asli - $diskon;
					if($harga_setelah_diskon < 0) $harga_setelah_diskon = 0;

					$subtotal = $harga_setelah_diskon * $row['qty'];
					$grand_total += $subtotal;
				?>
				<tr>
					<td><?= $no; ?></td>
					<td><?= $row['nama_produk']; ?></td>
					<td>Rp <?= number_format($harga_asli); ?></td>
					<td><?= $row['qty']; ?></td>
					<td>Rp <?= number_format($diskon); ?></td>
					<td>Rp <?= number_format($subtotal); ?></td>
				</tr>
				<?php 
					$no++;
				}
				?>
				<tr>
					<td colspan="6" style="text-align:right; font-weight:bold;">Grand Total = Rp <?= number_format($grand_total); ?></td>
				</tr>

			</table>
		</div>

	</div>
	<div class="row">
	<div class="col-md-6 bg-success">
		<h5>Pastikan Pesanan Anda Sudah Benar</h5>
	</div>
	</div>
	<br>
	<div class="row">
	<div class="col-md-6 bg-warning">
		<h5>isi Form dibawah ini </h5>
	</div>
	</div>
	<br>
	<form  id="formCheckout" action="proses/order.php" method="POST">
		<input type="hidden" name="kode_cs" value="<?= $kd; ?>">
		<div class="row">
			<div class="col-md-6">
				<div class="form-group">
					<label for="exampleInputEmail1">Nama</label>
					<input type="text" class="form-control" id="exampleInputEmail1" placeholder="Nama" name="nama" value="<?= $rows['nama']; ?>" readonly>
				</div>
			</div>

			<div class="col-md-6">
				<div class="form-group">
					<label>Metode Pembayaran</label>
					<select class="form-control" id="metodePembayaran" name="metode" required>
						<option value="">Pilih Metode</option>
						<option value="transfer">Transfer</option>
						<option value="cod">COD</option>
					</select>
				</div>
			</div>
		</div>

		<div class="row">
			<div class="col-md-6">
				<div class="form-group">
					<label for="exampleInputPassword1">Provinsi</label>
					<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Provinsi" name="prov">
				</div>
			</div>

			<div class="col-md-6">
				<div class="form-group">
					<label for="exampleInputPassword1">Kota</label>
					<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Kota" name="kota">
				</div>
			</div>
		</div>

		<div class="row">
			<div class="col-md-6">
				<div class="form-group">
					<label for="exampleInputPassword1">Alamat</label>
					<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Alamat" name="almt">
				</div>
			</div>

			<div class="col-md-6">
				<div class="form-group">
					<label for="exampleInputPassword1">Kode Pos</label>
					<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Kode Pos" name="kopos">
				</div>
			</div>
		</div>

		<div class="row">
			<div class="col-md-6">
				<div class="form-group">
					<label for="nomor_telepon">Nomor Telepon</label>
					<input type="tel" class="form-control" id="nomor_telepon" name="nomor_telepon" 
						placeholder="+62">
				</div>
			</div>

			 <div class="col-md-6">
            	<div class="form-group" style="position: relative;">
            		<label for="lokasi">Detail Lokasi</label>
            		<input class="form-control" id="lokasi" name="lokasi" 
            			placeholder="Detail Lainnya (Cth: Blok / Unit No., Patokan)"
            			style="padding-right: 35px;" autocomplete="off"> <!-- tambahkan jarak kanan agar teks tidak tertutup ikon -->
            		<i class="fas fa-map-marker-alt" id="getLocation"
            			style="
            				position: absolute;
            				right: 10px;
            				top: 34px;
            				color: #ff4500;
            				cursor: pointer;
            				font-size: 18px;
            			"></i>
            	</div>
            </div> 	
		</div>

		<button type="submit" class="btn btn-success"><i class="glyphicon glyphicon-shopping-cart"></i> Order Sekarang</button>
		<a href="keranjang.php" class="btn btn-danger">Cancel</a>
	</form>
</div>

</body>
</html>

<script>
document.getElementById("getLocation").addEventListener("click", function() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            let lat = position.coords.latitude;
            let lng = position.coords.longitude;
            document.getElementById("lokasi").value = lat + ", " + lng;
        }, function(error) {
            alert("Gagal mendapatkan lokasi: " + error.message);
        });
    } else {
        alert("Browser tidak mendukung GPS.");
    }
});

// Redirect ke QRIS jika metode Transfer
document.getElementById("formCheckout").addEventListener("submit", function(e){
    const metode = document.getElementById("metodePembayaran").value;
    const kodeCs = '<?= $kd; ?>';
    if(metode === 'transfer'){
        e.preventDefault();
        window.location.href = 'qris.php?kode_cs=' + kodeCs;
    }
});
</script>


