<?php
/**
 * Order Pending Page
 * Digital Product Store
 */

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

$page_title = 'Order Pending';

// Get order number
$order_number = isset($_GET['order']) ? sanitize($_GET['order']) : '';

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

// Get order details
$order = get_order_by_number($order_number);

if (!$order) {
    header('Location: index.php');
    exit;
}

// Get payment settings for bank transfer
$payment_settings = get_payment_settings('bank_transfer');
$bank_config = $payment_settings ? $payment_settings['config'] : [];

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

<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-lg-8 text-center">
            <div class="mb-4">
                <i class="fas fa-clock fa-5x text-warning"></i>
            </div>
            <h1 class="mb-3">Order Pending</h1>
            <p class="lead mb-4">Your order has been received and is pending payment confirmation.</p>
            
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-body">
                    <h5 class="mb-3">Order Details</h5>
                    <div class="row">
                        <div class="col-md-6">
                            <p class="mb-1"><strong>Order Number:</strong></p>
                            <p class="h5 text-primary">#<?php echo $order['order_number']; ?></p>
                        </div>
                        <div class="col-md-6">
                            <p class="mb-1"><strong>Total:</strong></p>
                            <p class="h5"><?php echo format_price($order['total']); ?></p>
                        </div>
                    </div>
                </div>
            </div>
            
            <?php if (!empty($bank_config)): ?>
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-body text-start">
                    <h5 class="mb-3"><i class="fas fa-university"></i> Bank Transfer Details</h5>
                    <p>Please transfer the total amount to the following account:</p>
                    
                    <table class="table table-bordered">
                        <?php if (!empty($bank_config['bank_name'])): ?>
                        <tr>
                            <th>Bank Name</th>
                            <td><?php echo sanitize($bank_config['bank_name']); ?></td>
                        </tr>
                        <?php endif; ?>
                        <?php if (!empty($bank_config['account_name'])): ?>
                        <tr>
                            <th>Account Name</th>
                            <td><?php echo sanitize($bank_config['account_name']); ?></td>
                        </tr>
                        <?php endif; ?>
                        <?php if (!empty($bank_config['account_number'])): ?>
                        <tr>
                            <th>Account Number</th>
                            <td><?php echo sanitize($bank_config['account_number']); ?></td>
                        </tr>
                        <?php endif; ?>
                        <?php if (!empty($bank_config['routing_number'])): ?>
                        <tr>
                            <th>Routing Number</th>
                            <td><?php echo sanitize($bank_config['routing_number']); ?></td>
                        </tr>
                        <?php endif; ?>
                    </table>
                    
                    <?php if (!empty($bank_config['instructions'])): ?>
                    <div class="alert alert-info">
                        <strong>Instructions:</strong><br>
                        <?php echo nl2br(sanitize($bank_config['instructions'])); ?>
                    </div>
                    <?php endif; ?>
                    
                    <div class="alert alert-warning">
                        <strong>Important:</strong> Please include your order number <strong>#<?php echo $order['order_number']; ?></strong> in the payment reference.
                    </div>
                </div>
            </div>
            <?php endif; ?>
            
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-body">
                    <h5 class="mb-3">What's Next?</h5>
                    <ol class="text-start">
                        <li>Make the bank transfer with the order number as reference</li>
                        <li>We will process your order within 1-2 business days</li>
                        <li>You will receive an email with download links once payment is confirmed</li>
                    </ol>
                </div>
            </div>
            
            <div class="d-flex justify-content-center gap-3">
                <a href="products.php" class="btn btn-primary">
                    <i class="fas fa-shopping-bag"></i> Continue Shopping
                </a>
                <a href="orders.php" class="btn btn-outline-primary">
                    <i class="fas fa-list"></i> View My Orders
                </a>
            </div>
        </div>
    </div>
</div>

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