<?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 = 'Manage Orders';
$orderModel = new Order();

// Handle order status updates
$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['action']) && $_POST['action'] === 'update_status') {
        $orderId = (int)$_POST['order_id'];
        $status = sanitize($_POST['status']);
        $result = $orderModel->updateStatus($orderId, $status);
        $message = 'Order status updated successfully!';
    }
}

// Get orders
$status = isset($_GET['status']) ? $_GET['status'] : null;
$search = isset($_GET['search']) ? $_GET['search'] : null;
$orders = $orderModel->getAll($status, $search);

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

<!-- Admin Orders Page -->
<div class="admin-header">
    <h1>Manage Orders</h1>
</div>

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

<!-- Filters -->
<div class="filters">
    <select class="filter-select" onchange="filterStatus(this.value)">
        <option value="">All Status</option>
        <option value="pending" <?php echo $status === 'pending' ? 'selected' : ''; ?>>Pending</option>
        <option value="processing" <?php echo $status === 'processing' ? 'selected' : ''; ?>>Processing</option>
        <option value="shipped" <?php echo $status === 'shipped' ? 'selected' : ''; ?>>Shipped</option>
        <option value="delivered" <?php echo $status === 'delivered' ? 'selected' : ''; ?>>Delivered</option>
        <option value="cancelled" <?php echo $status === 'cancelled' ? 'selected' : ''; ?>>Cancelled</option>
    </select>
    
    <form method="GET" style="display: flex; gap: var(--spacing-sm);">
        <?php if ($status): ?>
            <input type="hidden" name="status" value="<?php echo htmlspecialchars($status); ?>">
        <?php endif; ?>
        <input type="text" name="search" class="search-input" placeholder="Search by order #, name, email..." value="<?php echo htmlspecialchars($search ?? ''); ?>">
        <button type="submit" class="btn btn-secondary">Search</button>
    </form>
</div>

<!-- Orders Table -->
<div class="admin-card">
    <table class="admin-table">
        <thead>
            <tr>
                <th>Order #</th>
                <th>Customer</th>
                <th>Amount</th>
                <th>Payment</th>
                <th>Status</th>
                <th>Date</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($orders as $order): ?>
                <tr>
                    <td><?php echo htmlspecialchars($order['order_number']); ?></td>
                    <td>
                        <?php echo htmlspecialchars($order['full_name'] ?? 'Guest'); ?><br>
                        <small style="color: var(--medium-gray);"><?php echo htmlspecialchars($order['email'] ?? ''); ?></small>
                    </td>
                    <td><?php echo formatPrice($order['total_amount']); ?></td>
                    <td><span class="status-badge status-<?php echo $order['payment_status'] === 'paid' ? 'delivered' : 'pending'; ?>"><?php echo htmlspecialchars($order['payment_status']); ?></span></td>
                    <td>
                        <form method="POST" style="display: flex; gap: var(--spacing-xs);">
                            <input type="hidden" name="action" value="update_status">
                            <input type="hidden" name="order_id" value="<?php echo $order['id']; ?>">
                            <select name="status" style="padding: var(--spacing-xs); font-size: 0.75rem;">
                                <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>
                            <button type="submit" class="btn btn-small">Update</button>
                        </form>
                    </td>
                    <td><?php echo date('M j, Y', strtotime($order['created_at'])); ?></td>
                    <td><a href="order.php?id=<?php echo $order['id']; ?>" class="btn btn-small">View</a></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    
    <?php if (empty($orders)): ?>
        <p style="text-align: center; padding: var(--spacing-xl); color: var(--medium-gray);">No orders found.</p>
    <?php endif; ?>
</div>

<script>
function filterStatus(status) {
    if (status) {
        window.location.href = '?status=' + status;
    } else {
        window.location.href = 'orders.php';
    }
}
</script>

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