<?php
/**
 * EFT (Bank Transfer) Payment Handler
 * Digital Product Store - South Africa
 */

require_once __DIR__ . '/../config/database.php';
require_login();

$order_id = isset($_GET['order_id']) ? (int)$_GET['order_id'] : 0;

if (!$order_id) {
    set_flash_message('Invalid order.', 'error');
    header('Location: ' . BASE_URL . '/cart.php');
    exit;
}

$order = get_order_by_id($order_id);

if (!$order || $order['user_id'] != get_current_user_id()) {
    set_flash_message('Order not found.', 'error');
    header('Location: ' . BASE_URL . '/cart.php');
    exit;
}

// Get EFT settings
$eft = get_payment_gateway('eft');

if (!$eft) {
    set_flash_message('EFT payment is not configured.', 'error');
    header('Location: ' . BASE_URL . '/checkout.php');
    exit;
}

$config = $eft['config'];
$bank_name = $config['bank_name'] ?? '';
$account_number = $config['account_number'] ?? '';
$account_name = $config['account_name'] ?? '';
$branch_code = $config['branch_code'] ?? '';

// Update order status to pending (awaiting payment)
$stmt = $pdo->prepare("UPDATE orders SET payment_status = 'pending' WHERE id = ?");
$stmt->execute([$order_id]);

// Record transaction
record_payment_transaction($order_id, 'eft', 'PENDING_' . time(), $order['total'], get_setting('currency_code', 'ZAR'), 'pending');

$page_title = 'Bank Transfer Instructions';
include __DIR__ . '/../header.php';
?>

<section class="py-5">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-lg-8">
                <div class="card">
                    <div class="card-header bg-primary text-white">
                        <i class="fas fa-university me-2"></i>Bank Transfer Instructions
                    </div>
                    <div class="card-body">
                        <div class="alert alert-info">
                            <i class="fas fa-info-circle me-2"></i>
                            Please complete your payment using the bank details below. Your order will be processed once payment is confirmed.
                        </div>
                        
                        <h5 class="mb-3">Order Details</h5>
                        <table class="table table-bordered">
                            <tr>
                                <td><strong>Order Number</strong></td>
                                <td><?php echo $order['order_number']; ?></td>
                            </tr>
                            <tr>
                                <td><strong>Amount to Pay</strong></td>
                                <td><strong class="text-primary h5"><?php echo format_price($order['total']); ?></strong></td>
                            </tr>
                            <tr>
                                <td><strong>Reference</strong></td>
                                <td><code class="bg-light px-2 py-1"><?php echo $order['order_number']; ?></code></td>
                            </tr>
                        </table>
                        
                        <h5 class="mb-3 mt-4">Bank Account Details</h5>
                        <table class="table table-bordered">
                            <tr>
                                <td><strong>Bank Name</strong></td>
                                <td><?php echo htmlspecialchars($bank_name); ?></td>
                            </tr>
                            <tr>
                                <td><strong>Account Number</strong></td>
                                <td><code class="bg-light px-2 py-1"><?php echo htmlspecialchars($account_number); ?></code></td>
                            </tr>
                            <tr>
                                <td><strong>Account Name</strong></td>
                                <td><?php echo htmlspecialchars($account_name); ?></td>
                            </tr>
                            <tr>
                                <td><strong>Branch Code</strong></td>
                                <td><code class="bg-light px-2 py-1"><?php echo htmlspecialchars($branch_code); ?></code></td>
                            </tr>
                        </table>
                        
                        <div class="alert alert-warning mt-4">
                            <h6><i class="fas fa-exclamation-triangle me-2"></i>Important</h6>
                            <ul class="mb-0">
                                <li>Use your Order Number (<strong><?php echo $order['order_number']; ?></strong>) as the payment reference</li>
                                <li>Payment must be made within 48 hours</li>
                                <li>Send proof of payment to <?php echo get_setting('site_email', 'support@digitalstore.com'); ?></li>
                                <li>Your order will be processed once payment is confirmed</li>
                            </ul>
                        </div>
                        
                        <div class="d-grid gap-2 mt-4">
                            <a href="<?php echo BASE_URL; ?>/order-confirmation.php?order_id=<?php echo $order_id; ?>" class="btn btn-primary">
                                <i class="fas fa-check me-2"></i>I've Made the Payment
                            </a>
                            <a href="<?php echo BASE_URL; ?>/contact.php" class="btn btn-outline-secondary">
                                <i class="fas fa-envelope me-2"></i>Contact Support
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

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