<?php
require_once '../config.php';
require_once '../app/classes/AdminAuth.php';
require_once '../app/classes/BannerNotificationManager.php';

$adminAuth = new AdminAuth($conn);
$adminAuth->requireAuth('admin_login.php');

$bannerManager = new BannerNotificationManager($conn);
$message = '';
$messageType = '';

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['action'])) {
        switch ($_POST['action']) {
            case 'save_banner':
                $id = !empty($_POST['banner_id']) ? intval($_POST['banner_id']) : null;
                $message = trim($_POST['message']);
                $isEnabled = isset($_POST['is_enabled']) ? 1 : 0;
                $speed = isset($_POST['speed']) ? intval($_POST['speed']) : 50;
                $color = isset($_POST['color']) ? $_POST['color'] : 'blue';
                $backgroundColor = isset($_POST['background_color']) ? $_POST['background_color'] : 'blue';
                $textColor = isset($_POST['text_color']) ? $_POST['text_color'] : 'white';
                
                $result = $bannerManager->saveBanner($id, $message, $isEnabled, $speed, $color, $backgroundColor, $textColor);
                
                if ($result['success']) {
                    $message = $result['message'];
                    $messageType = 'success';
                } else {
                    $message = $result['message'];
                    $messageType = 'error';
                }
                break;
                
            case 'toggle_banner':
                $id = intval($_POST['banner_id']);
                $enabled = isset($_POST['enabled']) ? 1 : 0;
                
                $result = $bannerManager->toggleBanner($id, $enabled);
                
                if ($result['success']) {
                    $message = $result['message'];
                    $messageType = 'success';
                } else {
                    $message = $result['message'];
                    $messageType = 'error';
                }
                break;
                
            case 'delete_banner':
                $id = intval($_POST['banner_id']);
                
                $result = $bannerManager->deleteBanner($id);
                
                if ($result['success']) {
                    $message = $result['message'];
                    $messageType = 'success';
                } else {
                    $message = $result['message'];
                    $messageType = 'error';
                }
                break;
        }
    }
}

// Get all banners
$banners = $bannerManager->getAllBanners();
$activeBanner = $bannerManager->getActiveBanner();

// Get banner for editing if ID is provided
$editBanner = null;
if (isset($_GET['edit'])) {
    $editBanner = $bannerManager->getBannerById(intval($_GET['edit']));
}
?>

