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

$db = db();

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

// Fetch current settings
$stmt = $db->query("SELECT `key`, `value` FROM settings");
$settings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);

if (isset($settings['pln_to_eur_rate'])) {
    $rate = (float)$settings['pln_to_eur_rate'];
}
if (isset($settings['bank_name'])) {
    $bank_name = $settings['bank_name'];
}
if (isset($settings['bank_account_number'])) {
    $bank_account_number = $settings['bank_account_number'];
}

// 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("INSERT INTO settings (`key`, value) VALUES ('pln_to_eur_rate', :value) ON DUPLICATE KEY UPDATE value = :value");
            $stmt->execute([':value' => $new_rate]);
            
            $rate = $new_rate;
            $message = 'Kurs został zaktualizowany.';
        } else {
            $message = 'Wprowadź poprawną wartość kursu (liczba większa od 0).';
        }
    } elseif (isset($_POST['bank_name']) || isset($_POST['bank_account_number'])) {
        $new_bank_name = filter_input(INPUT_POST, 'bank_name', FILTER_SANITIZE_STRING);
        $new_bank_account_number = filter_input(INPUT_POST, 'bank_account_number', FILTER_SANITIZE_STRING);

        $stmt = $db->prepare("INSERT INTO settings (`key`, value) VALUES (:key, :value) ON DUPLICATE KEY UPDATE value = :value");
        
        $stmt->execute([':key' => 'bank_name', ':value' => $new_bank_name]);
        $bank_name = $new_bank_name;
        
        $stmt->execute([':key' => 'bank_account_number', ':value' => $new_bank_account_number]);
        $bank_account_number = $new_bank_account_number;

        $message = 'Dane do przelewu zostały zaktualizowane.';
    }
}

$page_title = 'Ustawienia';

?>

<?php require_once __DIR__ . '/../includes/html_head.php'; ?>

<body>

<?php require_once __DIR__ . '/menu.php'; ?>

<main class="container my-5">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h2"><?= $page_title ?></h1>
    </div>

    <div class="card mb-4">
        <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>

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

    <div class="card">
        <div class="card-body">
            <h5 class="card-title">Dane do przelewu</h5>
            <p class="card-text">Ustaw dane do przelewu, które będą wyświetlane w mailach i na stronie po złożeniu zamówienia.</p>

            <form method="POST" action="settings.php">
                <div class="mb-3">
                    <label for="bank_name" class="form-label">Nazwa banku</label>
                    <input type="text" class="form-control" id="bank_name" name="bank_name" value="<?= htmlspecialchars($bank_name) ?>">
                </div>
                <div class="mb-3">
                    <label for="bank_account_number" class="form-label">Numer konta bankowego</label>
                    <input type="text" class="form-control" id="bank_account_number" name="bank_account_number" value="<?= htmlspecialchars($bank_account_number) ?>">
                </div>
                <button type="submit" class="btn btn-primary">Zapisz</button>
            </form>
        </div>
    </div>
</main>

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

</body>
</html>
