<?php
require_once __DIR__ . '/../includes/init.php';
require_once __DIR__ . '/../includes/auth.php';
require_admin();

$db = db();

$rate = 0.23; // Default
$message = '';

// Fetch current rate
$stmt = $db->prepare("SELECT value FROM settings WHERE `key` = 'pln_to_eur_rate'");
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if ($result) {
    $rate = (float)$result['value'];
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['pln_to_eur_rate'])) {
        $new_rate = filter_input(INPUT_POST, 'pln_to_eur_rate', FILTER_VALIDATE_FLOAT);
        
        if ($new_rate !== false && $new_rate > 0) {
            $stmt = $db->prepare("UPDATE settings SET value = :value WHERE `key` = 'pln_to_eur_rate'");
            $stmt->execute([':value' => $new_rate]);
            
            // Verify update, or insert if it failed (e.g., key didn't exist)
            if ($stmt->rowCount() === 0) {
                $insert_stmt = $db->prepare("INSERT INTO settings (`key`, value) VALUES ('pln_to_eur_rate', :value)");
                $insert_stmt->execute([':value' => $new_rate]);
            }
            
            $rate = $new_rate;
            $message = 'Kurs został zaktualizowany.';
        } else {
            $message = 'Wprowadź poprawną wartość kursu (liczba większa od 0).';
        }
    }
}

$page_title = 'Ustawienia Walut';

?>

<?php require_once __DIR__ . '/../includes/html_head.php'; ?>
<body>
    <div class="container-fluid">
        <div class="row">
            <?php require_once __DIR__ . '/menu.php'; ?>
            <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
                <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                    <h1 class="h2"><?= $page_title ?></h1>
                </div>

                <?php if ($message): ?>
                    <div class="alert alert-info" role="alert">
                        <?= htmlspecialchars($message) ?>
                    </div>
                <?php endif; ?>

                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title">Kurs Wymiany Walut</h5>
                        <p class="card-text">Ustaw kurs wymiany PLN na EUR. Ten kurs będzie używany do wyświetlania cen w EUR dla klientów używających angielskiej wersji strony.</p>
                        
                        <form method="POST" action="settings.php">
                            <div class="mb-3">
                                <label for="pln_to_eur_rate" class="form-label">Kurs PLN → EUR (EUR = PLN * kurs)</label>
                                <input type="number" step="0.0001" class="form-control" id="pln_to_eur_rate" name="pln_to_eur_rate" value="<?= htmlspecialchars($rate) ?>" required>
                            </div>
                            <button type="submit" class="btn btn-primary">Zapisz</button>
                        </form>
                    </div>
                </div>

            </main>
        </div>
    </div>
    <?php require_once __DIR__ . '/../includes/footer.php'; ?>
</body>
</html>
