<?php
/**
 * (c) ININOUT & PYXIS. All Rights Reserved.
 * Wszelkie modyfikacje tego pliku wymagaja zgloszenia oraz zgody autorow.
 * Any modifications to this file require notification and consent of the authors.
 */
/**
 * i-BOK 5 - Strona powrotu po płatności
 *
 * Standalone. Wyświetla status płatności na podstawie tokenu.
 */
error_reporting(0);

require_once __DIR__ . '/config.php';
require_once __DIR__ . '/inc/session.php';
require_once __DIR__ . '/inc/security.php';
require_once __DIR__ . '/inc/db_config.php';
require_once __DIR__ . '/inc/auth.php';
require_once __DIR__ . '/inc/functions.php';
config_load_from_db();
require_once __DIR__ . '/inc/payment.php';
require_once __DIR__ . '/lang/loader.php';
load_language();

$token = trim($_GET['token'] ?? '');
$status_param = trim($_GET['status'] ?? '');
$session = null;

if (!empty($token)) {
    $session = payment_load_session($token);
}

$is_logged_in = is_client_logged_in();

// Determine display status
$display_status = 'pending';
if ($session) {
    if ($session['status'] === 'completed') {
        $display_status = 'success';
    } elseif ($session['status'] === 'failed') {
        $display_status = 'error';
    }
}
if ($status_param === 'error') {
    $display_status = 'error';
}

$theme = 'default';
$theme_file = IBOK_CFG_DIR . '/theme.cfg';
if (file_exists($theme_file)) {
    $td = @json_decode(file_get_contents($theme_file), true);
    if (!empty($td['theme'])) $theme = $td['theme'];
}
?>
<!DOCTYPE html>
<html lang="<?= get_current_language() ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= e($site_title) ?> - <?= __('payment.pay_online') ?></title>
    <link href="css/nunito-sans.css" rel="stylesheet">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/bootstrap-icons.min.css">
    <link rel="stylesheet" href="css/ibok.css">
</head>
<body class="bg-light<?= $theme !== 'default' ? ' theme-' . $theme : '' ?>">
    <div class="container py-5">
        <div class="row justify-content-center">
            <div class="col-md-6">
                <div class="card shadow-sm">
                    <div class="card-body text-center p-5">
                        <?php if ($display_status === 'success'): ?>
                            <div class="mb-4">
                                <i class="bi bi-check-circle text-success" style="font-size: 4rem;"></i>
                            </div>
                            <h4 class="mb-3"><?= __('payment.success_title') ?></h4>
                            <p class="text-muted mb-4"><?= __('payment.success_msg') ?></p>

                        <?php elseif ($display_status === 'error'): ?>
                            <div class="mb-4">
                                <i class="bi bi-x-circle text-danger" style="font-size: 4rem;"></i>
                            </div>
                            <h4 class="mb-3"><?= __('payment.error_title') ?></h4>
                            <p class="text-muted mb-4"><?= __('payment.error_msg') ?></p>

                        <?php else: ?>
                            <div class="mb-4">
                                <i class="bi bi-hourglass-split text-warning" style="font-size: 4rem;"></i>
                            </div>
                            <h4 class="mb-3"><?= __('payment.pending_title') ?></h4>
                            <p class="text-muted mb-4"><?= __('payment.pending_msg') ?></p>
                        <?php endif; ?>

                        <?php if ($session): ?>
                            <div class="bg-light rounded p-3 mb-4 text-start">
                                <small class="text-muted">
                                    <strong><?= __('invoices.invoice_number') ?>:</strong> <?= e($session['invoice_nr']) ?><br>
                                    <strong><?= __('common.amount') ?>:</strong> <?= number_format($session['amount'], 2, ',', ' ') ?> zł<br>
                                    <strong><?= __('payment_admin.history_gateway') ?>:</strong> <?= e(payment_gateway_label($session['gateway'])) ?>
                                </small>
                            </div>
                        <?php endif; ?>

                        <?php if ($is_logged_in): ?>
                            <a href="index.php?page=faktury" class="btn btn-primary">
                                <i class="bi bi-arrow-left me-1"></i> <?= __('payment.back_to_invoices') ?>
                            </a>
                        <?php else: ?>
                            <a href="index.php" class="btn btn-primary">
                                <i class="bi bi-box-arrow-in-right me-1"></i> <?= __('payment.back_to_login') ?>
                            </a>
                        <?php endif; ?>
                    </div>
                </div>

                <div class="text-center mt-3">
                    <small class="text-muted">i-BOK 5 &copy; <?= date('Y') ?></small>
                </div>
            </div>
        </div>
    </div>

    <script nonce="<?= csp_nonce() ?>" src="js/bootstrap.bundle.min.js"></script>
</body>
</html>
