<?php 
include 'header.php'; 
require_once __DIR__ . '/../db/config.php';

$driver_id = $_SESSION['driver_id'];
$pdo = db();

$stmt = $pdo->prepare(
    'SELECT ' .
    'o.id as order_id, ' .
    'o.status as order_status, ' .
    'o.delivery_address, ' .
    'u.name as customer_name, ' .
    'r.name as restaurant_name, ' .
    'r.address as restaurant_address ' .
    'FROM orders o ' .
    'JOIN driver_assignments da ON o.id = da.order_id ' .
    'JOIN users u ON o.user_id = u.id ' .
    'JOIN restaurants r ON o.restaurant_id = r.id ' .
    'WHERE da.driver_id = ? ' .
    'ORDER BY o.created_at DESC'
);
$stmt->execute([$driver_id]);
$assigned_orders = $stmt->fetchAll();

$order_statuses = ['preparing', 'out for delivery', 'delivered', 'cancelled'];

?>

<main class="container">
    <h1>My Assigned Deliveries</h1>

    <?php if (isset($_GET['success'])): ?>
        <p class="success-message"><?php echo htmlspecialchars($_GET['success']); ?></p>
    <?php endif; ?>
    <?php if (isset($_GET['error'])): ?>
        <p class="error-message"><?php echo htmlspecialchars($_GET['error']); ?></p>
    <?php endif; ?>

    <div class="order-list">
        <?php if (empty($assigned_orders)): ?>
            <p>You have no assigned orders at the moment.</p>
        <?php else: ?>
            <?php foreach ($assigned_orders as $order): ?>
                <div class="order-card">
                    <h3>Order #<?php echo htmlspecialchars($order['order_id']); ?></h3>
                    <p><strong>Status:</strong> <?php echo htmlspecialchars(ucwords($order['order_status'])); ?></p>
                    <hr>
                    <p><strong>Customer:</strong> <?php echo htmlspecialchars($order['customer_name']); ?></p>
                    <p><strong>Delivery Address:</strong> <?php echo htmlspecialchars($order['delivery_address']); ?></p>
                    <hr>
                    <p><strong>Restaurant:</strong> <?php echo htmlspecialchars($order['restaurant_name']); ?></p>
                    <p><strong>Restaurant Address:</strong> <?php echo htmlspecialchars($order['restaurant_address']); ?></p>
                    
                    <form action="update_order_status.php" method="POST" class="status-update-form">
                        <input type="hidden" name="order_id" value="<?php echo $order['order_id']; ?>">
                        <div class="form-group">
                            <label for="status-<?php echo $order['order_id']; ?>">Update Status:</label>
                            <select name="status" id="status-<?php echo $order['order_id']; ?>">
                                <?php foreach ($order_statuses as $status): ?>
                                    <option value="<?php echo $status; ?>" <?php echo ($order['order_status'] === $status) ? 'selected' : ''; ?>>
                                        <?php echo ucwords($status); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <button type="submit" class="btn-submit">Update</button>
                    </form>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
</main>

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