<?php
$pageTitle = 'Points System';
ob_start();
?>

<div class="admin-card">
    <h2 class="text-xl font-semibold mb-4">Points System Settings</h2>
    <p class="text-gray-300 mb-6">Configure the points/coins system for your gaming platform.</p>

    <form class="admin-form" method="POST" action="/admin/points-system">
        <div class="mb-8 border-b border-gray-800 pb-6">
            <h3 class="text-lg font-medium mb-4 text-primary">System Configuration</h3>

            <div class="mb-4">
                <label class="flex items-center cursor-pointer">
                    <input type="checkbox" name="enable_points_system" value="1" <?= ($settings['enable_points_system'] ?? '0') == '1' ? 'checked' : '' ?> class="mr-3">
                    <span>Enable Points System</span>
                </label>
                <p class="text-sm text-gray-400 mt-1">Allow users to earn and withdraw points by playing games</p>
            </div>

            <div class="mb-4">
                <label for="points_per_minute">Points Per Minute</label>
                <input type="number" id="points_per_minute" name="points_per_minute" value="<?= htmlspecialchars($settings['points_per_minute'] ?? '10') ?>" min="1" max="1000" required>
                <p class="text-sm text-gray-400 mt-1">How many points users earn per minute of gameplay</p>
            </div>

            <div class="mb-4">
                <label for="minimum_withdrawal">Minimum Withdrawal (Points)</label>
                <input type="number" id="minimum_withdrawal" name="minimum_withdrawal" value="<?= htmlspecialchars($settings['minimum_withdrawal'] ?? '1000') ?>" min="100" required>
                <p class="text-sm text-gray-400 mt-1">Minimum points required to make a withdrawal request</p>
            </div>

            <div class="mb-4">
                <label for="withdrawal_rate">Withdrawal Rate (Points per Dollar)</label>
                <input type="number" id="withdrawal_rate" name="withdrawal_rate" value="<?= htmlspecialchars($settings['withdrawal_rate'] ?? '100') ?>" min="1" required>
                <p class="text-sm text-gray-400 mt-1">How many points equal $1 USD (e.g., 100 points = $1)</p>
            </div>

            <div class="mb-4">
                <label for="withdrawal_methods">Available Withdrawal Methods</label>
                <input type="text" id="withdrawal_methods" name="withdrawal_methods" value="<?= htmlspecialchars($settings['withdrawal_methods'] ?? 'PayPal,Bank Transfer,Cryptocurrency') ?>" required>
                <p class="text-sm text-gray-400 mt-1">Comma-separated list of payment methods (e.g., PayPal,Bank Transfer,Cryptocurrency)</p>
            </div>
        </div>

        <div class="flex gap-4">
            <button type="submit" class="admin-button">
                <i class="fas fa-save mr-2"></i> Save Settings
            </button>
            <a href="/admin/withdrawal-requests" class="admin-button secondary">
                <i class="fas fa-money-bill-wave mr-2"></i> View Withdrawal Requests
            </a>
        </div>
    </form>
</div>

<?php if (($settings['enable_points_system'] ?? '0') == '1'): ?>
<div class="admin-card">
    <div class="flex justify-between items-center mb-4">
        <div>
            <h2 class="text-xl font-semibold">Points System Statistics</h2>
            <?php if (!empty($stats['reset_date'])): ?>
                <p class="text-sm text-gray-400 mt-1">
                    <i class="fas fa-info-circle mr-1"></i>
                    Statistics reset on <?= date('M j, Y \a\t g:i A', strtotime($stats['reset_date'])) ?>
                </p>
            <?php endif; ?>
        </div>
        <button onclick="resetPointsSystemStats()" class="admin-button secondary" title="Reset Statistics Display">
            <i class="fas fa-redo mr-2"></i> Reset Statistics
        </button>
    </div>
    
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
        <div class="bg-dark p-4 rounded-md border border-gray-800">
            <h3 class="text-lg font-medium mb-2 text-primary">Total Points Earned</h3>
            <p class="text-2xl font-bold text-white"><?= number_format($stats['total_points_earned'] ?? 0) ?></p>
        </div>
        
        <div class="bg-dark p-4 rounded-md border border-gray-800">
            <h3 class="text-lg font-medium mb-2 text-primary">Active Users with Points</h3>
            <p class="text-2xl font-bold text-white"><?= number_format($stats['users_with_points'] ?? 0) ?></p>
        </div>
        
        <div class="bg-dark p-4 rounded-md border border-gray-800">
            <h3 class="text-lg font-medium mb-2 text-primary">Pending Withdrawals</h3>
            <p class="text-2xl font-bold text-yellow-400"><?= number_format($stats['pending_withdrawals'] ?? 0) ?></p>
        </div>
        
        <div class="bg-dark p-4 rounded-md border border-gray-800">
            <h3 class="text-lg font-medium mb-2 text-primary">Total Withdrawn</h3>
            <p class="text-2xl font-bold text-green-400">$<?= number_format(($stats['total_withdrawn'] ?? 0) / ($settings['withdrawal_rate'] ?? 100), 2) ?></p>
        </div>
    </div>
</div>
<?php endif; ?>

<?php
$content = ob_get_clean();
include __DIR__ . '/layout.php';
?>

<script>
function resetPointsSystemStats() {
    if (confirm('Are you sure you want to reset the points system statistics display?\n\nThis will reset the statistics shown on this page to start counting from now.\n\nNote: This will NOT affect actual user balances, points, or withdrawal requests - only the statistics display will be reset.')) {
        fetch('/admin/points-system/reset', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            }
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                alert('Points system statistics display has been reset successfully!');
                location.reload();
            } else {
                alert('Error: ' + (data.message || 'Failed to reset statistics'));
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('An error occurred while resetting statistics');
        });
    }
}
</script>