File size: 4,148 Bytes
3129a87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Main application script
document.addEventListener('DOMContentLoaded', function() {
    initializeDashboard();
    setupEventListeners();
});

// Initialize dashboard with mock data
function initializeDashboard() {
    // Mock data for demonstration
    const mockStats = {
        activeUsers: 245,
        webhookEvents: 1567,
        monthlyRevenue: '$12,450'
    };

    const mockActivity = [
        {
            type: 'purchase',
            user: 'john@example.com',
            plan: 'Premium Plan',
            time: '2 hours ago',
            status: 'success'
        },
        {
            type: 'cancellation',
            user: 'jane@example.com',
            plan: 'Basic Plan',
            time: '5 hours ago',
            status: 'warning'
        },
        {
            type: 'webhook',
            user: 'system',
            plan: 'N/A',
            time: '1 day ago',
            status: 'info'
        },
        {
            type: 'upgrade',
            user: 'mike@example.com',
            plan: 'Enterprise Plan',
            time: '2 days ago',
            status: 'success'
        }
    ];

    // Update dashboard stats
    updateDashboardStats(mockStats);
    updateRecentActivity(mockActivity);
}

// Update dashboard statistics
function updateDashboardStats(stats) {
    const activeUsersElement = document.getElementById('activeUsersCount');
    const webhookEventsElement = document.getElementById('webhookEventsCount');
    const monthlyRevenueElement = document.getElementById('monthlyRevenue');

    if (activeUsersElement) activeUsersElement.textContent = stats.activeUsers;
    if (webhookEventsElement) textContent = stats.webhookEvents;
    if (monthlyRevenueElement) textContent = stats.monthlyRevenue;
}

// Update recent activity feed
function updateRecentActivity(activities) {
    const activityContainer = document.getElementById('recentActivity');
    
    if (!activityContainer) return;

    activityContainer.innerHTML = activities.map(activity => `
        <div class="flex items-center justify-between p-4 bg-gray-50 rounded-lg">
            <div class="flex items-center space-x-4">
                <div class="w-3 h-3 rounded-full ${
                    activity.status === 'success' ? 'bg-green-500' : 
                    activity.status === 'warning' ? 'bg-yellow-500' : 'bg-blue-500'
                }"></div>
                <div>
                    <p class="font-medium text-gray-800">
                        ${activity.user} - ${activity.type}
                    </p>
                    <p class="text-sm text-gray-600">${activity.plan}</p>
                </div>
            </div>
            <div class="text-right">
                <p class="text-sm text-gray-600">${activity.time}</p>
                <span class="inline-block px-2 py-1 text-xs font-medium rounded ${
                    activity.status === 'success' ? 'bg-green-100 text-green-800' : 
                    activity.status === 'warning' ? 'bg-yellow-100 text-yellow-800' : 'bg-blue-100 text-blue-800'
                }">
                    ${activity.status}
                </span>
            </div>
        </div>
    `).join('');
}

// Setup event listeners
function setupEventListeners() {
    // Add any global event listeners here
    console.log('Event listeners initialized');
}

// Utility function to format dates
function formatDate(date) {
    return new Date(date).toLocaleDateString('en-US', {
        year: 'numeric',
        month: 'short',
        day: 'numeric'
    });
}

// API integration functions (for future use)
async function fetchDashboardData() {
    try {
        // This would be replaced with actual API calls
        const response = await fetch('/api/dashboard');
        if (!response.ok) throw new Error('Failed to fetch dashboard data');
        return await response.json();
    } catch (error) {
        console.error('Error fetching dashboard data:', error);
        return null;
    }
}

// Export functions for use in other modules
window.HotmartFlow = {
    initializeDashboard,
    updateDashboardStats,
    updateRecentActivity,
    fetchDashboardData
};