<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once __DIR__ . "/../includes/admin_auth.php"; // Must be first
require_once '../includes/db.php';

/* ==========================
   SAVE SETTINGS (FIXED)
========================== */
$message = "";

if ($_SERVER["REQUEST_METHOD"] === "POST") {

    foreach ($_POST as $key => $value) {

        $stmt = $conn->prepare("
            INSERT INTO system_settings (setting_key, setting_value)
            VALUES (?, ?)
            ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)
        ");

        if (!$stmt) {
            die("SQL Error: " . $conn->error);
        }

        $stmt->bind_param("ss", $key, $value);
        $stmt->execute();
    }

    $message = "Settings updated successfully!";
}

/* ==========================
   LOAD SETTINGS (FIXED)
========================== */
$settings = [];

$result = $conn->query("SELECT setting_key, setting_value FROM system_settings");

if (!$result) {
    die("Query failed: " . $conn->error);
}

while ($row = $result->fetch_assoc()) {
    $settings[$row['setting_key']] = $row['setting_value'];
}

/* ==========================
   HELPER
========================== */
function getSetting($key, $default = '') {
    global $settings;
    return $settings[$key] ?? $default;
}

/* ==========================
   VIEW
========================== */
ob_start();
?>

<div class="main-content">

    <h2 class="page-title">System Settings</h2>

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

    <form method="POST" class="panel">

        <h3>General Settings</h3>

        <label>Site Name</label>
        <input type="text" name="site_name"
               value="<?= htmlspecialchars(getSetting('site_name', 'DemoBank')) ?>">

        <label>System Email (Sender)</label>
        <input type="email" name="system_email"
               value="<?= htmlspecialchars(getSetting('system_email', 'no-reply@demobank.com')) ?>">

        <label>Sender Name</label>
        <input type="text" name="sender_name"
               value="<?= htmlspecialchars(getSetting('sender_name', 'DemoBank')) ?>">

        <h3>Transfer Settings</h3>

        <label>Local Transfer Fee</label>
        <input type="number" step="0.01" name="local_transfer_fee"
               value="<?= htmlspecialchars(getSetting('local_transfer_fee', 3)) ?>">

        <label>International Transfer Fee</label>
        <input type="number" step="0.01" name="international_transfer_fee"
               value="<?= htmlspecialchars(getSetting('international_transfer_fee', 10)) ?>">

        <h3>System Control</h3>

        <label>Enable Transfers</label>
        <select name="transfers_enabled">
            <option value="1" <?= getSetting('transfers_enabled', 1) == 1 ? 'selected' : '' ?>>Enabled</option>
            <option value="0" <?= getSetting('transfers_enabled', 1) == 0 ? 'selected' : '' ?>>Disabled</option>
        </select>

        <label>Maintenance Mode</label>
        <select name="maintenance_mode">
            <option value="0" <?= getSetting('maintenance_mode', 0) == 0 ? 'selected' : '' ?>>Off</option>
            <option value="1" <?= getSetting('maintenance_mode', 0) == 1 ? 'selected' : '' ?>>On</option>
        </select>

        <br><br>

        <button type="submit" class="btn">Save Settings</button>

    </form>

</div>

<?php
$content = ob_get_clean();
$page_title = "System Settings";
include '../components/admin_layout.php';
?>