<?php
session_start();

/* ---------------------------
   LOGIN CHECK
--------------------------- */
if (!isset($_SESSION['user_id'])) {
    $_SESSION['redirect_after_login'] = "checkout.php";
    header("Location: auth.php");
    exit();
}

include "db.php";
include "header.php";

/* ---------------------------
   LOAD CART ITEMS
--------------------------- */
$items = [];
$total = 0;

if (!empty($_SESSION['cart'])) {
    foreach ($_SESSION['cart'] as $c) {

        $stmt = $pdo->prepare("SELECT product_name, images FROM products WHERE id=? LIMIT 1");
        $stmt->execute([$c['product_id']]);
        $p = $stmt->fetch(PDO::FETCH_ASSOC);

        $imgs = json_decode($p['images'], true);
        $img = (is_array($imgs) && count($imgs) > 0) ? ltrim($imgs[0], '/') : "";

        $subtotal = $c['qty'] * $c['price'];
        $total += $subtotal;

        $items[] = [
            "product_id" => $c['product_id'],
            "name"  => $p['product_name'],
            "image" => $img,
            "qty"   => $c['qty'],
            "price" => $c['price'],
            "size"  => $c['size'],
            "garlic"=> $c['garlic'],
            "spice" => $c['spice']
        ];
    }
}

$display_total = number_format($total, 2);
?>

<!-- ================= CHECKOUT CSS (SAME AS PREVIOUS) ================= -->
<style>
:root {
    --primary-color: #002344;
    --primary-hover: #003768;
}

.checkout-wrapper {
    max-width: 1200px;
    margin: 30px auto;
    display: flex;
    gap: 30px;
    padding: 20px;
}

.checkout-left { flex: 2; }
.checkout-right {
    flex: 1.2;
    background: #fafafa;
    padding: 25px;
    border: 1px solid #eee;
    border-radius: 10px;
}

.checkout-left h3,
.checkout-right h3 {
    margin-bottom: 15px;
    font-size: 22px;
    color: #111;
}

.checkout-left input,
.checkout-left select {
    width: 100%;
    padding: 13px;
    border: 1px solid #ccc;
    border-radius: 6px;
    margin-bottom: 15px;
    font-size: 15px;
}

.two-col {
    display: flex;
    gap: 15px;
}
.two-col .half { flex: 1; }

.checkout-btn {
    width: 100%;
    padding: 15px;
    background: var(--primary-color);
    color: #fff;
    border: none;
    border-radius: 6px;
    font-size: 18px;
    cursor: pointer;
}
.checkout-btn:hover { background: var(--primary-hover); }

.order-item {
    display: flex;
    align-items: center;
    margin-bottom: 15px;
}

.order-item img {
    width: 70px;
    height: 70px;
    border-radius: 8px;
    margin-right: 12px;
    object-fit: cover;
    border: 1px solid #ddd;
}

.summary-line {
    display: flex;
    justify-content: space-between;
    margin: 12px 0;
}

.summary-total {
    font-size: 22px;
    font-weight: bold;
    margin-top: 18px;
}

.variant-text {
    font-size: 13px;
    color: #555;
}
</style>

<!-- ================= CHECKOUT UI ================= -->
<div class="checkout-wrapper">

    <!-- LEFT -->
    <div class="checkout-left">
        <h3>Delivery Information</h3>

        <form id="checkout-form" action="payment.php" method="POST">

            <select name="country" id="country-select" required>
                <option value="">Select Country</option>
                <option value="India">India</option>
                <option value="United States">United States</option>
                <option value="United Kingdom">United Kingdom</option>
            </select>

            <div class="two-col">
                <div class="half">
                    <input type="text" name="first_name" placeholder="First name" required>
                </div>
                <div class="half">
                    <input type="text" name="last_name" placeholder="Last name" required>
                </div>
            </div>

            <input type="text" name="address" placeholder="Address" required>
            <input type="text" name="apartment" placeholder="Apartment (optional)">

            <div class="two-col">
                <div class="half">
                    <input type="text" name="city" placeholder="City" required>
                </div>
                <div class="half">
                    <input type="text" name="state" placeholder="State" required>
                </div>
            </div>

            <input type="text" name="pincode" placeholder="Pincode" required>
            <input type="text" name="phone" placeholder="Phone" required>

        </form>
    </div>

    <!-- RIGHT -->
    <div class="checkout-right">
        <h3>Order Summary</h3>

        <?php foreach ($items as $item): ?>
            <div class="order-item">
                <img src="https://lalitavarahifoods.com/<?= $item['image'] ?>">
                <div>
                    <strong><?= htmlspecialchars($item['name']) ?></strong><br>
                    <?= $item['qty'] ?> × ₹<?= number_format($item['price'], 2) ?><br>
                    <span class="variant-text">
                        Size: <?= $item['size'] ?> |
                        Garlic: <?= $item['garlic'] ?> |
                        Spice: <?= $item['spice'] ?>
                    </span>
                </div>
            </div>
        <?php endforeach; ?>

        <div class="summary-line">
            <span>Subtotal</span>
            <span>₹<?= $display_total ?></span>
        </div>

        <div class="summary-total">
            ₹<?= $display_total ?>
        </div>

        <button form="checkout-form" type="submit" class="checkout-btn">
            Continue to Payment →
        </button>
    </div>

</div>

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