<?php
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/order.php';

// Check if user is admin
if (!isAdmin()) {
    header('Location: ../login.php?redirect=admin/');
    exit;
}

$pageTitle = 'Order Details';
$orderModel = new Order();

$orderId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$order = $orderModel->getById($orderId);

if (!$order) {
    header('Location: orders.php');
    exit;
}

// Handle status update
$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    if ($_POST['action'] === 'update_status') {
        $status = sanitize($_POST['status']);
        $orderModel->updateStatus($orderId, $status);
        $message = 'Order status updated!';
        $order = $orderModel->getById($orderId);
    } elseif ($_POST['action'] === 'update_payment') {
        $paymentStatus = sanitize($_POST['payment_status']);
        $orderModel->updatePaymentStatus($orderId, $paymentStatus);
        $message = 'Payment status updated!';
        $order = $orderModel->getById($orderId);
    }
}

require_once __DIR__ . '/admin_header.php';
?>

<!-- Admin Order Detail Page -->
<div class="admin-header">
    <h1>Order #<?php echo htmlspecialchars($order['order_number']); ?></h1>
    <a href="orders.php" class="btn btn-secondary">Back to Orders</a>
</div>

<?php if ($message): ?>
    <div class="alert alert-success"><?php echo htmlspecialchars($message); ?></div>
<?php endif; ?>

<div style="display: grid; grid-template-columns: 2fr 1fr; gap: var(--spacing-lg);">
    <!-- Order Items -->
    <div class="admin-card">
        <div class="admin-card-header">
            <h2>Order Items</h2>
        </div>
        
        <table class="admin-table">
            <thead>
                <tr>
                    <th>Product</th>
                    <th>Price</th>
                    <th>Qty</th>
                    <th>Total</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($order['items'] as $item): ?>
                    <tr>
                        <td>
                            <?php echo htmlspecialchars($item['product_name']); ?><br>
                            <small style="color: var(--medium-gray);">
                                <?php echo $item['size'] ? 'Size: ' . htmlspecialchars($item['size']) : ''; ?>
                                <?php echo $item['color'] ? ' | Color: ' . htmlspecialchars($item['color']) : ''; ?>
                            </small>
                        </td>
                        <td><?php echo formatPrice($item['product_price']); ?></td>
                        <td><?php echo $item['quantity']; ?></td>
                        <td><?php echo formatPrice($item['subtotal']); ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="3"><strong>Total</strong></td>
                    <td><strong><?php echo formatPrice($order['total_amount']); ?></strong></td>
                </tr>
            </tfoot>
        </table>
    </div>
    
    <!-- Order Info -->
    <div>
        <div class="admin-card">
            <div class="admin-card-header">
                <h2>Order Status</h2>
            </div>
            
            <form method="POST">
                <input type="hidden" name="action" value="update_status">
                <div class="form-group">
                    <label for="status">Status</label>
                    <select name="status" id="status">
                        <option value="pending" <?php echo $order['status'] === 'pending' ? 'selected' : ''; ?>>Pending</option>
                        <option value="processing" <?php echo $order['status'] === 'processing' ? 'selected' : ''; ?>>Processing</option>
                        <option value="shipped" <?php echo $order['status'] === 'shipped' ? 'selected' : ''; ?>>Shipped</option>
                        <option value="delivered" <?php echo $order['status'] === 'delivered' ? 'selected' : ''; ?>>Delivered</option>
                        <option value="cancelled" <?php echo $order['status'] === 'cancelled' ? 'selected' : ''; ?>>Cancelled</option>
                    </select>
                </div>
                <button type="submit" class="btn btn-primary btn-block">Update Status</button>
            </form>
            
            <hr style="margin: var(--spacing-lg) 0; border: none; border-top: 1px solid var(--lighter-gray);">
            
            <form method="POST">
                <input type="hidden" name="action" value="update_payment">
                <div class="form-group">
                    <label for="payment_status">Payment Status</label>
                    <select name="payment_status" id="payment_status">
                        <option value="pending" <?php echo $order['payment_status'] === 'pending' ? 'selected' : ''; ?>>Pending</option>
                        <option value="paid" <?php echo $order['payment_status'] === 'paid' ? 'selected' : ''; ?>>Paid</option>
                        <option value="failed" <?php echo $order['payment_status'] === 'failed' ? 'selected' : ''; ?>>Failed</option>
                    </select>
                </div>
                <button type="submit" class="btn btn-secondary btn-block">Update Payment</button>
            </form>
        </div>
        
        <div class="admin-card">
            <div class="admin-card-header">
                <h2>Customer Info</h2>
            </div>
            <p><strong>Name:</strong> <?php echo htmlspecialchars($order['full_name'] ?? 'Guest'); ?></p>
            <p><strong>Email:</strong> <?php echo htmlspecialchars($order['email'] ?? 'N/A'); ?></p>
        </div>
        
        <div class="admin-card">
            <div class="admin-card-header">
                <h2>Shipping Address</h2>
            </div>
            <p><?php echo nl2br(htmlspecialchars($order['shipping_address'])); ?></p>
        </div>
        
        <div class="admin-card">
            <div class="admin-card-header">
                <h2>Order Details</h2>
            </div>
            <p><strong>Order Date:</strong> <?php echo date('F j, Y g:i A', strtotime($order['created_at'])); ?></p>
            <p><strong>Payment Method:</strong> <?php echo htmlspecialchars($order['payment_method']); ?></p>
            <?php if ($order['notes']): ?>
                <p><strong>Notes:</strong> <?php echo htmlspecialchars($order['notes']); ?></p>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php require_once __DIR__ . '/admin_footer.php'; ?>
