<?php
session_start();
require 'config/db.php';
require_once 'includes/functions.php'; // Essential for price conversion
include 'includes/header.php';

// Security Check
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php"); exit();
}

$user_id = $_SESSION['user_id'];

// FETCH ALL ORDERS (No Limit)
$stmt = $pdo->prepare("SELECT * FROM orders WHERE user_id = ? ORDER BY created_at DESC");
$stmt->execute([$user_id]);
$orders = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="container" style="padding-top: 40px; padding-bottom: 60px; min-height: 80vh;">
    
    <a href="profile.php" class="btn" style="background: #95a5a6; color: white; padding: 8px 15px; border-radius: 4px; font-size: 14px; text-decoration:none;">&larr; Back to Dashboard</a>
    
    <h1 style="margin-top: 20px; border-bottom: 2px solid #eee; padding-bottom: 15px;">My Order History</h1>

    <?php if (count($orders) > 0): ?>
        
        <?php foreach ($orders as $order): ?>
            <div style="background: #fff; border-radius: 8px; padding: 20px; margin-bottom: 20px; border: 1px solid #eee; border-left: 5px solid #ddd; box-shadow: 0 2px 5px rgba(0,0,0,0.05); border-left-color: <?php echo ($order['status']=='completed'?'#2ecc71':($order['status']=='processing'?'#3498db':'#f1c40f')); ?>;">
                
                <div style="display: flex; justify-content: space-between; flex-wrap: wrap; align-items: center; border-bottom: 1px solid #eee; padding-bottom: 10px; margin-bottom: 10px;">
                    <div>
                        <h3 style="margin: 0; font-size: 18px;">Order #<?php echo $order['id']; ?></h3>
                        <small style="color: #777;">
                            <?php echo date('d M Y, h:i A', strtotime($order['created_at'])); ?>
                        </small>
                    </div>
                    <div style="text-align: right;">
                        <span style="font-weight: bold; font-size: 18px; color: #2c3e50;">
                            <?php echo convertPrice($order['total_amount']); ?>
                        </span>
                        <br>
                        <span style="font-size: 12px; font-weight: bold; text-transform: uppercase; color: <?php echo ($order['status']=='completed'?'#27ae60':'#e67e22'); ?>">
                            <?php echo ucfirst($order['status']); ?>
                        </span>
                    </div>
                </div>

                <div style="margin-bottom: 15px;">
                    <?php 
                    $iStmt = $pdo->prepare("
                        SELECT p.name, v.name as variant 
                        FROM order_items oi 
                        JOIN products p ON oi.product_id = p.id 
                        JOIN product_variants v ON oi.variant_id = v.id 
                        WHERE oi.order_id = ?
                    ");
                    $iStmt->execute([$order['id']]);
                    $items = $iStmt->fetchAll();
                    foreach($items as $item) {
                        echo "<div style='color:#555; font-size:15px; margin-bottom:5px;'>
                                <i class='fas fa-box-open' style='color:#3498db; width:20px;'></i> " . 
                                htmlspecialchars($item['name']) . " (" . htmlspecialchars($item['variant']) . ")
                              </div>";
                    }
                    ?>
                </div>

                <?php if (!empty($order['user_note'])): ?>
                    <div style="background: #f0f9eb; padding: 15px; border: 1px dashed #2ecc71; margin-top: 10px; font-family: monospace; color: #333; border-radius: 5px; word-break: break-all;">
                        <strong style="color:#27ae60; display:block; margin-bottom:5px;"><i class="fas fa-key"></i> License / Details:</strong>
                        <?php echo nl2br(htmlspecialchars($order['user_note'])); ?>
                    </div>
                <?php endif; ?>
                
                <div style="margin-top: 15px; text-align: right; display: flex; justify-content: flex-end; gap: 10px;">
                    <?php if ($order['status'] == 'completed'): ?>
                        <a href="invoice.php?id=<?php echo $order['id']; ?>" target="_blank" style="font-size: 13px; color: #27ae60; text-decoration: none; font-weight: bold; border: 1px solid #27ae60; padding: 6px 12px; border-radius: 5px;">
                            <i class="fas fa-file-invoice"></i> Invoice
                        </a>
                    <?php endif; ?>
                    <a href="support.php?order_id=<?php echo $order['id']; ?>" style="font-size: 13px; color: #3498db; text-decoration: none; font-weight: bold; border: 1px solid #3498db; padding: 6px 12px; border-radius: 5px;">
                        Report Issue
                    </a>
                </div>

            </div>
        <?php endforeach; ?>

    <?php else: ?>
        <div style="text-align: center; padding: 50px; background: #f9f9f9; border-radius: 10px;">
            <p style="color: #777; font-size: 18px;">No orders found.</p>
            <a href="index.php" class="btn btn-primary" style="width:auto; display:inline-block;">Start Shopping</a>
        </div>
    <?php endif; ?>

</div>

<?php include 'includes/footer.php'; ?>