// Dashboard JavaScript
document.addEventListener('DOMContentLoaded', function() {
    // Animate card values
    function animateValue(element, start, end, duration) {
        let startTimestamp = null;
        const step = (timestamp) => {
            if (!startTimestamp) startTimestamp = timestamp;
            const progress = Math.min((timestamp - startTimestamp) / duration, 1);
            const value = Math.floor(progress * (end - start) + start);
            element.textContent = value;
            if (progress < 1) {
                window.requestAnimationFrame(step);
            }
        };
        window.requestAnimationFrame(step);
    }

    // Animate card values on load
    const cardValues = document.querySelectorAll('.card-value');
    cardValues.forEach(cardValue => {
        const finalValue = parseInt(cardValue.textContent);
        if (finalValue > 0) {
            animateValue(cardValue, 0, finalValue, 1000);
        }
    });

    // Add click effects to cards
    const cards = document.querySelectorAll('.card');
    cards.forEach(card => {
        card.addEventListener('click', function(e) {
            if (e.target.tagName === 'A') return; // Don't trigger if clicking links
            
            const links = this.querySelectorAll('a');
            if (links.length > 0) {
                links[0].click(); // Click the first link in the card
            }
        });
        
        // Add cursor pointer to indicate clickability
        const hasLinks = card.querySelectorAll('a').length > 0;
        if (hasLinks) {
            card.style.cursor = 'pointer';
        }
    });

    // Auto-refresh dashboard data every 5 minutes
    setInterval(() => {
        // You can add AJAX call here to refresh data without page reload
        console.log('Dashboard data can be refreshed here...');
    }, 300000); // 5 minutes

    // Add loading state to action buttons
    const actionButtons = document.querySelectorAll('.action-btn');
    actionButtons.forEach(button => {
        button.addEventListener('click', function(e) {
            const originalText = this.querySelector('span').textContent;
            this.querySelector('span').textContent = 'Loading...';
            this.style.opacity = '0.7';
            
            setTimeout(() => {
                this.querySelector('span').textContent = originalText;
                this.style.opacity = '1';
            }, 1500);
        });
    });

    // Highlight active section
    const currentSection = window.location.hash;
    if (currentSection) {
        const targetElement = document.querySelector(currentSection);
        if (targetElement) {
            targetElement.scrollIntoView({ behavior: 'smooth' });
            targetElement.style.background = '#f0f8ff';
            setTimeout(() => {
                targetElement.style.background = '';
            }, 2000);
        }
    }
});

// Export for potential module use
if (typeof module !== 'undefined' && module.exports) {
    module.exports = {};
}