<?php
// /admin/payments.php
$page_title = "Payment Gateway Settings";
require_once 'includes/header.php';

$errors = [];
$success_message = '';

// --- Function to update a setting in the database ---
function update_setting($conn, $name, $value) {
    // Check if the setting exists first
    $stmt_check = $conn->prepare("SELECT setting_name FROM settings WHERE setting_name = ?");
    $stmt_check->bind_param("s", $name);
    $stmt_check->execute();
    $result_check = $stmt_check->get_result();
    
    if ($result_check->num_rows > 0) {
        // Update existing setting
        $stmt = $conn->prepare("UPDATE settings SET setting_value = ? WHERE setting_name = ?");
        $stmt->bind_param("ss", $value, $name);
    } else {
        // Insert new setting
        $stmt = $conn->prepare("INSERT INTO settings (setting_name, setting_value) VALUES (?, ?)");
        $stmt->bind_param("ss", $name, $value);
    }
    
    $result = $stmt->execute();
    $stmt->close();
    return $result;
}

// --- Handle Form Submission ---
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['payment_gateway_public_key'])) {
        update_setting($conn, 'payment_gateway_public_key', trim($_POST['payment_gateway_public_key']));
    }
    
    if (isset($_POST['payment_gateway_secret_key'])) {
        // Only update the secret key if a new value is provided
        $secret_key = trim($_POST['payment_gateway_secret_key']);
        if (!empty($secret_key)) {
            update_setting($conn, 'payment_gateway_secret_key', $secret_key);
        }
    }

    if (empty($errors)) {
        $success_message = "Payment settings updated successfully!";
    }
}


// Fetch Current Settings
$settings_result = $conn->query("SELECT setting_name, setting_value FROM settings WHERE setting_name LIKE 'payment_gateway_%'");
$settings = [];
while ($row = $settings_result->fetch_assoc()) {
    $settings[$row['setting_name']] = $row['setting_value'];
}
?>

<?php if (!empty($errors)): ?>
    <div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4 mb-6" role="alert"><p><?php foreach($errors as $e) echo $e . '<br>'; ?></p></div>
<?php endif; ?>
<?php if ($success_message): ?>
    <div class="bg-green-100 border-l-4 border-green-500 text-green-700 p-4 mb-6" role="alert"><p><?php echo $success_message; ?></p></div>
<?php endif; ?>

<div class="bg-white p-8 rounded-lg shadow-md max-w-2xl mx-auto">
    <h1 class="text-2xl font-bold mb-2">Payment Gateway Settings</h1>
    <p class="text-gray-600 mb-6">Enter your API keys for your chosen payment provider (e.g., Paystack).</p>
    
    <form action="/admin/payments.php" method="POST" class="space-y-6">
        <div>
            <label for="pg_public_key" class="block text-sm font-medium text-gray-700">Public Key</label>
            <input type="text" name="payment_gateway_public_key" id="pg_public_key" value="<?php echo escape($settings['payment_gateway_public_key'] ?? ''); ?>" class="mt-1 block w-full border-gray-300 rounded-md shadow-sm">
        </div>
        <div>
            <label for="pg_secret_key" class="block text-sm font-medium text-gray-700">Secret Key</label>
            <input type="password" name="payment_gateway_secret_key" id="pg_secret_key" placeholder="Leave blank to keep current key" class="mt-1 block w-full border-gray-300 rounded-md shadow-sm">
        </div>
        <div class="pt-5">
            <div class="flex justify-end">
                <button type="submit" class="bg-blue-600 text-white font-semibold py-2 px-6 rounded-lg hover:bg-blue-700">
                    Save Payment Settings
                </button>
            </div>
        </div>
    </form>
</div>

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