-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
746 lines (646 loc) · 26.6 KB
/
Copy pathapp.js
File metadata and controls
746 lines (646 loc) · 26.6 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
// Camva Pro CRM Dashboard JavaScript
// Supabase Configuration
const SUPABASE_URL = 'https://wpwdzccyvfupcjrwtaqb.supabase.co';
const SUPABASE_ANON_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Indwd2R6Y2N5dmZ1cGNqcnd0YXFiIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTQwNDE1MjUsImV4cCI6MjA2OTYxNzUyNX0.N8n1p9IrvLFODHs_pSCROSB3yzKHtqWSGL9SZkrs6v8';
// Initialize Supabase client
const supabaseClient = window.supabase.createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
// Plan Configuration
const PLANS = {
monthly: { name: 'Monthly', price: 49, duration: 30 },
quarterly: { name: 'Quarterly', price: 99, duration: 90 },
annual: { name: 'Annual', price: 299, duration: 365 }
};
// Message Templates
const MESSAGE_TEMPLATES = {
welcome: "🎉 Welcome to Camva Pro, {name}! Your {plan} subscription is now active. ✅ Plan: {plan} 📅 Valid Until: {expiry} 💰 Full access to premium features. Contact: +91 6387617678",
renewal: "✅ Subscription Renewed! Hi {name}, your Camva Pro subscription has been renewed. 🔄 New Plan: {plan} 📅 Valid Until: {expiry} Thank you for staying with Camva Pro! Contact: +91 6387617678",
reminder: "⏰ Subscription Reminder - {name}. Your Canva Pro {plan} subscription expires in {days} days. 📅 Expiry: {expiry} Renew now to avoid interruption! Contact: +91 6387617678"
};
// Global State
let customers = [];
let filteredCustomers = [];
// DOM Elements
const customersGrid = document.getElementById('customersGrid');
const loadingState = document.getElementById('loadingState');
const emptyState = document.getElementById('emptyState');
const searchInput = document.getElementById('searchInput');
const teamFilter = document.getElementById('teamFilter');
const planFilter = document.getElementById('planFilter');
const statusFilter = document.getElementById('statusFilter');
// Statistics Elements
const totalCustomersEl = document.getElementById('totalCustomers');
const activeSubscriptionsEl = document.getElementById('activeSubscriptions');
const expiringThisWeekEl = document.getElementById('expiringThisWeek');
const totalRevenueEl = document.getElementById('totalRevenue');
// Initialize Application
document.addEventListener('DOMContentLoaded', async () => {
await loadCustomers();
setupEventListeners();
setupFormHandlers();
});
// Setup Event Listeners
function setupEventListeners() {
// Header buttons
document.getElementById('addCustomerBtn').addEventListener('click', () => openModal('addCustomerModal'));
document.getElementById('importCustomerBtn').addEventListener('click', () => openModal('importCustomerModal'));
document.getElementById('exportCsvBtn').addEventListener('click', exportToCSV);
document.getElementById('refreshBtn').addEventListener('click', refreshData);
document.getElementById('viewExpiringBtn').addEventListener('click', showExpiringCustomers);
// Search and filters
searchInput.addEventListener('input', debounce(filterCustomers, 300));
teamFilter.addEventListener('change', filterCustomers);
planFilter.addEventListener('change', filterCustomers);
statusFilter.addEventListener('change', filterCustomers);
// Plan selection auto-fill payment
const planSelect = document.getElementById('planSelect');
if (planSelect) {
planSelect.addEventListener('change', (e) => {
const plan = e.target.value;
const paymentInput = document.getElementById('paymentAmount');
if (plan && PLANS[plan] && paymentInput) {
paymentInput.value = PLANS[plan].price;
}
});
}
// Close modals on outside click
document.addEventListener('click', (e) => {
if (e.target.classList.contains('modal')) {
closeModal(e.target.id);
}
});
}
// Setup Form Handlers
function setupFormHandlers() {
// Add Customer Form
const addCustomerForm = document.getElementById('addCustomerForm');
if (addCustomerForm) {
addCustomerForm.addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData(e.target);
const customerData = Object.fromEntries(formData.entries());
// Calculate dates
const joinedDate = new Date();
const plan = PLANS[customerData.plan];
const expiryDate = new Date(joinedDate);
expiryDate.setDate(expiryDate.getDate() + plan.duration);
const reminderDate = new Date(expiryDate);
reminderDate.setDate(reminderDate.getDate() - 3);
customerData.joined = joinedDate.toISOString().split('T')[0];
customerData.reminder = reminderDate.toISOString().split('T')[0];
customerData.expired = expiryDate.toISOString().split('T')[0];
customerData.total_payment = parseFloat(customerData.payment);
await addCustomer(customerData);
});
}
// Import Customer Form
const importCustomerForm = document.getElementById('importCustomerForm');
if (importCustomerForm) {
importCustomerForm.addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData(e.target);
const customerData = Object.fromEntries(formData.entries());
customerData.total_payment = parseFloat(customerData.payment);
await addCustomer(customerData);
});
}
// Edit Customer Form
const editCustomerForm = document.getElementById('editCustomerForm');
if (editCustomerForm) {
editCustomerForm.addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData(e.target);
const customerData = Object.fromEntries(formData.entries());
const customerId = customerData.id;
delete customerData.id;
customerData.total_payment = parseFloat(customerData.payment);
await updateCustomer(customerId, customerData);
});
}
}
// Load All Customers
async function loadCustomers() {
try {
showLoading(true);
// Try to load customers from Supabase
const { data, error } = await supabaseClient
.from('customers')
.select('*')
.order('timestamp', { ascending: false });
if (error) {
console.error('Supabase error:', error);
// Initialize with sample data if Supabase fails
initializeSampleData();
} else {
customers = data || [];
}
filteredCustomers = [...customers];
renderCustomers();
updateStatistics();
showLoading(false);
} catch (error) {
console.error('Error loading customers:', error);
// Initialize with sample data as fallback
initializeSampleData();
showLoading(false);
}
}
// Initialize with sample data for demo purposes
function initializeSampleData() {
const today = new Date();
const sampleCustomers = [
{
id: 'sample-1',
name: 'Rajesh Kumar',
email: 'rajesh@example.com',
phone: '+919876543210',
team: 'Team 1',
plan: 'monthly',
payment: 49,
joined: new Date(today.getTime() - 15 * 24 * 60 * 60 * 1000).toISOString().split('T')[0],
reminder: new Date(today.getTime() + 12 * 24 * 60 * 60 * 1000).toISOString().split('T')[0],
expired: new Date(today.getTime() + 15 * 24 * 60 * 60 * 1000).toISOString().split('T')[0],
total_payment: 49,
additional_notes: 'Good customer'
},
{
id: 'sample-2',
name: 'Priya Sharma',
email: 'priya@example.com',
phone: '+919876543211',
team: 'Team 2',
plan: 'quarterly',
payment: 99,
joined: new Date(today.getTime() - 80 * 24 * 60 * 60 * 1000).toISOString().split('T')[0],
reminder: new Date(today.getTime() + 7 * 24 * 60 * 60 * 1000).toISOString().split('T')[0],
expired: new Date(today.getTime() + 10 * 24 * 60 * 60 * 1000).toISOString().split('T')[0],
total_payment: 99,
additional_notes: 'Expiring soon'
},
{
id: 'sample-3',
name: 'Amit Patel',
email: 'amit@example.com',
phone: '+919876543212',
team: 'Team 3',
plan: 'annual',
payment: 299,
joined: new Date(today.getTime() - 350 * 24 * 60 * 60 * 1000).toISOString().split('T')[0],
reminder: new Date(today.getTime() - 18 * 24 * 60 * 60 * 1000).toISOString().split('T')[0],
expired: new Date(today.getTime() - 15 * 24 * 60 * 60 * 1000).toISOString().split('T')[0],
total_payment: 299,
additional_notes: 'Expired customer'
}
];
customers = sampleCustomers;
filteredCustomers = [...customers];
renderCustomers();
updateStatistics();
showToast('Demo data loaded. Add real customers to get started!', 'info');
}
// Add New Customer
async function addCustomer(customerData) {
try {
// Try Supabase first
const { data, error } = await supabaseClient
.from('customers')
.insert([customerData])
.select();
if (error) {
// Fallback to local storage
const newCustomer = {
...customerData,
id: 'local-' + Date.now(),
timestamp: new Date().toISOString()
};
customers.unshift(newCustomer);
} else {
customers.unshift(data[0]);
}
filterCustomers();
updateStatistics();
closeModal('addCustomerModal');
closeModal('importCustomerModal');
showToast('Customer added successfully!', 'success');
// Clear forms
document.getElementById('addCustomerForm').reset();
document.getElementById('importCustomerForm').reset();
} catch (error) {
console.error('Error adding customer:', error);
// Fallback: add to local array
const newCustomer = {
...customerData,
id: 'local-' + Date.now(),
timestamp: new Date().toISOString()
};
customers.unshift(newCustomer);
filterCustomers();
updateStatistics();
closeModal('addCustomerModal');
closeModal('importCustomerModal');
showToast('Customer added locally (Supabase unavailable)', 'warning');
// Clear forms
const addForm = document.getElementById('addCustomerForm');
const importForm = document.getElementById('importCustomerForm');
if (addForm) addForm.reset();
if (importForm) importForm.reset();
}
}
// Update Customer
async function updateCustomer(customerId, customerData) {
try {
const { data, error } = await supabaseClient
.from('customers')
.update(customerData)
.eq('id', customerId)
.select();
if (error) throw error;
const customerIndex = customers.findIndex(c => c.id === customerId);
if (customerIndex !== -1) {
customers[customerIndex] = { ...customers[customerIndex], ...data[0] };
}
filterCustomers();
updateStatistics();
closeModal('editCustomerModal');
showToast('Customer updated successfully!', 'success');
} catch (error) {
console.error('Error updating customer:', error);
// Fallback: update in local array
const customerIndex = customers.findIndex(c => c.id === customerId);
if (customerIndex !== -1) {
customers[customerIndex] = { ...customers[customerIndex], ...customerData };
filterCustomers();
updateStatistics();
closeModal('editCustomerModal');
showToast('Customer updated locally (Supabase unavailable)', 'warning');
} else {
showToast('Error updating customer', 'error');
}
}
}
// Delete Customer
async function deleteCustomer(customerId) {
if (!confirm('Are you sure you want to delete this customer?')) return;
try {
const { error } = await supabaseClient
.from('customers')
.delete()
.eq('id', customerId);
if (error) throw error;
customers = customers.filter(c => c.id !== customerId);
filterCustomers();
updateStatistics();
showToast('Customer deleted successfully!', 'success');
} catch (error) {
console.error('Error deleting customer:', error);
// Fallback: remove from local array
customers = customers.filter(c => c.id !== customerId);
filterCustomers();
updateStatistics();
showToast('Customer deleted locally (Supabase unavailable)', 'warning');
}
}
// Filter Customers
function filterCustomers() {
const searchTerm = searchInput.value.toLowerCase();
const teamValue = teamFilter.value;
const planValue = planFilter.value;
const statusValue = statusFilter.value;
filteredCustomers = customers.filter(customer => {
const matchesSearch = !searchTerm ||
customer.name.toLowerCase().includes(searchTerm) ||
customer.email.toLowerCase().includes(searchTerm) ||
customer.phone.includes(searchTerm);
const matchesTeam = !teamValue || customer.team === teamValue;
const matchesPlan = !planValue || customer.plan === planValue;
let matchesStatus = true;
if (statusValue) {
const status = getCustomerStatus(customer);
matchesStatus = status.toLowerCase() === statusValue;
}
return matchesSearch && matchesTeam && matchesPlan && matchesStatus;
});
renderCustomers();
}
// Render Customers
function renderCustomers() {
if (filteredCustomers.length === 0) {
customersGrid.style.display = 'none';
emptyState.classList.remove('hidden');
return;
}
customersGrid.style.display = 'grid';
emptyState.classList.add('hidden');
customersGrid.innerHTML = filteredCustomers.map(customer => createCustomerCard(customer)).join('');
}
// Create Customer Card HTML
function createCustomerCard(customer) {
const status = getCustomerStatus(customer);
const statusClass = `customer-status--${status.toLowerCase()}`;
const initials = customer.name.split(' ').map(n => n[0]).join('').toUpperCase();
const expiryDate = new Date(customer.expired);
const formattedExpiry = expiryDate.toLocaleDateString('en-IN');
const planInfo = PLANS[customer.plan];
return `
<div class="customer-card">
<div class="customer-header">
<div class="customer-avatar">${initials}</div>
<div class="customer-info">
<h3>${customer.name}</h3>
<p>${customer.email}</p>
</div>
</div>
<div class="customer-body">
<div class="customer-details">
<div class="detail-row">
<span class="detail-label">Phone:</span>
<span class="detail-value">${customer.phone}</span>
</div>
<div class="detail-row">
<span class="detail-label">Team:</span>
<span class="detail-value">${customer.team}</span>
</div>
<div class="detail-row">
<span class="detail-label">Plan:</span>
<span class="detail-value">${planInfo?.name || customer.plan} - ₹${customer.payment}</span>
</div>
<div class="detail-row">
<span class="detail-label">Expiry:</span>
<span class="detail-value">${formattedExpiry}</span>
</div>
<div class="detail-row">
<span class="detail-label">Status:</span>
<span class="customer-status ${statusClass}">
<i class="fas fa-circle"></i>
${status}
</span>
</div>
<div class="detail-row">
<span class="detail-label">Total Paid:</span>
<span class="detail-value">₹${customer.total_payment || customer.payment}</span>
</div>
</div>
<div class="customer-actions">
<button class="btn btn--outline" onclick="editCustomer('${customer.id}')">
<i class="fas fa-edit"></i> Edit
</button>
<button class="btn btn--outline" onclick="sendWhatsApp('${customer.id}')">
<i class="fab fa-whatsapp"></i> WhatsApp
</button>
<button class="btn btn--outline" onclick="sendEmail('${customer.id}')">
<i class="fas fa-envelope"></i> Email
</button>
<button class="btn btn--primary" onclick="renewCustomer('${customer.id}')">
<i class="fas fa-sync-alt"></i> Renew
</button>
</div>
</div>
</div>
`;
}
// Get Customer Status
function getCustomerStatus(customer) {
const today = new Date();
const expiryDate = new Date(customer.expired);
const diffTime = expiryDate - today;
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
if (diffDays < 0) return 'Expired';
if (diffDays <= 7) return 'Expiring';
return 'Active';
}
// Update Statistics
function updateStatistics() {
const total = customers.length;
const today = new Date();
const weekFromNow = new Date(today.getTime() + 7 * 24 * 60 * 60 * 1000);
let active = 0;
let expiring = 0;
let totalRevenue = 0;
customers.forEach(customer => {
const expiryDate = new Date(customer.expired);
totalRevenue += parseFloat(customer.total_payment || customer.payment || 0);
if (expiryDate >= today) {
active++;
if (expiryDate <= weekFromNow) {
expiring++;
}
}
});
totalCustomersEl.textContent = total;
activeSubscriptionsEl.textContent = active;
expiringThisWeekEl.textContent = expiring;
totalRevenueEl.textContent = `₹${totalRevenue.toLocaleString('en-IN')}`;
}
// Edit Customer
function editCustomer(customerId) {
const customer = customers.find(c => c.id === customerId);
if (!customer) return;
const form = document.getElementById('editCustomerForm');
// Fill form with customer data
form.querySelector('input[name="id"]').value = customer.id;
form.querySelector('input[name="name"]').value = customer.name;
form.querySelector('input[name="email"]').value = customer.email;
form.querySelector('input[name="phone"]').value = customer.phone;
form.querySelector('select[name="team"]').value = customer.team;
form.querySelector('select[name="plan"]').value = customer.plan;
form.querySelector('input[name="payment"]').value = customer.payment;
form.querySelector('textarea[name="additional_notes"]').value = customer.additional_notes || '';
openModal('editCustomerModal');
}
// Renew Customer
function renewCustomer(customerId) {
const customer = customers.find(c => c.id === customerId);
if (!customer) return;
// Pre-fill add customer form with existing data for renewal
const form = document.getElementById('addCustomerForm');
form.querySelector('input[name="name"]').value = customer.name;
form.querySelector('input[name="email"]').value = customer.email;
form.querySelector('input[name="phone"]').value = customer.phone;
form.querySelector('select[name="team"]').value = customer.team;
form.querySelector('select[name="plan"]').value = customer.plan;
form.querySelector('input[name="payment"]').value = customer.payment;
openModal('addCustomerModal');
}
// Send WhatsApp Message
function sendWhatsApp(customerId) {
const customer = customers.find(c => c.id === customerId);
if (!customer) return;
const status = getCustomerStatus(customer);
let template = MESSAGE_TEMPLATES.welcome;
if (status === 'Expiring') {
template = MESSAGE_TEMPLATES.reminder;
}
const expiryDate = new Date(customer.expired);
const today = new Date();
const diffDays = Math.ceil((expiryDate - today) / (1000 * 60 * 60 * 24));
const message = template
.replace(/{name}/g, customer.name)
.replace(/{plan}/g, PLANS[customer.plan]?.name || customer.plan)
.replace(/{expiry}/g, expiryDate.toLocaleDateString('en-IN'))
.replace(/{days}/g, diffDays);
const phoneNumber = customer.phone.replace(/[^0-9]/g, '');
const whatsappUrl = `https://api.whatsapp.com/send?phone=${phoneNumber}&text=${encodeURIComponent(message)}`;
window.open(whatsappUrl, '_blank');
}
// Send Email
function sendEmail(customerId) {
const customer = customers.find(c => c.id === customerId);
if (!customer) return;
const status = getCustomerStatus(customer);
let subject = 'Welcome to Camva Pro!';
let template = MESSAGE_TEMPLATES.welcome;
if (status === 'Expiring') {
subject = 'Subscription Reminder - Camva Pro';
template = MESSAGE_TEMPLATES.reminder;
}
const expiryDate = new Date(customer.expired);
const today = new Date();
const diffDays = Math.ceil((expiryDate - today) / (1000 * 60 * 60 * 1000));
const body = template
.replace(/{name}/g, customer.name)
.replace(/{plan}/g, PLANS[customer.plan]?.name || customer.plan)
.replace(/{expiry}/g, expiryDate.toLocaleDateString('en-IN'))
.replace(/{days}/g, diffDays);
const mailtoUrl = `mailto:${customer.email}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;
window.location.href = mailtoUrl;
}
// Show Expiring Customers
function showExpiringCustomers() {
statusFilter.value = 'expiring';
filterCustomers();
showToast('Showing customers expiring within 7 days', 'info');
}
// Export to CSV
function exportToCSV() {
if (customers.length === 0) {
showToast('No customers to export', 'warning');
return;
}
const headers = ['Name', 'Email', 'Phone', 'Team', 'Plan', 'Payment', 'Joined', 'Expiry', 'Status', 'Total Payment'];
const csvContent = [
headers.join(','),
...customers.map(customer => {
const status = getCustomerStatus(customer);
return [
`"${customer.name}"`,
`"${customer.email}"`,
`"${customer.phone}"`,
`"${customer.team}"`,
`"${PLANS[customer.plan]?.name || customer.plan}"`,
customer.payment,
customer.joined,
customer.expired,
status,
customer.total_payment || customer.payment
].join(',');
})
].join('\n');
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const link = document.createElement('a');
const url = URL.createObjectURL(blob);
link.setAttribute('href', url);
link.setAttribute('download', `camva-pro-customers-${new Date().toISOString().split('T')[0]}.csv`);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
showToast('Customer data exported successfully!', 'success');
}
// Refresh Data
async function refreshData() {
const refreshBtn = document.getElementById('refreshBtn');
const icon = refreshBtn.querySelector('i');
icon.classList.add('spinning');
await loadCustomers();
setTimeout(() => {
icon.classList.remove('spinning');
}, 1000);
showToast('Data refreshed successfully!', 'success');
}
// Modal Functions
function openModal(modalId) {
const modal = document.getElementById(modalId);
if (modal) {
modal.classList.remove('hidden');
document.body.style.overflow = 'hidden';
}
}
function closeModal(modalId) {
const modal = document.getElementById(modalId);
if (modal) {
modal.classList.add('hidden');
document.body.style.overflow = 'auto';
// Clear forms
const forms = modal.querySelectorAll('form');
forms.forEach(form => form.reset());
}
}
// Show/Hide Loading State
function showLoading(show) {
if (show) {
loadingState.style.display = 'flex';
customersGrid.style.display = 'none';
emptyState.classList.add('hidden');
} else {
loadingState.style.display = 'none';
}
}
// Toast Notification System
function showToast(message, type = 'info', duration = 5000) {
const toastContainer = document.getElementById('toastContainer');
const toast = document.createElement('div');
const toastId = 'toast-' + Date.now();
const icons = {
success: 'fas fa-check-circle',
error: 'fas fa-exclamation-circle',
warning: 'fas fa-exclamation-triangle',
info: 'fas fa-info-circle'
};
toast.id = toastId;
toast.className = `toast toast--${type}`;
toast.innerHTML = `
<i class="toast-icon ${icons[type]}"></i>
<div class="toast-content">
<div class="toast-message">${message}</div>
</div>
<button class="toast-close" onclick="removeToast('${toastId}')">
<i class="fas fa-times"></i>
</button>
`;
toastContainer.appendChild(toast);
// Auto remove after duration
setTimeout(() => {
removeToast(toastId);
}, duration);
}
function removeToast(toastId) {
const toast = document.getElementById(toastId);
if (toast) {
toast.style.transform = 'translateX(100%)';
toast.style.opacity = '0';
setTimeout(() => {
toast.remove();
}, 300);
}
}
// Utility Functions
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// Global functions for HTML onclick events
window.openModal = openModal;
window.closeModal = closeModal;
window.editCustomer = editCustomer;
window.renewCustomer = renewCustomer;
window.sendWhatsApp = sendWhatsApp;
window.sendEmail = sendEmail;
window.removeToast = removeToast;
window.openAddCustomerModal = () => openModal('addCustomerModal');
window.deleteCustomer = deleteCustomer;