<?php
/**
 * Payment Gateway Settings (Sketpay Integration)
 */
require_once 'header.php';

$success = '';
$error = '';

// Ensure payment settings table exists
try {
    $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
    )");

    $stmt_check_pay = $pdo->query("SELECT COUNT(*) FROM payment_settings");
    if ($stmt_check_pay->fetchColumn() == 0) {
        $pdo->exec("INSERT INTO payment_settings (api_key, base_url) VALUES (NULL, NULL)");
    }
} catch (PDOException $e) {
    $error = "Database initialization error: " . $e->getMessage();
}

// Fetch current settings
try {
    $stmt = $pdo->query("SELECT * FROM payment_settings LIMIT 1");
    $pay_settings = $stmt->fetch();
} catch (PDOException $e) {
    $error = "Settings fetch error: " . $e->getMessage();
}

// Update Logic
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_gateway'])) {
    $base_url = rtrim(trim($_POST['base_url']), '/');
    $api_key  = trim($_POST['api_key']);

    try {
        $sql = "UPDATE payment_settings SET base_url = ?, api_key = ? WHERE id = ?";
        $pdo->prepare($sql)->execute([$base_url, $api_key, $pay_settings['id']]);
        $success = "Sketpay Gateway settings updated successfully!";
        
        // Refresh data
        $pay_settings = $pdo->query("SELECT * FROM payment_settings LIMIT 1")->fetch();
    } catch (PDOException $e) {
        $error = "Update failed: " . $e->getMessage();
    }
}
?>

<style>
    .gateway-card { background: #fff; border-radius: 12px; border: 1px solid var(--border-color); padding: 30px; box-shadow: 0 1px 3px rgba(0,0,0,0.05); }
    
    .nav-tabs { display: flex; gap: 10px; border-bottom: 1px solid var(--border-color); margin-bottom: 25px; padding-bottom: 5px; }
    .tab-btn { padding: 10px 15px; border-radius: 6px; font-size: 0.85rem; font-weight: 700; color: var(--text-gray); cursor: pointer; border: 1px solid transparent; transition: 0.3s; text-decoration: none; }
    .tab-btn.active { color: #00059E; background: #eef2ff; }
    
    .form-grid { display: grid; grid-template-columns: 1fr; gap: 20px; }
    .form-label { display: block; font-size: 0.85rem; font-weight: 700; color: var(--text-dark); margin-bottom: 8px; }
    .form-label span { color: #ef4444; }

    .help-text { font-size: 0.8rem; color: #64748b; margin-top: 5px; display: block; }

    @media (max-width: 768px) {
        .gateway-card { padding: 20px 15px; }
    }
</style>

<h1 class="page-title">Payment Settings</h1>

<div class="nav-tabs">
    <a href="settings" class="tab-btn">General</a>
    <div class="tab-btn active">Payment Gateway</div>
</div>

<?php if($success): ?> <div class="alert-box alert-success">✓ <?php echo $success; ?></div> <?php endif; ?>
<?php if($error): ?> <div class="alert-box alert-danger">⚠ <?php echo $error; ?></div> <?php endif; ?>

<div class="gateway-card">
    <div style="margin-bottom: 25px; display: flex; align-items: center; gap: 15px; padding-bottom: 15px; border-bottom: 1px dashed #e2e8f0;">
        <img src="https://i.supaimg.com/d25ec2bf-78c6-492d-a44f-f0d52090f6c0/a6919cbd-3639-4223-8455-84bd9a90b194.png" alt="Sketpay" style="height: 35px; object-fit: contain;">
        <div>
            <h3 style="margin: 0; color: #0f172a; font-size: 1.1rem;">Sketpay Configuration</h3>
            <p style="margin: 3px 0 0; color: #64748b; font-size: 0.85rem;">Setup your automated payment gateway credentials.</p>
        </div>
    </div>

    <form action="" method="POST">
        <div class="form-grid">
            
            <div class="form-group">
                <label class="form-label">API Base URL <span>*</span></label>
                <input type="url" name="base_url" class="form-control" value="<?php echo htmlspecialchars($pay_settings['base_url'] ?? ''); ?>" placeholder="e.g. https://sketpay.top" required>
                <span class="help-text">The domain URL of your Sketpay merchant panel (without trailing slash).</span>
            </div>

            <div class="form-group">
                <label class="form-label">API Key <span>*</span></label>
                <input type="text" name="api_key" class="form-control" value="<?php echo htmlspecialchars($pay_settings['api_key'] ?? ''); ?>" placeholder="Enter your secret API Key" required>
                <span class="help-text">You can find this key in your Sketpay Merchant Dashboard > Developer API settings.</span>
            </div>

        </div>

        <button type="submit" name="save_gateway" class="btn-admin" style="margin-top: 25px; padding: 12px 25px; border-radius: 6px; background: #00059E; color: white; border: none; font-weight: 600; cursor: pointer;">Save Gateway Settings</button>
    </form>
</div>

<?php echo '</div></div></body></html>'; ?>
