<?php
session_start();
include "../config/db.php";

if (!isset($_SESSION['admin_id'])) {
    header("Location: login.php");
    exit();
}

$order_id = mysqli_real_escape_string($conn, $_GET['id']);

// Get order details
$order = mysqli_fetch_assoc(mysqli_query($conn,
    "SELECT orders.*, users.name, users.email, users.phone, users.address
     FROM orders
     JOIN users ON orders.user_id = users.id
     WHERE orders.id='$order_id'"
));

// Get order items
$items = mysqli_query($conn,
    "SELECT * FROM order_items WHERE order_id='$order_id'"
);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Order Details - Admin Panel</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="../assets/css/style.css" rel="stylesheet">
    <style>
        /* Replaced sidebar CSS */
        .main-content {
            margin-left: 220px;
            padding: 30px;
            background: #f8f9fa;
            min-height: 100vh;
        }
        .order-details-container {
            background: white;
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
        }
        .status-badge {
            padding: 5px 15px;
            border-radius: 20px;
            font-size: 0.8rem;
            font-weight: 600;
        }
        .status-pending { background: #fff3cd; color: #856404; }
        .status-paid { background: #d4edda; color: #155724; }
        .status-shipped { background: #cce5ff; color: #004085; }
        .status-delivered { background: #d1ecf1; color: #0c5460; }
        .status-cancelled { background: #f8d7da; color: #721c24; }
        .status-returned { background: #f3e5f5; color: #7b1fa2; }
        .status-inuse { background: #fff4e5; color: #d9480f; }
    </style>
</head>
<body>
    <!-- Sidebar -->
    <?php include "includes/sidebar.php"; ?>

    <div class="main-content">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2 style="color: var(--dark-blue);">
                <i class="fas fa-file-invoice me-2"></i>Order Details
            </h2>
            <a href="orders.php" class="btn btn-outline-primary">
                <i class="fas fa-arrow-left me-2"></i>Back to Orders
            </a>
        </div>

        <?php if($order): ?>
        <div class="order-details-container">
            <div class="row mb-4">
                <div class="col-md-6">
                    <h4>Order #<?php echo str_pad($order['id'], 6, '0', STR_PAD_LEFT); ?></h4>
                    <p class="text-muted">
                        Date: <?php echo date('F j, Y, h:i A', strtotime($order['created_at'])); ?>
                    </p>
                </div>
                <div class="col-md-6 text-end">
                    <span class="status-badge 
                        <?php 
                        switch($order['status']) {
                            case 'Pending': echo 'status-pending'; break;
                            case 'Paid': echo 'status-paid'; break;
                            case 'Shipped': echo 'status-shipped'; break;
                            case 'Delivered': echo 'status-delivered'; break;
                            case 'Cancelled': echo 'status-cancelled'; break;
                            case 'Returned': echo 'status-returned'; break;
                            case 'In Use': echo 'status-inuse'; break;
                        }
                        ?>">
                        <?php echo $order['status'] ?? 'Pending'; ?>
                    </span>
                </div>
            </div>

            <div class="row">
                <div class="col-md-6">
                    <div class="card mb-4">
                        <div class="card-header bg-light">
                            <h5 class="mb-0">Customer Information</h5>
                        </div>
                        <div class="card-body">
                            <p class="mb-2">
                                <strong>Name:</strong> <?php echo htmlspecialchars($order['name']); ?>
                            </p>
                            <p class="mb-2">
                                <strong>Email:</strong> <?php echo htmlspecialchars($order['email']); ?>
                            </p>
                            <p class="mb-2">
                                <strong>Phone:</strong> <?php echo htmlspecialchars($order['phone'] ?? 'N/A'); ?>
                            </p>
                            <?php if(!empty($order['address'])): ?>
                            <p class="mb-0">
                                <strong>Address:</strong> <?php echo htmlspecialchars($order['address']); ?>
                            </p>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>

                <div class="col-md-6">
                    <div class="card mb-4">
                        <div class="card-header bg-light">
                            <h5 class="mb-0">Order Summary</h5>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-sm">
                                    <thead>
                                        <tr>
                                            <th>Product</th>
                                            <th>Qty</th>
                                            <th>Price</th>
                                            <th>Total</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php 
                                        $subtotal = 0;
                                        while($item = mysqli_fetch_assoc($items)):
                                            $item_total = $item['price'] * $item['quantity'];
                                            $subtotal += $item_total;
                                        ?>
                                        <tr>
                                            <td><?php echo htmlspecialchars($item['product_name']); ?></td>
                                            <td><?php echo $item['quantity']; ?></td>
                                            <td>₹<?php echo number_format($item['price'], 2); ?></td>
                                            <td>₹<?php echo number_format($item_total, 2); ?></td>
                                        </tr>
                                        <?php endwhile; ?>
                                    </tbody>
                                    <tfoot>
                                        <tr>
                                            <td colspan="3" class="text-end fw-bold">Subtotal:</td>
                                            <td class="fw-bold">₹<?php echo number_format($subtotal, 2); ?></td>
                                        </tr>
                                        <tr>
                                            <td colspan="3" class="text-end fw-bold">Shipping:</td>
                                            <td class="fw-bold">₹0.00</td>
                                        </tr>
                                        <tr>
                                            <td colspan="3" class="text-end fw-bold">Total:</td>
                                            <td class="fw-bold text-success">₹<?php echo number_format($order['total_price'], 2); ?></td>
                                        </tr>
                                    </tfoot>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Order Notes -->
            <div class="card">
                <div class="card-header bg-light">
                    <h5 class="mb-0">Order Notes</h5>
                </div>
                <div class="card-body">
                    <?php if(!empty($order['special_instructions'])): ?>
                        <p><?php echo htmlspecialchars($order['special_instructions']); ?></p>
                    <?php else: ?>
                        <p class="text-muted">No special instructions provided.</p>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <?php else: ?>
            <div class="alert alert-danger">
                <i class="fas fa-exclamation-circle me-2"></i>
                Order not found!
            </div>
        <?php endif; ?>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>