<!-- Banner Notifications Page Content -->
<div class="container mx-auto px-4 py-8">
        <div class="mb-6">
            <h1 class="text-3xl font-bold text-gray-900">Banner Notifications</h1>
            <p class="text-gray-600 mt-2">Manage moving banner notifications displayed on user dashboards</p>
        </div>

        <?php if ($message): ?>
        <div class="mb-4 p-4 rounded-lg <?php echo $messageType === 'success' ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'; ?>">
            <i class="fas <?php echo $messageType === 'success' ? 'fa-check-circle' : 'fa-exclamation-circle'; ?> mr-2"></i>
            <?php echo htmlspecialchars($message); ?>
        </div>
        <?php endif; ?>

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
            <!-- Create/Edit Form -->
            <div class="lg:col-span-2">
                <div class="bg-white rounded-lg shadow-md p-6">
                    <h2 class="text-xl font-semibold text-gray-900 mb-4">
                        <?php echo $editBanner ? 'Edit Banner' : 'Create New Banner'; ?>
                    </h2>
                    
                    <form method="POST" class="space-y-4">
                        <input type="hidden" name="action" value="save_banner">
                        <?php if ($editBanner): ?>
                        <input type="hidden" name="banner_id" value="<?php echo $editBanner['id']; ?>">
                        <?php endif; ?>
                        
                        <div>
                            <label for="message" class="block text-sm font-medium text-gray-700 mb-2">
                                Banner Message <span class="text-red-500">*</span>
                            </label>
                            <textarea 
                                id="message" 
                                name="message" 
                                rows="3" 
                                required
                                class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
                                placeholder="Enter the message to display on the banner..."><?php echo htmlspecialchars($editBanner['message'] ?? ''); ?></textarea>
                            <p class="text-xs text-gray-500 mt-1">This message will scroll across the user dashboard</p>
                        </div>
                        
                        <div class="grid grid-cols-2 gap-4">
                            <div>
                                <label for="speed" class="block text-sm font-medium text-gray-700 mb-2">
                                    Animation Speed
                                </label>
                                <input 
                                    type="range" 
                                    id="speed" 
                                    name="speed" 
                                    min="10" 
                                    max="200" 
                                    value="<?php echo $editBanner['speed'] ?? 50; ?>"
                                    class="w-full"
                                    oninput="document.getElementById('speedValue').textContent = this.value">
                                <p class="text-xs text-gray-500 mt-1">
                                    Speed: <span id="speedValue"><?php echo $editBanner['speed'] ?? 50; ?></span> (10 = slow, 200 = fast)
                                </p>
                            </div>
                            
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-2">
                                    Enable Banner
                                </label>
                                <label class="flex items-center">
                                    <input 
                                        type="checkbox" 
                                        name="is_enabled" 
                                        value="1"
                                        <?php echo ($editBanner && $editBanner['is_enabled']) || (!$editBanner && isset($activeBanner)) ? 'checked' : ''; ?>
                                        class="w-4 h-4 text-blue-600 border-gray-300 rounded focus:ring-blue-500">
                                    <span class="ml-2 text-sm text-gray-700">Show this banner on user dashboards</span>
                                </label>
                                <p class="text-xs text-gray-500 mt-1">Only one banner can be active at a time</p>
                            </div>
                        </div>
                        
                        <div class="grid grid-cols-3 gap-4">
                            <div>
                                <label for="color" class="block text-sm font-medium text-gray-700 mb-2">
                                    Color Theme
                                </label>
                                <select 
                                    id="color" 
                                    name="color" 
                                    class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
                                    <option value="blue" <?php echo ($editBanner['color'] ?? 'blue') === 'blue' ? 'selected' : ''; ?>>Blue</option>
                                    <option value="green" <?php echo ($editBanner['color'] ?? 'blue') === 'green' ? 'selected' : ''; ?>>Green</option>
                                    <option value="red" <?php echo ($editBanner['color'] ?? 'blue') === 'red' ? 'selected' : ''; ?>>Red</option>
                                    <option value="yellow" <?php echo ($editBanner['color'] ?? 'blue') === 'yellow' ? 'selected' : ''; ?>>Yellow</option>
                                    <option value="purple" <?php echo ($editBanner['color'] ?? 'blue') === 'purple' ? 'selected' : ''; ?>>Purple</option>
                                </select>
                            </div>
                            
                            <div>
                                <label for="background_color" class="block text-sm font-medium text-gray-700 mb-2">
                                    Background Color
                                </label>
                                <input 
                                    type="color" 
                                    id="background_color" 
                                    name="background_color" 
                                    value="<?php echo htmlspecialchars(isset($editBanner['background_color']) && $editBanner['background_color'] ? $editBanner['background_color'] : '#3b82f6'); ?>"
                                    class="w-full h-10 border border-gray-300 rounded-md">
                            </div>
                            
                            <div>
                                <label for="text_color" class="block text-sm font-medium text-gray-700 mb-2">
                                    Text Color
                                </label>
                                <input 
                                    type="color" 
                                    id="text_color" 
                                    name="text_color" 
                                    value="<?php echo htmlspecialchars(isset($editBanner['text_color']) && $editBanner['text_color'] ? $editBanner['text_color'] : '#ffffff'); ?>"
                                    class="w-full h-10 border border-gray-300 rounded-md">
                            </div>
                        </div>
                        
                        <div class="flex space-x-3">
                            <button 
                                type="submit" 
                                class="px-6 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500">
                                <i class="fas fa-save mr-2"></i>
                                <?php echo $editBanner ? 'Update Banner' : 'Create Banner'; ?>
                            </button>
                            
                            <?php if ($editBanner): ?>
                            <a 
                                href="?page=banner_notifications" 
                                class="px-6 py-2 bg-gray-300 text-gray-700 rounded-md hover:bg-gray-400 focus:outline-none focus:ring-2 focus:ring-gray-500">
                                Cancel
                            </a>
                            <?php endif; ?>
                        </div>
                    </form>
                </div>
            </div>
            
            <!-- Active Banner Preview -->
            <div>
                <div class="bg-white rounded-lg shadow-md p-6">
                    <h2 class="text-xl font-semibold text-gray-900 mb-4">Active Banner</h2>
                    
                    <?php if ($activeBanner): ?>
                    <div class="mb-4 p-4 rounded-lg" style="background-color: <?php echo htmlspecialchars($activeBanner['background_color']); ?>; color: <?php echo htmlspecialchars($activeBanner['text_color']); ?>;">
                        <div class="flex items-center justify-between mb-2">
                            <span class="text-sm font-semibold">Currently Active</span>
                            <span class="px-2 py-1 bg-green-500 text-white text-xs rounded">ENABLED</span>
                        </div>
                        <p class="text-sm"><?php echo htmlspecialchars($activeBanner['message']); ?></p>
                    </div>
                    <?php else: ?>
                    <div class="text-center py-8 text-gray-500">
                        <i class="fas fa-bell-slash text-4xl mb-2"></i>
                        <p>No active banner</p>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        
        <!-- Existing Banners List -->
        <div class="mt-6 bg-white rounded-lg shadow-md p-6">
            <h2 class="text-xl font-semibold text-gray-900 mb-4">All Banners</h2>
            
            <?php if (empty($banners)): ?>
            <div class="text-center py-8 text-gray-500">
                <i class="fas fa-inbox text-4xl mb-2"></i>
                <p>No banners created yet</p>
            </div>
            <?php else: ?>
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Message</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Status</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Speed</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Created</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        <?php foreach ($banners as $banner): ?>
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="text-sm font-medium text-gray-900">
                                    <?php echo htmlspecialchars(substr($banner['message'], 0, 50)); ?>
                                    <?php echo strlen($banner['message']) > 50 ? '...' : ''; ?>
                                </div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <?php if ($banner['is_enabled']): ?>
                                <span class="px-2 py-1 text-xs font-semibold rounded-full bg-green-100 text-green-800">Active</span>
                                <?php else: ?>
                                <span class="px-2 py-1 text-xs font-semibold rounded-full bg-gray-100 text-gray-800">Inactive</span>
                                <?php endif; ?>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                <?php echo $banner['speed']; ?>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                <?php echo date('M d, Y', strtotime($banner['created_at'])); ?>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                                <div class="flex space-x-2">
                                    <a 
                                        href="?page=banner_notifications&edit=<?php echo $banner['id']; ?>" 
                                        class="text-blue-600 hover:text-blue-900">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    
                                    <form method="POST" class="inline" onsubmit="return confirm('Are you sure you want to toggle this banner?');">
                                        <input type="hidden" name="action" value="toggle_banner">
                                        <input type="hidden" name="banner_id" value="<?php echo $banner['id']; ?>">
                                        <input type="hidden" name="enabled" value="<?php echo $banner['is_enabled'] ? 0 : 1; ?>">
                                        <button 
                                            type="submit" 
                                            class="text-<?php echo $banner['is_enabled'] ? 'yellow' : 'green'; ?>-600 hover:text-<?php echo $banner['is_enabled'] ? 'yellow' : 'green'; ?>-900">
                                            <i class="fas fa-<?php echo $banner['is_enabled'] ? 'pause' : 'play'; ?>"></i>
                                        </button>
                                    </form>
                                    
                                    <form method="POST" class="inline" onsubmit="return confirm('Are you sure you want to delete this banner?');">
                                        <input type="hidden" name="action" value="delete_banner">
                                        <input type="hidden" name="banner_id" value="<?php echo $banner['id']; ?>">
                                        <button 
                                            type="submit" 
                                            class="text-red-600 hover:text-red-900">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php endif; ?>
        </div>
</div>

