<?php
session_start();
include('includes/dbcon.php'); // Include your database connection

// Check if the user is logged in
$is_logged_in = isset($_SESSION['rm_id']); // Adjust this based on your session variable
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Select Customer</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- FontAwesome for icons -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        .customer-form {
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 10px;
            background-color: #f9f9f9;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="my-5">Select Customer</h1>
        <div class="customer-form">
            <?php if ($is_logged_in): ?>
                <!-- Logged-in user: Display customer selection dropdown -->
                <form action="checkout.php" method="POST">
                    <div class="mb-3">
                        <label for="customer" class="form-label">Select Customer</label>
                        <select class="form-select" id="customer" name="cus_id" required>
                            <option value="">Select a customer</option>
                            <?php
                            // Fetch customers from the database
                            $query = "SELECT cus_id, cus_name FROM customer_master";
                            $result = mysqli_query($con, $query);
                            while ($row = mysqli_fetch_assoc($result)) {
                                echo "<option value='{$row['cus_id']}'>{$row['cus_name']}</option>";
                            }
                            ?>
                        </select>
                    </div>
                    <button type="submit" class="btn btn-success">
                        <i class="fas fa-shopping-cart"></i> Proceed to Checkout
                    </button>
                </form>
            <?php else: ?>
                <!-- Guest user: Display customer details form -->
                <form action="checkout.php" method="POST">
                    <div class="mb-3">
                        <label for="cus_name" class="form-label">Full Name</label>
                        <input type="text" class="form-control" id="cus_name" name="cus_name" required>
                    </div>
                    <div class="mb-3">
                        <label for="cus_email" class="form-label">Email Address</label>
                        <input type="email" class="form-control" id="cus_email" name="cus_email" required>
                    </div>
                    <div class="mb-3">
                        <label for="cus_contact" class="form-label">Contact Number</label>
                        <input type="text" class="form-control" id="cus_contact" name="cus_contact" required>
                    </div>
                    <div class="mb-3">
                        <label for="cus_address" class="form-label">Address</label>
                        <textarea class="form-control" id="cus_address" name="cus_address" rows="3" required></textarea>
                    </div>
                    <button type="submit" class="btn btn-success">
                        <i class="fas fa-shopping-cart"></i> Proceed to Checkout
                    </button>
                </form>
            <?php endif; ?>
        </div>
    </div>

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>