<?php
session_start();
$cart = $_SESSION['cart'] ?? [];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Your Cart</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<link href="https://fonts.googleapis.com/css2?family=Lora:wght@400;500;600;700&display=swap" rel="stylesheet">

<style>
*{box-sizing:border-box}
body{
  margin:0;
  font-family:'Lora', serif;
  background:#EDF6E8;
  color:#1f2937;
}
.page{max-width:1200px;margin:auto;padding:30px 16px}
h2{margin-bottom:20px;color:#2F6B2F}
.cart-box{
  background:#fff;
  border-radius:18px;
  box-shadow:0 10px 26px rgba(0,0,0,0.1);
  padding:22px;
}
.cart-item{
  display:grid;
  grid-template-columns:80px 1fr auto auto auto;
  gap:16px;
  align-items:center;
  padding:14px 0;
  border-bottom:1px solid #E5E7EB;
}
.cart-item:last-child{border-bottom:none}
.cart-item img{
  width:80px;height:80px;
  object-fit:contain;
  background:#F7FBF4;
  border-radius:10px;
  padding:8px;
}
.item-info h4{margin:0 0 4px;font-size:16px}
.item-info p{margin:0;font-size:14px;color:#6B7280}
.item-price{font-weight:600;color:#E6B800}
.qty{display:flex;align-items:center;gap:8px}
.qty button{
  width:30px;height:30px;
  border:none;border-radius:6px;
  font-size:18px;font-weight:600;
  cursor:pointer;background:#F2C94C;
}
.qty span{min-width:24px;text-align:center;font-weight:600}
.remove{
  background:none;border:none;
  color:#dc2626;font-size:14px;
  cursor:pointer;
}
.summary{
  margin-top:22px;
  display:flex;
  justify-content:space-between;
  align-items:center;
  gap:16px;
}
.total{font-size:18px;font-weight:700}
.checkout-btn{
  padding:14px 22px;
  border:none;border-radius:12px;
  background:#6FB23F;
  color:#fff;font-weight:600;
  cursor:pointer;
}
.checkout-form{
  display:none;
  margin-top:30px;
  padding-top:20px;
  border-top:1px solid #E5E7EB;
}
.checkout-form h3{margin-bottom:16px;color:#2F6B2F}
.form-grid{
  display:grid;
  grid-template-columns:1fr 1fr;
  gap:14px;
}
.form-group{display:flex;flex-direction:column;gap:6px}
.form-group label{font-size:14px;font-weight:600}
.form-group input{
  padding:10px;
  border-radius:8px;
  border:1px solid #cbd5e1;
}
.whatsapp-btn{
  margin-top:16px;
  padding:14px;
  width:100%;
  border:none;border-radius:12px;
  background:#25D366;
  color:#fff;font-weight:600;
  cursor:pointer;
}
.disclaimer{margin-top:10px;font-size:13px;color:#6B7280}
.empty{text-align:center;padding:40px}
@media(max-width:768px){
  .cart-item{grid-template-columns:60px 1fr}
  .item-price,.qty,.remove{grid-column:2}
  .summary{flex-direction:column;align-items:flex-start}
  .form-grid{grid-template-columns:1fr}
}
</style>
</head>

<body>

<?php include "header.php"; ?>

<div class="page">
<h2>Your Cart</h2>

<div class="cart-box">

<?php if(!$cart){ ?>
  <div class="empty">Your cart is empty 🛒</div>
<?php } else {

$total = 0;
$orderLines = "";

foreach($cart as $item){
  $subtotal = $item['price'] * $item['qty'];
  $total += $subtotal;
  $orderLines .= "• {$item['name']} ({$item['weight']}) x {$item['qty']} = ₹{$subtotal}\n";
}
?>

<?php foreach($cart as $key=>$item){ ?>
<div class="cart-item">
  <img src="<?= $item['image'] ?>">
  <div class="item-info">
    <h4><?= $item['name'] ?></h4>
    <p><?= $item['weight'] ?></p>
  </div>
  <div class="item-price">₹<?= $item['price'] ?></div>
  <div class="qty">
    <button onclick="updateQty('<?= $key ?>',-1)">–</button>
    <span><?= $item['qty'] ?></span>
    <button onclick="updateQty('<?= $key ?>',1)">+</button>
  </div>
  <button class="remove" onclick="removeItem('<?= $key ?>')">Remove</button>
</div>
<?php } ?>

<div class="summary">
  <div class="total">Total: ₹<?= $total ?></div>
  <button class="checkout-btn" id="proceedBtn" onclick="showCheckout()">Proceed to Checkout</button>
</div>

<div class="checkout-form" id="checkoutForm">
  <h3>Delivery Details</h3>

  <div class="form-grid">
    <div class="form-group"><label>Full Name</label><input id="name"></div>
    <div class="form-group"><label>Phone</label><input id="phone"></div>
    <div class="form-group"><label>Door No</label><input id="door"></div>
    <div class="form-group"><label>Street</label><input id="street"></div>
    <div class="form-group"><label>Landmark</label><input id="landmark"></div>
    <div class="form-group"><label>City</label><input id="city"></div>
  </div>

  <button class="whatsapp-btn" onclick="openWhatsApp()">Proceed to WhatsApp Checkout</button>
  <div class="disclaimer">⚠️ Order completes only after WhatsApp Send</div>
</div>

<?php } ?>

</div>
</div>

<script>
let whatsappOpened = false;
let whatsappTime = 0;

function showCheckout(){
  document.getElementById("checkoutForm").style.display="block";
  document.getElementById("proceedBtn").style.display="none";
}

function openWhatsApp(){
  let name = document.getElementById("name").value.trim();
  let phone = document.getElementById("phone").value.trim();
  let door = document.getElementById("door").value.trim();
  let street = document.getElementById("street").value.trim();
  let landmark = document.getElementById("landmark").value.trim();
  let city = document.getElementById("city").value.trim();

  if(!name || !phone || !door || !street || !city){
    alert("Please fill all required details");
    return;
  }

  let msg = `🛒 New Order
Name: ${name}
Phone: ${phone}

Address:
${door}, ${street}
Landmark: ${landmark}
City: ${city}

Items:
<?= trim($orderLines) ?>

Total: ₹<?= $total ?>

Please confirm availability.`;

  whatsappOpened = true;
  whatsappTime = Date.now();

  window.open(
    "https://wa.me/918187818164?text=" + encodeURIComponent(msg),
    "_blank"
  );
}

window.addEventListener("focus",function(){
  if(whatsappOpened && (Date.now() - whatsappTime) > 5000){
    fetch("order_complete.php")
      .then(()=>window.location.href="index.php");
  }
});

function updateQty(key,change){
  fetch("cart_update.php",{
    method:"POST",
    headers:{ "Content-Type":"application/x-www-form-urlencoded" },
    body:`key=${key}&change=${change}`
  }).then(()=>location.reload());
}

function removeItem(key){
  fetch("cart_update.php",{
    method:"POST",
    headers:{ "Content-Type":"application/x-www-form-urlencoded" },
    body:`key=${key}&remove=1`
  }).then(()=>location.reload());
}
</script>

</body>
</html>
