{% load navigation_tags %}

<!-- Permission-Aware Quick Actions Component -->
<div class="quick-actions-container">
    {% if quick_actions %}
    <div class="quick-actions-header">
        <h3 class="quick-actions-title">
            <span>⚡</span>
            Quick Actions
        </h3>
        <p class="quick-actions-subtitle">
            Actions available based on your role and permissions
        </p>
    </div>
    
    <div class="quick-actions-grid">
        {% for action in quick_actions %}
        <a href="{{ action.get_url }}" 
           class="quick-action-card"
           title="{{ action.tooltip|default:action.title }}"
           aria-label="{{ action.title }} - {{ action.tooltip|default:'Quick action' }}">
            <div class="action-icon-container">
                <i class="{{ action.icon|get_icon_class }} action-icon" aria-hidden="true"></i>
                {% if action.badge_count %}
                <span class="action-badge badge-{{ action.badge_color|default:'red' }}" 
                      aria-label="{{ action.badge_count }} items">
                    {{ action.badge_count }}
                </span>
                {% endif %}
            </div>
            <div class="action-content">
                <h4 class="action-title">{{ action.title }}</h4>
                {% if action.description %}
                <p class="action-description">{{ action.description }}</p>
                {% endif %}
            </div>
            <div class="action-arrow">
                <span>›</span>
            </div>
        </a>
        {% endfor %}
    </div>
    
    <!-- Role-specific tips -->
    <div class="quick-actions-tips">
        <div class="tips-header">
            <span>💡</span>
            <span class="tips-title">Tips for {{ user.get_role_display }}s</span>
        </div>
        <div class="tips-content">
            {% if user.role == 'admin' %}
            <p class="tip-item">
                <span>ℹ️</span>
                Use keyboard shortcuts (Ctrl+K) to quickly navigate to any page.
            </p>
            <p class="tip-item">
                <i class="fas fa-users-cog mr-2" aria-hidden="true"></i>
                Manage staff permissions from the Staff Management section.
            </p>
            {% elif user.role == 'team_leader' %}
            <p class="tip-item">
                <span>📈</span>
                Monitor your team's performance from the Portfolio section.
            </p>
            <p class="tip-item">
                <span>✓</span>
                Review pending loan approvals to keep operations flowing.
            </p>
            {% elif user.role == 'loan_officer' %}
            <p class="tip-item">
                <span>👥</span>
                Keep track of your assigned clients in the Portfolio section.
            </p>
            <p class="tip-item">
                <span>⏰</span>
                Check pending tasks regularly to stay on top of your workload.
            </p>
            {% elif user.role == 'secretary' %}
            <p class="tip-item">
                <i class="fas fa-file-upload mr-2" aria-hidden="true"></i>
                Upload client documents as soon as they're received.
            </p>
            <p class="tip-item">
                <span>📞</span>
                Use the communication tools to stay in touch with clients.
            </p>
            {% elif user.role == 'auditor' %}
            <p class="tip-item">
                <span>🔍</span>
                Access all compliance reports from the Reports section.
            </p>
            <p class="tip-item">
                <span>⬇️</span>
                Export data for external analysis when needed.
            </p>
            {% endif %}
        </div>
    </div>
    
    {% else %}
    <!-- No quick actions available -->
    <div class="no-actions-message">
        <div class="no-actions-icon">
            <span>ℹ️</span>
        </div>
        <h3 class="no-actions-title">No Quick Actions Available</h3>
        <p class="no-actions-text">
            Quick actions will appear here based on your role and permissions.
        </p>
    </div>
    {% endif %}
</div>

<style>
/* Quick Actions Component Styles */
.quick-actions-container {
    background: white;
    border-radius: 0.75rem;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    transition: all 0.3s ease;
}

.dark .quick-actions-container {
    background: #1e293b;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.quick-actions-header {
    padding: 1.5rem;
    border-bottom: 1px solid #e5e7eb;
    background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
}

.dark .quick-actions-header {
    border-bottom-color: #334155;
    background: linear-gradient(135deg, #334155 0%, #475569 100%);
}

.quick-actions-title {
    font-size: 1.125rem;
    font-weight: 600;
    color: #111827;
    margin: 0 0 0.25rem 0;
    display: flex;
    align-items: center;
}

.dark .quick-actions-title {
    color: #f1f5f9;
}

.quick-actions-subtitle {
    font-size: 0.875rem;
    color: #6b7280;
    margin: 0;
}

.dark .quick-actions-subtitle {
    color: #94a3b8;
}

.quick-actions-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 1rem;
    padding: 1.5rem;
}

