<?php
session_start();
require_once '../includes/db.php';

$pdo->exec("CREATE TABLE IF NOT EXISTS payment_settings (
    id INT AUTO_INCREMENT PRIMARY KEY,
    base_url VARCHAR(255) NULL,
    api_key VARCHAR(255) NULL
)");

try { $pdo->exec("ALTER TABLE payment_settings ADD COLUMN base_url VARCHAR(255) NULL"); } catch (PDOException $e) {}
try { $pdo->exec("ALTER TABLE payment_settings ADD COLUMN api_key VARCHAR(255) NULL"); } catch (PDOException $e) {}

$stmt_check = $pdo->query("SELECT COUNT(*) FROM payment_settings");
if ($stmt_check->fetchColumn() == 0) {
    $pdo->exec("INSERT INTO payment_settings (api_key, base_url) VALUES (NULL, NULL)");
}

$message = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_settings'])) {
    $base_url = trim($_POST['base_url']);
    $api_key = trim($_POST['api_key']);
    
    $stmt = $pdo->prepare("UPDATE payment_settings SET base_url = ?, api_key = ? WHERE id = 1");
    if ($stmt->execute([$base_url, $api_key])) {
        $message = '<div class="alert alert-success shadow-sm rounded-3 fw-bold">Settings updated successfully!</div>';
    } else {
        $message = '<div class="alert alert-danger shadow-sm rounded-3 fw-bold">Failed to update settings.</div>';
    }
}

$stmt = $pdo->query("SELECT * FROM payment_settings LIMIT 1");
$settings = $stmt->fetch(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin - Payment Gateway Settings</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { background-color: #f4f7f6; font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; }
    </style>
</head>
<body>
    <div class="container py-5">
        <div class="row justify-content-center">
            <div class="col-md-8 col-lg-6">
                <div class="card shadow-lg border-0 rounded-4">
                    <div class="card-header bg-dark text-white p-4 border-0 rounded-top-4">
                        <h4 class="mb-0 fw-bold">Payment Gateway Settings</h4>
                    </div>
                    <div class="card-body p-4">
                        <?php echo $message; ?>
                        <form method="POST" action="">
                            <div class="mb-4">
                                <label for="base_url" class="form-label fw-bold text-muted small">Base URL</label>
                                <input type="url" class="form-control shadow-none bg-light" id="base_url" name="base_url" value="<?php echo htmlspecialchars($settings['base_url'] ?? ''); ?>" placeholder="https://secure-pay.nagorikpay.com" required>
                            </div>
                            <div class="mb-4">
                                <label for="api_key" class="form-label fw-bold text-muted small">API Key</label>
                                <input type="text" class="form-control shadow-none bg-light" id="api_key" name="api_key" value="<?php echo htmlspecialchars($settings['api_key'] ?? ''); ?>" placeholder="Enter API Key" required>
                            </div>
                            <button type="submit" name="save_settings" class="btn btn-primary w-100 py-3 fw-bold shadow-sm rounded-3">
                                Save Settings
                            </button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
