<?php
// Minimum Top-up Configuration Page Content

// Set page variables for header
$page_title = 'Minimum Top-up Configuration';
$page_description = 'Configure minimum top-up amount for agents';

require_once __DIR__ . '/../../app/classes/MinimumTopupManager.php';

// Initialize minimum top-up manager
$topupManager = new MinimumTopupManager($conn);

$message = '';
$messageType = '';

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['update_minimum_topup'])) {
        $minimumAmount = floatval($_POST['minimum_amount']);
        $adminId = $_SESSION['admin_id'] ?? 1; // Get admin ID from session
        
        $result = $topupManager->updateMinimumTopupConfig($minimumAmount, $adminId);
        
        if ($result['success']) {
            $message = $result['message'];
            $messageType = 'success';
        } else {
            $message = $result['message'];
            $messageType = 'error';
        }
    }
}

// Get current configuration
$currentConfig = $topupManager->getMinimumTopupConfig();
$configHistory = $topupManager->getConfigurationHistory(10);
?>

<div class="container mx-auto px-4 py-8">
    <div class="max-w-4xl mx-auto">
        <!-- Page Header -->
        <div class="mb-8">
            <h1 class="text-3xl font-bold dark-text-primary mb-2">
                <i class="fas fa-coins text-yellow-500 mr-3"></i>Minimum Top-up Configuration
            </h1>
            <p class="dark-text-muted">Configure the minimum amount agents can top up their accounts</p>
        </div>

        <!-- Success/Error Messages -->
        <?php if ($message): ?>
        <div class="mb-6 p-4 rounded-lg <?= $messageType === 'success' ? 'bg-green-100 text-green-800 border border-green-200' : 'bg-red-100 text-red-800 border border-red-200' ?>">
            <div class="flex items-center">
                <i class="fas <?= $messageType === 'success' ? 'fa-check-circle' : 'fa-exclamation-triangle' ?> mr-2"></i>
                <?= htmlspecialchars($message) ?>
            </div>
        </div>
        <?php endif; ?>

        <!-- Current Configuration -->
        <div class="dark-card rounded-lg shadow-md mb-8">
            <div class="px-6 py-4 border-b dark-border">
                <h2 class="text-lg font-semibold dark-text-primary">
                    <i class="fas fa-cog mr-2"></i>Current Configuration
                </h2>
            </div>
            <div class="p-6">
                <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                    <div class="bg-blue-50 rounded-lg p-4">
                        <div class="flex items-center">
                            <div class="flex-shrink-0">
                                <i class="fas fa-coins text-blue-500 text-2xl"></i>
                            </div>
                            <div class="ml-4">
                                <p class="text-sm font-medium text-blue-600">Minimum Amount</p>
                                <p class="text-2xl font-bold text-blue-900">GHS <?= number_format($currentConfig['minimum_amount'], 2) ?></p>
                            </div>
                        </div>
                    </div>
                    
                    <div class="bg-green-50 rounded-lg p-4">
                        <div class="flex items-center">
                            <div class="flex-shrink-0">
                                <i class="fas fa-toggle-on text-green-500 text-2xl"></i>
                            </div>
                            <div class="ml-4">
                                <p class="text-sm font-medium text-green-600">Status</p>
                                <p class="text-2xl font-bold text-green-900"><?= $currentConfig['is_active'] ? 'Active' : 'Inactive' ?></p>
                            </div>
                        </div>
                    </div>
                    
                    <div class="bg-purple-50 rounded-lg p-4">
                        <div class="flex items-center">
                            <div class="flex-shrink-0">
                                <i class="fas fa-clock text-purple-500 text-2xl"></i>
                            </div>
                            <div class="ml-4">
                                <p class="text-sm font-medium text-purple-600">Last Updated</p>
                                <p class="text-sm font-bold text-purple-900"><?= date('M j, Y g:i A', strtotime($currentConfig['updated_at'])) ?></p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Configuration Form -->
        <div class="dark-card rounded-lg shadow-md mb-8">
            <div class="px-6 py-4 border-b dark-border">
                <h2 class="text-lg font-semibold dark-text-primary">
                    <i class="fas fa-edit mr-2"></i>Update Configuration
                </h2>
            </div>
            <div class="p-6">
                <form method="POST" class="space-y-6">
                    <div>
                        <label for="minimum_amount" class="block text-sm font-medium dark-text-primary mb-2">
                            <i class="fas fa-coins mr-2"></i>Minimum Top-up Amount (GHS)
                        </label>
                        <div class="relative">
                            <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                                <span class="text-gray-500 sm:text-sm">GHS</span>
                            </div>
                            <input type="number" 
                                   id="minimum_amount" 
                                   name="minimum_amount" 
                                   value="<?= number_format($currentConfig['minimum_amount'], 2) ?>" 
                                   step="0.01"
                                   min="0"
                                   max="1000"
                                   class="pl-12 block w-full border-gray-300 rounded-md shadow-sm focus:ring-blue-500 focus:border-blue-500" 
                                   required>
                        </div>
                        <p class="mt-2 text-sm dark-text-muted">
                            <i class="fas fa-info-circle mr-1"></i>
                            Agents will not be able to top up with amounts below this value
                        </p>
                    </div>
                    
                    <div class="bg-yellow-50 border border-yellow-200 rounded-lg p-4">
                        <div class="flex">
                            <div class="flex-shrink-0">
                                <i class="fas fa-exclamation-triangle text-yellow-400"></i>
                            </div>
                            <div class="ml-3">
                                <h3 class="text-sm font-medium text-yellow-800">Important Notice</h3>
                                <div class="mt-2 text-sm text-yellow-700">
                                    <ul class="list-disc list-inside space-y-1">
                                        <li>This setting affects all agents and super agents</li>
                                        <li>Changes take effect immediately</li>
                                        <li>Previous top-up attempts below the new minimum will be rejected</li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="flex justify-end">
                        <button type="submit" 
                                name="update_minimum_topup" 
                                class="bg-blue-600 hover:bg-blue-700 text-white px-6 py-2 rounded-lg font-medium transition-colors">
                            <i class="fas fa-save mr-2"></i>Update Configuration
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <!-- Configuration History -->
        <div class="dark-card rounded-lg shadow-md">
            <div class="px-6 py-4 border-b dark-border">
                <h2 class="text-lg font-semibold dark-text-primary">
                    <i class="fas fa-history mr-2"></i>Configuration History
                </h2>
            </div>
            <div class="p-6">
                <?php if (!empty($configHistory)): ?>
                <div class="overflow-x-auto">
                    <table class="min-w-full divide-y dark-border">
                        <thead class="bg-dark-primary">
                            <tr>
                                <th class="px-6 py-3 text-left text-xs font-medium dark-text-muted uppercase tracking-wider">Amount</th>
                                <th class="px-6 py-3 text-left text-xs font-medium dark-text-muted uppercase tracking-wider">Status</th>
                                <th class="px-6 py-3 text-left text-xs font-medium dark-text-muted uppercase tracking-wider">Created By</th>
                                <th class="px-6 py-3 text-left text-xs font-medium dark-text-muted uppercase tracking-wider">Date</th>
                            </tr>
                        </thead>
                        <tbody class="bg-dark-secondary divide-y dark-border">
                            <?php foreach ($configHistory as $config): ?>
                            <tr class="<?= $config['is_active'] ? 'bg-green-900/20' : '' ?>">
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium dark-text-primary">
                                    GHS <?= number_format($config['minimum_amount'], 2) ?>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium <?= $config['is_active'] ? 'bg-green-100 text-green-800' : 'bg-gray-100 text-gray-800' ?>">
                                        <i class="fas fa-<?= $config['is_active'] ? 'check-circle' : 'times-circle' ?> mr-1"></i>
                                        <?= $config['is_active'] ? 'Active' : 'Inactive' ?>
                                    </span>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm dark-text-muted">
                                    Admin #<?= $config['created_by'] ?>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm dark-text-muted">
                                    <?= date('M j, Y g:i A', strtotime($config['created_at'])) ?>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php else: ?>
                <div class="text-center py-8">
                    <i class="fas fa-history dark-text-muted text-4xl mb-4"></i>
                    <p class="dark-text-muted">No configuration history available</p>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<script>
// Add input validation
document.getElementById('minimum_amount').addEventListener('input', function(e) {
    const value = parseFloat(e.target.value);
    if (value < 0) {
        e.target.setCustomValidity('Minimum amount cannot be negative');
    } else if (value > 1000) {
        e.target.setCustomValidity('Minimum amount cannot exceed GHS 1,000');
    } else {
        e.target.setCustomValidity('');
    }
});
</script>
v