<?php
/**
 * Order Details Page
 * Digital Marketplace
 */

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

require_login();

$page_title = 'Order Details';

$user_id = get_current_user_id();
$order_id = (int)($_GET['id'] ?? 0);

// Get order
$stmt = $pdo->prepare("SELECT * FROM orders WHERE id = ? AND user_id = ?");
$stmt->execute([$order_id, $user_id]);
$order = $stmt->fetch();

if (!$order) {
    set_flash_message('Order not found.', 'error');
    header('Location: ' . BASE_URL . '/account/orders.php');
    exit;
}

// Get order items
$stmt = $pdo->prepare("SELECT oi.*, p.file_path, p.download_url, p.download_limit as product_download_limit 
                      FROM order_items oi 
                      LEFT JOIN products p ON oi.product_id = p.id 
                      WHERE oi.order_id = ?");
$stmt->execute([$order_id]);
$items = $stmt->fetchAll();

// Calculate totals
$subtotal = 0;
foreach ($items as $item) {
    $subtotal += $item['subtotal'];
}

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

<section class="section">
    <div class="container">
        <div class="d-flex justify-content-between align-items-center mb-4" data-aos="fade-up">
            <div>
                <a href="<?php echo BASE_URL; ?>/account/orders.php" class="text-muted mb-2 d-inline-block">
                    <i class="fas fa-arrow-left me-1"></i>Back to Orders
                </a>
                <h1 class="mb-0"><i class="fas fa-file-invoice me-2"></i>Order #<?php echo $order['order_number']; ?></h1>
            </div>
            <div class="text-end">
                <span class="badge bg-<?php 
                    echo match($order['payment_status']) {
                        'completed' => 'success',
                        'pending' => 'warning',
                        'failed' => 'danger',
                        default => 'secondary'
                    };
                ?> fs-6">
                    <?php echo ucfirst($order['payment_status']); ?>
                </span>
            </div>
        </div>
        
        <div class="row g-4">
            <!-- Order Info -->
            <div class="col-lg-8" data-aos="fade-right">
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0"><i class="fas fa-box me-2"></i>Order Items</h5>
                    </div>
                    <div class="card-body p-0">
                        <div class="table-responsive">
                            <table class="table table-dark table-hover mb-0">
                                <thead>
                                    <tr>
                                        <th>Product</th>
                                        <th class="text-center">Price</th>
                                        <th class="text-center">Qty</th>
                                        <th class="text-end">Total</th>
                                        <?php if ($order['payment_status'] === 'completed'): ?>
                                        <th class="text-center">Download</th>
                                        <?php endif; ?>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($items as $item): ?>
                                    <tr>
                                        <td>
                                            <strong><?php echo sanitize($item['product_name']); ?></strong>
                                        </td>
                                        <td class="text-center"><?php echo format_price($item['product_price']); ?></td>
                                        <td class="text-center"><?php echo $item['quantity']; ?></td>
                                        <td class="text-end"><?php echo format_price($item['subtotal']); ?></td>
                                        <?php if ($order['payment_status'] === 'completed'): ?>
                                        <td class="text-center">
                                            <?php if ($item['download_count'] < $item['download_limit']): ?>
                                                <a href="<?php echo BASE_URL; ?>/download.php?item=<?php echo $item['id']; ?>" 
                                                   class="btn btn-sm btn-success">
                                                    <i class="fas fa-download me-1"></i>Download
                                                </a>
                                                <small class="d-block text-muted mt-1">
                                                    <?php echo $item['download_count']; ?>/<?php echo $item['download_limit']; ?> downloads
                                                </small>
                                            <?php else: ?>
                                                <span class="badge bg-secondary">Limit Reached</span>
                                            <?php endif; ?>
                                        </td>
                                        <?php endif; ?>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                
                <!-- Order Timeline -->
                <div class="card mt-4">
                    <div class="card-header">
                        <h5 class="mb-0"><i class="fas fa-history me-2"></i>Order Timeline</h5>
                    </div>
                    <div class="card-body">
                        <div class="timeline">
                            <div class="timeline-item">
                                <div class="timeline-marker bg-primary"></div>
                                <div class="timeline-content">
                                    <h6>Order Placed</h6>
                                    <p class="text-muted mb-0"><?php echo format_datetime($order['created_at']); ?></p>
                                </div>
                            </div>
                            
                            <?php if ($order['payment_status'] === 'completed'): ?>
                            <div class="timeline-item">
                                <div class="timeline-marker bg-success"></div>
                                <div class="timeline-content">
                                    <h6>Payment Confirmed</h6>
                                    <p class="text-muted mb-0">Your payment has been received</p>
                                </div>
                            </div>
                            <div class="timeline-item">
                                <div class="timeline-marker bg-info"></div>
                                <div class="timeline-content">
                                    <h6>Order Completed</h6>
                                    <p class="text-muted mb-0">Your order is ready for download</p>
                                </div>
                            </div>
                            <?php elseif ($order['payment_status'] === 'pending'): ?>
                            <div class="timeline-item">
                                <div class="timeline-marker bg-warning"></div>
                                <div class="timeline-content">
                                    <h6>Payment Pending</h6>
                                    <p class="text-muted mb-0">Waiting for payment confirmation</p>
                                </div>
                            </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Order Summary -->
            <div class="col-lg-4" data-aos="fade-left">
                <div class="card order-summary" style="position: sticky; top: 100px;">
                    <div class="card-header">
                        <h5 class="mb-0"><i class="fas fa-receipt me-2"></i>Order Summary</h5>
                    </div>
                    <div class="card-body">
                        <div class="mb-3">
                            <small class="text-muted d-block">Order Date</small>
                            <strong><?php echo format_datetime($order['created_at']); ?></strong>
                        </div>
                        
                        <div class="mb-3">
                            <small class="text-muted d-block">Payment Method</small>
                            <strong><?php echo ucfirst($order['payment_method']); ?></strong>
                        </div>
                        
                        <?php if ($order['payment_id']): ?>
                        <div class="mb-3">
                            <small class="text-muted d-block">Transaction ID</small>
                            <strong><?php echo $order['payment_id']; ?></strong>
                        </div>
                        <?php endif; ?>
                        
                        <hr class="border-secondary">
                        
                        <div class="d-flex justify-content-between mb-2">
                            <span class="text-muted">Subtotal</span>
                            <span><?php echo format_price($subtotal); ?></span>
                        </div>
                        
                        <?php if ($order['discount'] > 0): ?>
                        <div class="d-flex justify-content-between mb-2">
                            <span class="text-success">Discount</span>
                            <span class="text-success">-<?php echo format_price($order['discount']); ?></span>
                        </div>
                        <?php endif; ?>
                        
                        <?php if ($order['tax'] > 0): ?>
                        <div class="d-flex justify-content-between mb-2">
                            <span class="text-muted">Tax</span>
                            <span><?php echo format_price($order['tax']); ?></span>
                        </div>
                        <?php endif; ?>
                        
                        <?php if ($order['shipping'] > 0): ?>
                        <div class="d-flex justify-content-between mb-2">
                            <span class="text-muted">Shipping</span>
                            <span><?php echo format_price($order['shipping']); ?></span>
                        </div>
                        <?php endif; ?>
                        
                        <hr class="border-secondary">
                        
                        <div class="d-flex justify-content-between align-items-center">
                            <span class="h5 mb-0">Total</span>
                            <span class="h3 mb-0 text-primary"><?php echo format_price($order['total']); ?></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<style>
.timeline {
    position: relative;
    padding-left: 30px;
}

.timeline::before {
    content: '';
    position: absolute;
    left: 8px;
    top: 0;
    bottom: 0;
    width: 2px;
    background: var(--dark-lighter);
}

.timeline-item {
    position: relative;
    padding-bottom: 1.5rem;
}

.timeline-item:last-child {
    padding-bottom: 0;
}

.timeline-marker {
    position: absolute;
    left: -26px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    border: 3px solid var(--dark);
}
</style>

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