<?php
/**
 * Super Admin — System Settings
 *
 * Document pricing, referral commission, grade system, serial prefixes.
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$settings = $settings ?? [];
$groups = $groups ?? [];
?>
<div class="admin-section">
    <form method="POST" action="<?= $bp ?>/super-admin/settings" style="max-width:800px;">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">

        <?php foreach ($groups as $groupName => $groupFields): ?>
        <div class="admin-card" style="background:#fff;border-radius:12px;padding:24px;margin-bottom:20px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
            <h2 style="margin:0 0 6px;font-size:1.1rem;color:#2c1e4a;"><?= htmlspecialchars($groupName, ENT_QUOTES, 'UTF-8') ?></h2>
            <div style="margin-bottom:16px;"></div>

            <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(200px,1fr));gap:14px;">
                <?php foreach ($groupFields as $key => $config): ?>
                <div>
                    <label style="display:block;font-size:0.82rem;font-weight:600;color:#2c1e4a;margin-bottom:4px;" for="setting_<?= htmlspecialchars($key, ENT_QUOTES, 'UTF-8') ?>">
                        <?= htmlspecialchars($config['label'], ENT_QUOTES, 'UTF-8') ?>
                    </label>
                    <?php if ($config['type'] === 'number'): ?>
                        <input type="number" id="setting_<?= htmlspecialchars($key, ENT_QUOTES, 'UTF-8') ?>" name="<?= htmlspecialchars($key, ENT_QUOTES, 'UTF-8') ?>"
                               value="<?= htmlspecialchars($settings[$key] ?? $config['default'], ENT_QUOTES, 'UTF-8') ?>"
                               step="any" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                    <?php elseif ($config['type'] === 'url'): ?>
                        <input type="url" id="setting_<?= htmlspecialchars($key, ENT_QUOTES, 'UTF-8') ?>" name="<?= htmlspecialchars($key, ENT_QUOTES, 'UTF-8') ?>"
                               value="<?= htmlspecialchars($settings[$key] ?? $config['default'], ENT_QUOTES, 'UTF-8') ?>"
                               placeholder="https://..." style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                    <?php else: ?>
                        <input type="text" id="setting_<?= htmlspecialchars($key, ENT_QUOTES, 'UTF-8') ?>" name="<?= htmlspecialchars($key, ENT_QUOTES, 'UTF-8') ?>"
                               value="<?= htmlspecialchars($settings[$key] ?? $config['default'], ENT_QUOTES, 'UTF-8') ?>"
                               style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                    <?php endif; ?>
                    <?php if (!empty($config['help'])): ?>
                        <small style="display:block;margin-top:3px;color:#999;font-size:0.72rem;"><?= htmlspecialchars($config['help'], ENT_QUOTES, 'UTF-8') ?></small>
                    <?php endif; ?>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endforeach; ?>

        <div style="display:flex;gap:12px;justify-content:flex-end;">
            <button type="submit" style="padding:12px 28px;background:linear-gradient(135deg,#6c3fe8,#4a2db8);color:#fff;border:none;border-radius:8px;font-weight:600;font-size:0.9rem;cursor:pointer;font-family:'Poppins',sans-serif;">Save All Settings</button>
        </div>
    </form>
</div>
