<?php
/**
 * Payment Success Page - Premium Version
 * Digital Product Store
 */

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

$page_title = 'Payment Successful';

// Get order
$order_number = $_GET['order'] ?? '';

if (empty($order_number)) {
    header('Location: ' . BASE_URL . '/index.php');
    exit;
}

$order = get_order_by_number($order_number);

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

// Check if order belongs to current user
if ($order['user_id'] != get_current_user_id()) {
    set_flash_message('Unauthorized access.', 'error');
    header('Location: ' . BASE_URL . '/index.php');
    exit;
}

// Get order items
$items = get_order_items($order['id']);

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

<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-lg-8">
            <div class="card border-0 shadow-lg text-center">
                <div class="card-body p-5">
                    <div class="mb-4">
                        <div class="bg-success rounded-circle d-inline-flex align-items-center justify-content-center" style="width: 100px; height: 100px;">
                            <i class="fas fa-check fa-4x text-white"></i>
                        </div>
                    </div>
                    
                    <h1 class="display-5 fw-bold text-success mb-3">Payment Successful!</h1>
                    <p class="lead text-muted mb-4">Thank you for your purchase. Your order has been confirmed.</p>
                    
                    <div class="bg-light rounded-3 p-4 mb-4">
                        <h5 class="mb-3">Order Details</h5>
                        <div class="row">
                            <div class="col-md-6 text-md-end border-end">
                                <p class="mb-1"><strong>Order Number:</strong></p>
                                <p class="mb-1"><strong>Date:</strong></p>
                                <p class="mb-1"><strong>Total:</strong></p>
                                <p class="mb-0"><strong>Status:</strong></p>
                            </div>
                            <div class="col-md-6 text-md-start">
                                <p class="mb-1"><?php echo $order['order_number']; ?></p>
                                <p class="mb-1"><?php echo format_datetime($order['created_at']); ?></p>
                                <p class="mb-1"><?php echo format_price($order['total']); ?></p>
                                <p class="mb-0"><span class="badge bg-success">Completed</span></p>
                            </div>
                        </div>
                    </div>
                    
                    <div class="mb-4">
                        <h5 class="mb-3">What You've Purchased</h5>
                        <div class="list-group list-group-flush text-start">
                            <?php foreach ($items as $item): ?>
                            <div class="list-group-item d-flex justify-content-between align-items-center">
                                <div>
                                    <strong><?php echo htmlspecialchars($item['product_name']); ?></strong>
                                    <small class="text-muted d-block">Quantity: <?php echo $item['quantity']; ?></small>
                                </div>
                                <span class="badge bg-primary"><?php echo format_price($item['price'] * $item['quantity']); ?></span>
                            </div>
                            <?php endforeach; ?>
                        </div>
                    </div>
                    
                    <div class="d-grid gap-3 d-md-flex justify-content-md-center">
                        <a href="<?php echo BASE_URL; ?>/account/downloads.php" class="btn btn-primary btn-lg">
                            <i class="fas fa-download me-2"></i>Download Products
                        </a>
                        <a href="<?php echo BASE_URL; ?>/account/orders.php" class="btn btn-outline-primary btn-lg">
                            <i class="fas fa-list me-2"></i>View Orders
                        </a>
                    </div>
                    
                    <div class="mt-4">
                        <p class="text-muted mb-0">
                            <i class="fas fa-envelope me-2"></i>
                            A confirmation email has been sent to <?php echo htmlspecialchars($order['customer_email']); ?>
                        </p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

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