<?php
session_start();
include 'db.php';

if(empty($_SESSION['cart'])){
    echo "Cart empty";
    exit;
}

// CALCULATE TOTAL
$total = 0;
$products = [];

foreach($_SESSION['cart'] as $id => $qty){
    $p = $conn->query("SELECT * FROM products WHERE id=$id")->fetch_assoc();
    $price = $p['sale_price'] ?: $p['price'];
    $sub = $price * $qty;
    $total += $sub;

    $products[] = [
        'name' => $p['pro_title'],
        'qty' => $qty,
        'price' => $price,
        'sub' => $sub
    ];
}

// TAX (example)
$tax = round($total * 0.03,2);
$grand = $total + $tax;

include 'include/header.php'; // ✅ include after session start

?>


<style>
.checkout-container {
    max-width: 1200px;
    margin: auto;
    padding: 30px;
}

.box {
    background: white;
    padding: 20px;
    border-radius: 10px;
}

.input {
    width: 100%;
    padding: 12px;
    border: 1px solid #ddd;
    border-radius: 8px;
    margin-bottom: 12px;
}

.right-box {
    background: #fafafa;
    padding: 20px;
    border-radius: 10px;
}

.checkout-btn {
    width: 100%;
    padding: 15px;
    background: black;
    color: white;
    border-radius: 30px;
    border: none;
}
</style>


<div class="checkout-container">
    <div class="row">

        <!-- LEFT FORM -->
        <div class="col-md-7">
            <div class="box">

                <h4>Contact</h4>
                <input type="email" id="email" class="input" placeholder="Email">

                <h4>Delivery</h4>
                <div class="row">
                    <div class="col-md-6">
                        <input type="text" id="fname" class="input" placeholder="First name">
                    </div>
                    <div class="col-md-6">
                        <input type="text" id="lname" class="input" placeholder="Last name">
                    </div>
                    <div class="col-md-12">
                        <input type="text" id="address" class="input" placeholder="Address">
                    </div>
                    <div class="col-md-4">
                        <input type="text" id="city" class="input" placeholder="City">

                    </div>
                    <div class="col-md-4">
                        <input type="text" id="state" class="input" placeholder="State">

                    </div>
                    <div class="col-md-4">
                        <input type="text" id="pin" class="input" placeholder="PIN code">

                    </div>
                </div>
                <input type="text" id="phone" class="input" placeholder="Phone">

                <button onclick="sendWhatsApp()" class="checkout-btn mt-3">
                    Continue
                </button>

            </div>
        </div>

        <!-- RIGHT SUMMARY -->
        <div class="col-md-5">
            <div class="right-box">

                <?php foreach($products as $p): ?>
                <div class="d-flex justify-content-between mb-2">
                    <div><?= $p['name'] ?> x <?= $p['qty'] ?></div>
                    <div>₹<?= $p['sub'] ?></div>
                </div>
                <?php endforeach; ?>

                <hr>

                <div class="d-flex justify-content-between">
                    <span>Subtotal</span>
                    <span>₹<?= $total ?></span>
                </div>

                <div class="d-flex justify-content-between">
                    <span>Tax</span>
                    <span>₹<?= $tax ?></span>
                </div>

                <hr>

                <div class="d-flex justify-content-between">
                    <strong>Total</strong>
                    <strong>₹<?= $grand ?></strong>
                </div>

            </div>
        </div>

    </div>
</div>

<script>
function sendWhatsApp() {

    let name = document.getElementById("fname").value + " " + document.getElementById("lname").value;
    let phone = document.getElementById("phone").value;
    let address = document.getElementById("address").value;
    let city = document.getElementById("city").value;
    let state = document.getElementById("state").value;
    let pin = document.getElementById("pin").value;

    let msg = "*New Order* %0A%0A";

    msg += " Name: " + name + "%0A";
    msg += " Phone: " + phone + "%0A";
    msg += "Address: " + address + ", " + city + ", " + state + " - " + pin + "%0A%0A";

    msg += "*Order Details* %0A";

    <?php foreach($products as $p): ?>
    msg += "<?= $p['name'] ?> (x<?= $p['qty'] ?>) = ₹<?= $p['sub'] ?>%0A";
    <?php endforeach; ?>

    msg += "%0A Total: ₹<?= $grand ?>";

    let url = "https://wa.me/918617886268?text=" + msg;

    window.open(url, '_blank');
}
</script>
<?php include 'include/footer.php'; ?>