.quick-action-card {
    display: flex;
    align-items: center;
    padding: 1rem;
    background: #f9fafb;
    border: 1px solid #e5e7eb;
    border-radius: 0.5rem;
    text-decoration: none;
    color: inherit;
    transition: all 0.2s ease;
    position: relative;
    overflow: hidden;
}

.quick-action-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 4px;
    height: 100%;
    background: #86c662;
    transform: scaleY(0);
    transition: transform 0.2s ease;
    transform-origin: bottom;
}

.quick-action-card:hover::before {
    transform: scaleY(1);
    transform-origin: top;
}

.quick-action-card:hover {
    background: white;
    border-color: #86c662;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(134, 198, 98, 0.15);
}

.dark .quick-action-card {
    background: #334155;
    border-color: #475569;
    color: #f1f5f9;
}

.dark .quick-action-card:hover {
    background: #475569;
    border-color: #86c662;
    box-shadow: 0 4px 12px rgba(134, 198, 98, 0.25);
}

.action-icon-container {
    position: relative;
    margin-right: 1rem;
    flex-shrink: 0;
}

.action-icon {
    width: 2.5rem;
    height: 2.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #86c662 0%, #73b554 100%);
    color: white;
    border-radius: 0.5rem;
    font-size: 1.25rem;
    box-shadow: 0 2px 4px rgba(134, 198, 98, 0.3);
    transition: all 0.2s ease;
}

.quick-action-card:hover .action-icon {
    transform: scale(1.1);
    box-shadow: 0 4px 8px rgba(134, 198, 98, 0.4);
}

.action-badge {
    position: absolute;
    top: -0.25rem;
    right: -0.25rem;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 1.25rem;
    height: 1.25rem;
    padding: 0 0.25rem;
    font-size: 0.75rem;
    font-weight: 600;
    border-radius: 9999px;
    color: white;
    border: 2px solid white;
}

.badge-red {
    background: #ef4444;
}

.badge-blue {
    background: #3b82f6;
}

.badge-green {
    background: #10b981;
}

.badge-yellow {
    background: #f59e0b;
}

.badge-purple {
    background: #8b5cf6;
}

.action-content {
    flex: 1;
    min-width: 0;
}

.action-title {
    font-size: 1rem;
    font-weight: 600;
    color: #111827;
    margin: 0 0 0.25rem 0;
    line-height: 1.25;
}

.dark .action-title {
    color: #f1f5f9;
}

.action-description {
    font-size: 0.875rem;
    color: #6b7280;
    margin: 0;
    line-height: 1.4;
}

.dark .action-description {
    color: #94a3b8;
}

.action-arrow {
    margin-left: 1rem;
    color: #9ca3af;
    transition: all 0.2s ease;
}

.quick-action-card:hover .action-arrow {
    color: #86c662;
    transform: translateX(4px);
}

.dark .action-arrow {
    color: #6b7280;
}

.dark .quick-action-card:hover .action-arrow {
    color: #86c662;
}

/* Quick Actions Tips */
.quick-actions-tips {
    padding: 1.5rem;
    background: #f8fafc;
    border-top: 1px solid #e5e7eb;
}

.dark .quick-actions-tips {
    background: #0f172a;
    border-top-color: #334155;
}

.tips-header {
    display: flex;
    align-items: center;
    margin-bottom: 0.75rem;
}

.tips-title {
    font-size: 0.875rem;
    font-weight: 600;
    color: #374151;
}

.dark .tips-title {
    color: #d1d5db;
}

.tips-content {
    space-y: 0.5rem;
}

.tip-item {
    display: flex;
    align-items: flex-start;
    font-size: 0.8125rem;
    color: #6b7280;
    margin: 0 0 0.5rem 0;
    line-height: 1.5;
}

.dark .tip-item {
    color: #9ca3af;
}

.tip-item:last-child {
    margin-bottom: 0;
}

.tip-item i {
    margin-top: 0.125rem;
    flex-shrink: 0;
}

/* No Actions Message */
.no-actions-message {
    padding: 3rem 1.5rem;
    text-align: center;
}

.no-actions-icon {
    margin-bottom: 1rem;
}

.no-actions-icon i {
    font-size: 3rem;
}

.no-actions-title {
    font-size: 1.125rem;
    font-weight: 600;
    color: #374151;
    margin: 0 0 0.5rem 0;
}

.dark .no-actions-title {
    color: #d1d5db;
}

.no-actions-text {
    font-size: 0.875rem;
    color: #6b7280;
    margin: 0;
}

.dark .no-actions-text {
    color: #9ca3af;
}

