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

if (!isset($_GET['id'])) {
    echo "<div class='alert alert-danger'>No order ID specified.</div>";
    require_once 'footer.php';
    exit;
}

$order_id = $_GET['id'];
$db = db();

// Handle status update
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['status'])) {
    $status = $_POST['status'];
    $update_stmt = $db->prepare("UPDATE orders SET status = :status WHERE id = :order_id");
    $update_stmt->bindParam(':status', $status);
    $update_stmt->bindParam(':order_id', $order_id);
    $update_stmt->execute();
}

// Fetch order details
$order_stmt = $db->prepare("SELECT o.*, u.name AS user_name, u.email AS user_email FROM orders o JOIN users u ON o.user_id = u.id WHERE o.id = :order_id");
$order_stmt->bindParam(':order_id', $order_id);
$order_stmt->execute();
$order = $order_stmt->fetch(PDO::FETCH_ASSOC);

if (!$order) {
    echo "<div class='alert alert-danger'>Order not found.</div>";
    require_once 'footer.php';
    exit;
}

// Fetch order items
$items_stmt = $db->prepare("SELECT oi.*, p.name AS product_name FROM order_items oi JOIN products p ON oi.product_id = p.id WHERE oi.order_id = :order_id");
$items_stmt->bindParam(':order_id', $order_id);
$items_stmt->execute();
$items = $items_stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<h2>Order Details #<?php echo htmlspecialchars($order['id']); ?></h2>

<div class="card mb-4">
    <div class="card-header">Customer & Order Info</div>
    <div class="card-body">
        <p><strong>Customer:</strong> <?php echo htmlspecialchars($order['user_name']); ?></p>
        <p><strong>Email:</strong> <?php echo htmlspecialchars($order['user_email']); ?></p>
        <p><strong>Address:</strong> <?php echo htmlspecialchars($order['delivery_address']); ?></p>
        <p><strong>Total Price:</strong> $<?php echo htmlspecialchars(number_format($order['total_price'], 2)); ?></p>
        <p><strong>Order Date:</strong> <?php echo htmlspecialchars($order['created_at']); ?></p>
        <p><strong>Status:</strong> <?php echo htmlspecialchars($order['status']); ?></p>
    </div>
</div>

<div class="card mb-4">
    <div class="card-header">Order Items</div>
    <div class="card-body">
        <table class="table">
            <thead>
                <tr>
                    <th>Product</th>
                    <th>Quantity</th>
                    <th>Price</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($items as $item): ?>
                    <tr>
                        <td><?php echo htmlspecialchars($item['product_name']); ?></td>
                        <td><?php echo htmlspecialchars($item['quantity']); ?></td>
                        <td>$<?php echo htmlspecialchars(number_format($item['price'], 2)); ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<div class="card">
    <div class="card-header">Update Status</div>
    <div class="card-body">
        <form action="order_details.php?id=<?php echo $order['id']; ?>" method="POST">
            <div class="input-group">
                <select name="status" class="form-select">
                    <option value="Pending" <?php echo $order['status'] === 'Pending' ? 'selected' : ''; ?>>Pending</option>
                    <option value="Confirmed" <?php echo $order['status'] === 'Confirmed' ? 'selected' : ''; ?>>Confirmed</option>
                    <option value="In Progress" <?php echo $order['status'] === 'In Progress' ? 'selected' : ''; ?>>In Progress</option>
                    <option value="Out for Delivery" <?php echo $order['status'] === 'Out for Delivery' ? 'selected' : ''; ?>>Out for Delivery</option>
                    <option value="Completed" <?php echo $order['status'] === 'Completed' ? 'selected' : ''; ?>>Completed</option>
                    <option value="Cancelled" <?php echo $order['status'] === 'Cancelled' ? 'selected' : ''; ?>>Cancelled</option>
                </select>
                <button type="submit" class="btn btn-primary">Update</button>
            </div>
        </form>
    </div>
</div>

<a href="index.php" class="btn btn-secondary mt-3">Back to Orders</a>

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