<?php
session_start();
require_once('../connection.php');

$first_name = $_SESSION['first_name'];
$email = $_SESSION['email'];
$customerID = $_SESSION['userID'];

$cart_items = $_SESSION['cart'] ?? [];

    if (empty($cart_items)) {
        header("Location: basket.php");
        exit();
    }

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>GLH | Checkout</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">
    
    <style>
  
        .navbar-container {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 20px 5px; 
            border-bottom: 1px solid #ddd;
            background-color: #90d5ff;
        }
        
        .logo { 
            display: block;
            height: 40px;
            width: auto;
        }
        
        .navbar {
            display: flex;
            flex-direction: row;
            flex-wrap: nowrap; 
            gap: 80px;           
        }
        
        .navbar a {
            text-decoration: none;
            color: black;
            position: relative;
            padding-bottom: 2px;
        }
        
        .navbar a::after{
            content: '';
            position: absolute;
            width: 100%;
            transform: scaleX(0);
            height: 2px;
            bottom: 0;
            left: 0;
            background-color: black;
            transition: transform 0.25s ease-out;
        }

        .navbar a:hover::after {
            transform: scaleX(1);
        }
        
        .account {
            justify-content: flex-end;
        }
        
        .account button {
            background: white;
            border: 0px solid #ccc;
            padding: 5px 15px;
            border-radius: 5px;
            cursor: pointer;
        }
        
        .account button:hover {
            background-color: #f0f0f0;
            transition: 0.3s;
        }
        
        
        .logo, .account {
            flex: 1; 
             display: flex;
        }
        

        @media (max-width: 576px) {
            .navbar a {
            font-size: 14px;
            }
            
            .navbar {
                gap: 20px
            }
            
            .logo img {
                width: 90px;
                height: auto;
            }
            
        }
        
        .billing-details {
            display: flex;
            justify-content: center;
            align-items: center;    
            min-height: 80vh;   
        }

        .form-card {
            background: white;
            padding: 70px;
            border-radius: 7px;
            box-shadow: 0 8px 8px rgba(0,0,0,0.1);
            text-align: center;
        }
    
        .form-card input {
            display: block;
            width: 100%;
            margin-bottom: -30px;
            padding: 7px;
            box-sizing: border-box;
            border-radius: 5px;
        }
    
        .submit-button {
            width: 140px !important; 
            height: 40px;
            cursor: pointer;
            display: block; 
            margin: 0 auto;
            border-color: transparent;
            background-color: #90d5ff;
        }
        
        .submit-button:hover {
            background-color: #6FB7E3;
            transition: 0.3s;
        }
        
        
    
    
        
    </style>
</head>
    <body>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js" integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI" crossorigin="anonymous"></script>
        
        
        <header class="navbar-container">
            
            <div class="logo">
                <a href="../index.php">
                    <img src="../glh.png" class="logo" alt="logo" width="50" height="15">
                </a>
            </div>
        
            <div class="navbar">
                <a class="market" href="/glh/market/market.php">Market</a>
                <a class="dashboard" href="/glh/dashboard/dashboard.php">Dashboard</a>
                <a class="basket" href="/glh/market/basket.php">Basket</a>
            </div>
            
            <div class="account">
                <a href="/glh/login/login-form.php">
                    <button>Login <i class="bi bi-person-circle"></i></button>
                </a>
            </div>
            
        </header>
            
            <div class="billing-details">
                <div class="form-card">
                <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                <h3>Billing Information</h3><br>
                    <input type="text" name="name" placeholder="Name" required><br><br>
                    <input type="text" name="address" placeholder="Address" required><br><br>
                    <input type="text" name="city" placeholder="City" required><br><br>
                    <input type="text" name="postcode" placeholder="Postcode" required><br><br>
                    
                    <input type="hidden" name="productID" value="<?php echo $productID; ?>">
                    <input type="hidden" name="product_name" value="<?php echo $product_name; ?>">
                    <input type="hidden" name="grand_total" value="<?php echo $grand_total; ?>">
                    <input type="hidden" name="quantity" value="<?php echo $quantity; ?>">
                    <input type="hidden" name="customerID" value="<?php echo $customerID; ?>">
                
                    <input type="submit" class="submit-button" name="submit" value="Purchase"><br><br>
                </form>
                </div>
            </div> 
                
                <?php
            
                    if (isset($_POST['submit'])) {
                        $productID = $_POST['productID'];
                        $product_name = $_POST['product_name'];
                        $grand_total = $_POST['grand_total'];
                        $quantity = $_POST['quantity'];
                        $customerID = $_POST['customerID'];
                        $order_date = date('Y-m-d');

                    try {

                        $stmt = $conn->prepare("INSERT INTO orders (productID, product_name, grand_total, quantity, order_date, customerID) VALUES (?, ?, ?, ?, ?, ?)");

        
                        foreach ($cart_items as $id => $quantity) {
            
            
                        $res = $conn->query("SELECT product_name, product_price FROM products WHERE productID = $id");
                        $product = $res->fetch_assoc();
            
                        $product_name = $product['product_name'];
                        $subtotal = $product['product_price'] * $quantity;


                        $stmt->bind_param("isdisi", $id, $product_name, $subtotal, $quantity, $order_date, $customerID);
                        $stmt->execute();
                    }

        
                    $_SESSION['cart'] = [];

                    echo "<script>
                            alert('Purchase successful! Your orders have been placed.');
                            window.location.href = 'market.php';
                        </script>";
                    exit();

                    } catch (Exception $e) {
                        echo "<script>alert('Error processing order: " . $e->getMessage() . "');</script>";
                    }
                }
            ?>
        
        
    </body>
</html>


        