/* Responsive Design */
@media (max-width: 768px) {
    .quick-actions-grid {
        grid-template-columns: 1fr;
        gap: 0.75rem;
        padding: 1rem;
    }
    
    .quick-action-card {
        padding: 0.75rem;
    }
    
    .action-icon {
        width: 2rem;
        height: 2rem;
        font-size: 1rem;
    }
    
    .action-title {
        font-size: 0.875rem;
    }
    
    .action-description {
        font-size: 0.8125rem;
    }
    
    .quick-actions-header {
        padding: 1rem;
    }
    
    .quick-actions-tips {
        padding: 1rem;
    }
}

@media (max-width: 640px) {
    .quick-action-card {
        flex-direction: column;
        text-align: center;
        padding: 1rem 0.75rem;
    }
    
    .action-icon-container {
        margin-right: 0;
        margin-bottom: 0.75rem;
    }
    
    .action-arrow {
        margin-left: 0;
        margin-top: 0.5rem;
    }
    
    .quick-action-card:hover .action-arrow {
        transform: translateY(4px);
    }
}

/* Animation for loading state */
@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.quick-action-card {
    animation: fadeInUp 0.3s ease forwards;
}

.quick-action-card:nth-child(1) { animation-delay: 0.1s; }
.quick-action-card:nth-child(2) { animation-delay: 0.2s; }
.quick-action-card:nth-child(3) { animation-delay: 0.3s; }
.quick-action-card:nth-child(4) { animation-delay: 0.4s; }
.quick-action-card:nth-child(5) { animation-delay: 0.5s; }
.quick-action-card:nth-child(6) { animation-delay: 0.6s; }

/* Focus states for accessibility */
.quick-action-card:focus {
    outline: 2px solid #86c662;
    outline-offset: 2px;
}

/* Print styles */
@media print {
    .quick-actions-tips,
    .action-arrow {
        display: none !important;
    }
    
    .quick-actions-container {
        box-shadow: none !important;
        border: 1px solid #000 !important;
    }
    
    .quick-action-card {
        border: 1px solid #ccc !important;
        box-shadow: none !important;
        break-inside: avoid;
    }
}
</style>

<script>
// Quick actions interaction enhancements
document.addEventListener('DOMContentLoaded', function() {
    // Add click tracking for analytics
    const quickActionCards = document.querySelectorAll('.quick-action-card');
    
    quickActionCards.forEach(card => {
        card.addEventListener('click', function(e) {
            const actionTitle = this.querySelector('.action-title').textContent;
            
            // Track quick action usage (integrate with analytics if available)
            if (typeof gtag !== 'undefined') {
                gtag('event', 'quick_action_click', {
                    'action_name': actionTitle,
                    'user_role': '{{ user.role }}'
                });
            }
            
            // Add visual feedback
            this.style.transform = 'scale(0.98)';
            setTimeout(() => {
                this.style.transform = '';
            }, 150);
        });
        
        // Add keyboard navigation
        card.addEventListener('keydown', function(e) {
            if (e.key === 'Enter' || e.key === ' ') {
                e.preventDefault();
                this.click();
            }
        });
    });
    
    // Add loading states for actions that might take time
    const loadingActions = ['Export', 'Generate', 'Process'];
    
    quickActionCards.forEach(card => {
        const actionTitle = card.querySelector('.action-title').textContent;
        
        if (loadingActions.some(action => actionTitle.includes(action))) {
            card.addEventListener('click', function(e) {
                const icon = this.querySelector('.action-icon i');
                const originalClass = icon.className;
                
                // Show loading spinner
                icon.className = 'fas fa-spinner fa-spin';
                
                // Restore original icon after a delay (or when page loads)
                setTimeout(() => {
                    icon.className = originalClass;
                }, 2000);
            });
        }
    });
});

// Keyboard shortcuts for quick actions
document.addEventListener('keydown', function(e) {
    if (e.altKey) {
        const quickActions = document.querySelectorAll('.quick-action-card');
        const actionIndex = parseInt(e.key) - 1;
        
        if (actionIndex >= 0 && actionIndex < quickActions.length) {
            e.preventDefault();
            quickActions[actionIndex].click();
        }
    }
});

// Auto-refresh quick actions if needed (for dynamic badge counts)
function refreshQuickActionBadges() {
    // This would make an AJAX call to get updated badge counts
    // Implementation depends on your backend API
    fetch('/api/quick-actions/badges/')
        .then(response => response.json())
        .then(data => {
            Object.keys(data).forEach(actionId => {
                const badge = document.querySelector(`[data-action-id="${actionId}"] .action-badge`);
                if (badge) {
                    badge.textContent = data[actionId];
                    badge.style.display = data[actionId] > 0 ? 'flex' : 'none';
                }
            });
        })
        .catch(error => {
            console.error('Error refreshing quick action badges:', error);
        });
}

// Refresh badges every 5 minutes
setInterval(refreshQuickActionBadges, 300000);
</script>