<?php
/**
 * Admin Bonuses Management
 */
require_once 'includes/header.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $settings = [
        'welcome_bonus_percent' => (int)$_POST['welcome_bonus'],
        'deposit_bonus_percent' => (int)$_POST['deposit_bonus'],
        'referral_commission_percent' => (int)$_POST['referral_commission']
    ];
    foreach ($settings as $key => $value) {
        $exists = dbFetch("SELECT id FROM settings WHERE setting_key = ?", [$key]);
        if ($exists) {
            dbUpdate('settings', ['setting_value' => $value], 'setting_key = ?', [$key]);
        } else {
            dbInsert('settings', ['setting_key' => $key, 'setting_value' => $value]);
        }
    }
    echo '<div class="alert alert-success"><i class="fas fa-check"></i> Bonus settings updated!</div>';
}

$welcomeBonus = dbFetch("SELECT setting_value FROM settings WHERE setting_key = 'welcome_bonus_percent'")['setting_value'] ?? 10;
$depositBonus = dbFetch("SELECT setting_value FROM settings WHERE setting_key = 'deposit_bonus_percent'")['setting_value'] ?? 20;
$referralCommission = dbFetch("SELECT setting_value FROM settings WHERE setting_key = 'referral_commission_percent'")['setting_value'] ?? 5;
?>

<div class="page-header">
    <h1 class="page-title">Bonuses & Promotions</h1>
</div>

<form method="POST">
    <div class="row">
        <div class="col-md-4">
            <div class="card">
                <div class="card-header"><h4><i class="fas fa-gift text-green"></i> Welcome Bonus</h4></div>
                <div class="card-body">
                    <div class="form-group">
                        <label class="form-label">Bonus Percentage</label>
                        <div class="input-group">
                            <input type="number" name="welcome_bonus" class="form-control" value="<?= $welcomeBonus ?>" min="0" max="100">
                            <span class="input-group-text">%</span>
                        </div>
                        <small class="text-muted">Bonus on first deposit</small>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card">
                <div class="card-header"><h4><i class="fas fa-percent text-blue"></i> Deposit Bonus</h4></div>
                <div class="card-body">
                    <div class="form-group">
                        <label class="form-label">Bonus Percentage</label>
                        <div class="input-group">
                            <input type="number" name="deposit_bonus" class="form-control" value="<?= $depositBonus ?>" min="0" max="100">
                            <span class="input-group-text">%</span>
                        </div>
                        <small class="text-muted">Bonus on all deposits</small>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card">
                <div class="card-header"><h4><i class="fas fa-users text-yellow"></i> Referral Commission</h4></div>
                <div class="card-body">
                    <div class="form-group">
                        <label class="form-label">Commission Percentage</label>
                        <div class="input-group">
                            <input type="number" name="referral_commission" class="form-control" value="<?= $referralCommission ?>" min="0" max="50">
                            <span class="input-group-text">%</span>
                        </div>
                        <small class="text-muted">Commission from referral trades</small>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <button type="submit" class="btn btn-primary btn-lg mt-4">
        <i class="fas fa-save"></i> Save Bonus Settings
    </button>
</form>

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