<?php
/**
 * Tom's Java Jive - Customer Orders
 */

$pageTitle = "My Orders - Tom's Java Jive";
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/auth.php';

CustomerAuth::require();
$customer = CustomerAuth::getFullUser();
$currentPage = 'orders';

// Pagination
$page = max(1, intval($_GET['page'] ?? 1));
$status = $_GET['status'] ?? '';

$where = 'customer_id = :id';
$params = ['id' => $customer['customer_id']];

if ($status) {
    $where .= ' AND order_status = :status';
    $params['status'] = $status;
}

$total = db()->count('orders', $where, $params);
$pagination = paginate($total, $page, 10);

$orders = db()->fetchAll(
    "SELECT * FROM orders WHERE {$where} ORDER BY created_at DESC LIMIT :limit OFFSET :offset",
    array_merge($params, ['limit' => $pagination['per_page'], 'offset' => $pagination['offset']])
);

$extraHead = '<link rel="stylesheet" href="/assets/css/account.css?v='. filemtime(__DIR__ . '/../assets/css/account.css') .'">';
require_once __DIR__ . '/../includes/header.php';
require_once __DIR__ . '/includes/sidebar.php';
?>

<div class="account-header">
    <h1>My Orders</h1>
    <p class="text-muted">View and track your order history</p>
</div>

<!-- Filter -->
<div class="section-card">
    <div class="section-card-body" style="padding: 1rem 1.5rem;">
        <form method="GET" style="display: flex; gap: 1rem; flex-wrap: wrap; align-items: center;">
            <select name="status" class="form-select" style="width: auto;" onchange="this.form.submit()">
                <option value="">All Orders</option>
                <option value="pending" <?= $status === 'pending' ? 'selected' : '' ?>>Pending</option>
                <option value="confirmed" <?= $status === 'confirmed' ? 'selected' : '' ?>>Confirmed</option>
                <option value="processing" <?= $status === 'processing' ? 'selected' : '' ?>>Processing</option>
                <option value="shipped" <?= $status === 'shipped' ? 'selected' : '' ?>>Shipped</option>
                <option value="delivered" <?= $status === 'delivered' ? 'selected' : '' ?>>Delivered</option>
                <option value="cancelled" <?= $status === 'cancelled' ? 'selected' : '' ?>>Cancelled</option>
            </select>
            <span class="text-muted"><?= $total ?> orders found</span>
        </form>
    </div>
</div>

<?php if (empty($orders)): ?>
    <div class="section-card">
        <div class="section-card-body text-center" style="padding: 3rem;">
            <i class="fas fa-shopping-bag" style="font-size: 3rem; color: var(--color-text-muted); margin-bottom: 1rem;"></i>
            <h3>No orders yet</h3>
            <p class="text-muted">Start shopping to see your orders here!</p>
            <a href="/shop.php" class="btn btn-primary mt-1">Browse Products</a>
        </div>
    </div>
<?php else: ?>
    <?php foreach ($orders as $order): 
        $items = json_decode($order['items'], true);
        $statusClass = match($order['order_status']) {
            'pending' => 'warning',
            'confirmed', 'processing' => 'primary',
            'shipped' => 'primary',
            'delivered' => 'success',
            'cancelled', 'refunded' => 'error',
            default => 'primary'
        };
    ?>
        <div class="section-card">
            <div class="section-card-header">
                <div>
                    <strong>Order #<?= htmlspecialchars($order['order_number']) ?></strong>
                    <span class="text-muted" style="margin-left: 1rem;"><?= formatDate($order['created_at']) ?></span>
                </div>
                <span class="badge badge-<?= $statusClass ?>"><?= ucfirst($order['order_status']) ?></span>
            </div>
            <div class="section-card-body">
                <div style="display: flex; gap: 1rem; flex-wrap: wrap;">
                    <?php foreach (array_slice($items, 0, 4) as $item): ?>
                        <div style="display: flex; align-items: center; gap: 0.75rem; background: var(--color-background); padding: 0.5rem 1rem; border-radius: var(--radius-md);">
                            <span><?= $item['quantity'] ?>x</span>
                            <span><?= htmlspecialchars(truncate($item['name'], 30)) ?></span>
                        </div>
                    <?php endforeach; ?>
                    <?php if (count($items) > 4): ?>
                        <div style="padding: 0.5rem 1rem; color: var(--color-text-muted);">
                            +<?= count($items) - 4 ?> more items
                        </div>
                    <?php endif; ?>
                </div>
                
                <div style="display: flex; justify-content: space-between; align-items: center; margin-top: 1rem; padding-top: 1rem; border-top: 1px solid var(--color-border);">
                    <div>
                        <span class="text-muted">Total:</span>
                        <strong style="font-size: 1.125rem; margin-left: 0.5rem;"><?= formatCurrency($order['total']) ?></strong>
                    </div>
                    <div style="display: flex; gap: 0.5rem;">
                        <?php if ($order['tracking_number']): ?>
                            <a href="<?= htmlspecialchars($order['tracking_url'] ?? '#') ?>" target="_blank" class="btn btn-sm btn-secondary">
                                <i class="fas fa-truck"></i> Track
                            </a>
                        <?php endif; ?>
                        <a href="/account/order.php?id=<?= $order['order_id'] ?>" class="btn btn-sm btn-primary">
                            View Details
                        </a>
                    </div>
                </div>
            </div>
        </div>
    <?php endforeach; ?>
    
    <?php if ($pagination['total_pages'] > 1): ?>
        <div style="display: flex; justify-content: center; gap: 0.5rem; margin-top: 2rem;">
            <?php for ($i = 1; $i <= $pagination['total_pages']; $i++): ?>
                <a href="/account/orders.php?page=<?= $i ?><?= $status ? '&status=' . $status : '' ?>" 
                   class="btn <?= $i === $page ? 'btn-primary' : 'btn-secondary' ?>">
                    <?= $i ?>
                </a>
            <?php endfor; ?>
        </div>
    <?php endif; ?>
<?php endif; ?>

<?php require_once __DIR__ . '/includes/footer.php'; ?>
