<?php
/**
 * Customer Orders Page
 * Digital Product Store
 */

require_once __DIR__ . '/config/database.php';

$page_title = 'My Orders';

// Get email from query or session
$email = isset($_GET['email']) ? sanitize_email($_GET['email']) : (isset($_SESSION['customer_email']) ? $_SESSION['customer_email'] : '');

$orders = [];
if ($email && is_valid_email($email)) {
    $_SESSION['customer_email'] = $email;
    
    $stmt = $pdo->prepare("SELECT * FROM orders WHERE customer_email = ? ORDER BY created_at DESC");
    $stmt->execute([$email]);
    $orders = $stmt->fetchAll();
}

include __DIR__ . '/includes/header.php';
?>

<div class="container py-5">
    <h1 class="mb-4">My Orders</h1>
    
    <?php if (empty($email)): ?>
    <div class="row justify-content-center">
        <div class="col-md-6">
            <div class="card border-0 shadow-sm">
                <div class="card-body text-center">
                    <i class="fas fa-envelope fa-3x text-primary mb-3"></i>
                    <h4>Enter Your Email</h4>
                    <p class="text-muted">Enter the email address you used for your orders.</p>
                    <form method="GET">
                        <div class="input-group mb-3">
                            <input type="email" name="email" class="form-control" placeholder="your@email.com" required>
                            <button type="submit" class="btn btn-primary">View Orders</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <?php elseif (empty($orders)): ?>
    <div class="text-center py-5">
        <i class="fas fa-box-open fa-4x text-muted mb-3"></i>
        <h3>No orders found</h3>
        <p class="text-muted">We couldn't find any orders for <?php echo sanitize($email); ?>.</p>
        <a href="products.php" class="btn btn-primary">Start Shopping</a>
    </div>
    <?php else: ?>
    <div class="alert alert-info">
        Showing orders for: <strong><?php echo sanitize($email); ?></strong>
        <a href="orders.php" class="float-end">Change email</a>
    </div>
    
    <div class="row">
        <?php foreach ($orders as $order): 
            $items = get_order_items($order['id']);
        ?>
        <div class="col-12 mb-4">
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-white d-flex justify-content-between align-items-center">
                    <div>
                        <strong>Order #<?php echo $order['order_number']; ?></strong>
                        <span class="text-muted ms-2"><?php echo format_datetime($order['created_at']); ?></span>
                    </div>
                    <span class="badge bg-<?php echo $order['payment_status'] === 'completed' ? 'success' : ($order['payment_status'] === 'pending' ? 'warning' : 'danger'); ?>">
                        <?php echo ucfirst($order['payment_status']); ?>
                    </span>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-sm mb-0">
                            <thead>
                                <tr>
                                    <th>Product</th>
                                    <th>Price</th>
                                    <th>Qty</th>
                                    <th>Total</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($items as $item): ?>
                                <tr>
                                    <td><?php echo sanitize($item['product_name']); ?></td>
                                    <td><?php echo format_price($item['price']); ?></td>
                                    <td><?php echo $item['quantity']; ?></td>
                                    <td><?php echo format_price($item['price'] * $item['quantity']); ?></td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                            <tfoot class="table-light">
                                <tr>
                                    <td colspan="3" class="text-end"><strong>Total:</strong></td>
                                    <td><strong><?php echo format_price($order['total']); ?></strong></td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                    
                    <?php if ($order['payment_status'] === 'completed'): ?>
                    <div class="mt-3">
                        <a href="download.php?order=<?php echo $order['order_number']; ?>" class="btn btn-success btn-sm">
                            <i class="fas fa-download"></i> Download Products
                        </a>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>
</div>

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