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

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

$page_title = 'Payment';

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

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

$order = get_order_by_number($order_number);

if (!$order) {
    set_flash_message('Order not found.', 'error');
    header('Location: ' . BASE_URL . '/cart.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 . '/cart.php');
    exit;
}

// Check if already paid
if ($order['payment_status'] === 'completed') {
    set_flash_message('This order has already been paid.', 'info');
    header('Location: ' . BASE_URL . '/account/orders.php');
    exit;
}

// Handle payment success (simulated for demo)
if (isset($_GET['status']) && $_GET['status'] === 'success') {
    // Update order status
    update_order_payment_status($order['id'], 'completed', 'demo_payment_' . time());
    
    // Send confirmation
    send_order_confirmation($order['id']);
    
    // Clear pending order
    unset($_SESSION['pending_order_id']);
    
    set_flash_message('Payment successful! Thank you for your purchase.', 'success');
    header('Location: ' . BASE_URL . '/payment-success.php?order=' . $order_number);
    exit;
}

// Handle payment cancel
if (isset($_GET['status']) && $_GET['status'] === 'cancel') {
    set_flash_message('Payment was cancelled.', 'warning');
    header('Location: ' . BASE_URL . '/checkout.php');
    exit;
}

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

<!-- Page Header -->
<section class="bg-gradient text-white py-5">
    <div class="container">
        <h1 class="display-5 fw-bold mb-0"><i class="fas fa-credit-card me-3"></i>Complete Payment</h1>
    </div>
</section>

<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-lg-6">
            <div class="card border-0 shadow-lg">
                <div class="card-header bg-white text-center py-4">
                    <div class="mb-3">
                        <i class="fas fa-lock fa-3x text-success"></i>
                    </div>
                    <h4 class="mb-1">Secure Payment</h4>
                    <p class="text-muted mb-0">Order #<?php echo $order['order_number']; ?></p>
                </div>
                <div class="card-body p-4">
                    <div class="text-center mb-4">
                        <h2 class="text-primary mb-0"><?php echo format_price($order['total']); ?></h2>
                        <p class="text-muted">Total Amount</p>
                    </div>
                    
                    <!-- Payment Form (Demo) -->
                    <form action="<?php echo BASE_URL; ?>/payment.php" method="GET">
                        <input type="hidden" name="order" value="<?php echo $order['order_number']; ?>">
                        
                        <div class="mb-3">
                            <label class="form-label">Card Number</label>
                            <div class="input-group">
                                <span class="input-group-text"><i class="fas fa-credit-card"></i></span>
                                <input type="text" class="form-control" placeholder="4242 4242 4242 4242" maxlength="19">
                            </div>
                        </div>
                        
                        <div class="row g-3 mb-3">
                            <div class="col-6">
                                <label class="form-label">Expiry Date</label>
                                <input type="text" class="form-control" placeholder="MM/YY" maxlength="5">
                            </div>
                            <div class="col-6">
                                <label class="form-label">CVV</label>
                                <div class="input-group">
                                    <input type="text" class="form-control" placeholder="123" maxlength="4">
                                    <span class="input-group-text"><i class="fas fa-question-circle"></i></span>
                                </div>
                            </div>
                        </div>
                        
                        <div class="mb-4">
                            <label class="form-label">Cardholder Name</label>
                            <input type="text" class="form-control" placeholder="John Doe">
                        </div>
                        
                        <button type="submit" name="status" value="success" class="btn btn-primary btn-lg w-100 mb-3">
                            <i class="fas fa-lock me-2"></i>Pay <?php echo format_price($order['total']); ?>
                        </button>
                        
                        <a href="<?php echo BASE_URL; ?>/payment.php?order=<?php echo $order['order_number']; ?>&status=cancel" class="btn btn-outline-secondary w-100">
                            Cancel Payment
                        </a>
                    </form>
                    
                    <div class="text-center mt-4">
                        <img src="https://via.placeholder.com/50x30?text=VISA" alt="Visa" class="me-2">
                        <img src="https://via.placeholder.com/50x30?text=MC" alt="MasterCard" class="me-2">
                        <img src="https://via.placeholder.com/50x30?text=AMEX" alt="Amex">
                    </div>
                    
                    <div class="alert alert-info mt-4 mb-0">
                        <i class="fas fa-info-circle me-2"></i>
                        <strong>Demo Mode:</strong> Click "Pay" to simulate a successful payment.
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

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