<?php
// includes/components/notifications.php
// Este archivo es incluido desde header.php
?>
<!-- Notificaciones -->
<div class="dropdown">
    <button class="btn btn-outline-secondary position-relative" type="button" 
            data-bs-toggle="dropdown" aria-expanded="false"
            title="Notificaciones del sistema">
        <i class="fas fa-bell"></i>
        <?php if (countUnreadNotifications() > 0): ?>
            <span class="notification-badge"><?php echo countUnreadNotifications(); ?></span>
        <?php endif; ?>
    </button>
    
    <div class="dropdown-menu dropdown-menu-end p-0" style="min-width: 380px;">
        <div class="dropdown-header bg-primary text-white">
            <div class="d-flex justify-content-between align-items-center">
                <span>
                    <i class="fas fa-bell me-1"></i>Notificaciones
                </span>
                <?php if (countUnreadNotifications() > 0): ?>
                    <span class="badge bg-warning"><?php echo countUnreadNotifications(); ?> nuevas</span>
                <?php endif; ?>
            </div>
        </div>
        
        <div style="max-height: 400px; overflow-y: auto;">
            <?php if (empty($_SESSION['notificaciones'])): ?>
                <div class="text-center text-muted py-4">
                    <i class="fas fa-bell-slash fa-2x mb-2"></i>
                    <p class="small mb-0">No hay notificaciones</p>
                </div>
            <?php else: ?>
                <?php foreach (getRecentNotifications(8) as $notif): ?>
                    <div class="dropdown-item notification-item <?php echo !$notif['leida'] ? 'unread' : ''; ?> p-3 border-bottom">
                        <div class="d-flex align-items-start">
                            <div class="flex-grow-1">
                                <div class="d-flex justify-content-between align-items-start mb-1">
                                    <h6 class="mb-0 <?php echo !$notif['leida'] ? 'fw-bold' : ''; ?>">
                                        <i class="fas <?php echo $notif['icono'] ?? 'fa-bell'; ?> me-1 text-<?php echo $notif['tipo']; ?>"></i>
                                        <?php echo htmlspecialchars($notif['titulo']); ?>
                                    </h6>
                                    <small class="text-muted"><?php echo $notif['fecha']; ?></small>
                                </div>
                                <p class="small text-muted mb-1"><?php echo htmlspecialchars($notif['mensaje']); ?></p>
                                <span class="badge bg-<?php echo $notif['tipo']; ?>">
                                    <?php echo getNotificationTypeText($notif['tipo']); ?>
                                </span>
                            </div>
                            <div class="notification-actions ms-2">
                                <div class="btn-group btn-group-sm">
                                    <?php if (!$notif['leida']): ?>
                                        <a href="?marcar_leida=<?php echo $notif['id']; ?>" class="btn btn-outline-success" title="Marcar como leída">
                                            <i class="fas fa-check"></i>
                                        </a>
                                    <?php endif; ?>
                                    <a href="?eliminar_notificacion=<?php echo $notif['id']; ?>" class="btn btn-outline-danger" title="Eliminar notificación" 
                                       onclick="return confirm('¿Estás seguro de que quieres eliminar esta notificación?')">
                                        <i class="fas fa-trash"></i>
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
        
        <div class="dropdown-footer p-2 border-top">
            <div class="d-flex justify-content-between">
                <button type="button" class="btn btn-sm btn-outline-primary" data-bs-toggle="modal" data-bs-target="#crearNotificacionModal">
                    <i class="fas fa-plus me-1"></i> Nueva
                </button>
                <div class="btn-group">
                    <?php if (countUnreadNotifications() > 0): ?>
                        <a href="?marcar_todas_leidas=1" class="btn btn-sm btn-outline-success" title="Marcar todas como leídas">
                            <i class="fas fa-check-double me-1"></i> Leer Todas
                        </a>
                    <?php endif; ?>
                    <?php if (!empty($_SESSION['notificaciones'])): ?>
                        <a href="?eliminar_todas_notificaciones=1" class="btn btn-sm btn-outline-danger" 
                           onclick="return confirm('¿Estás seguro de que quieres eliminar TODAS las notificaciones?')">
                            <i class="fas fa-trash me-1"></i> Limpiar
                        </a>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>