File size: 36,262 Bytes
01278f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Business Invoice Generator</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        .invoice-item:last-child {
            border-bottom: none;
        }
        
        .modal {
            animation: fadeIn 0.3s ease-out;
        }
        
        #invoice-preview {
            background-color: white;
            color: black;
            box-shadow: 0 0 20px rgba(0,0,0,0.1);
        }
        
        #logo-preview {
            max-width: 150px;
            max-height: 80px;
            object-fit: contain;
        }
        
        .logo-upload-container {
            border: 2px dashed #d1d5db;
            border-radius: 0.5rem;
            padding: 1rem;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s;
        }
        
        .logo-upload-container:hover {
            border-color: #3b82f6;
            background-color: #f8fafc;
        }
    </style>
</head>
<body class="bg-gray-100 min-h-screen">
    <div class="container mx-auto px-4 py-8">
        <div class="flex justify-between items-center mb-8">
            <div class="flex items-center">
                <h1 class="text-3xl font-bold text-gray-800">
                    <i class="fas fa-file-invoice-dollar mr-2 text-blue-600"></i>
                    Invoice Generator
                </h1>
                <button id="theme-toggle" type="button" class="ml-4 p-2 rounded-full bg-gray-200 text-gray-700">
                    <i id="theme-icon" class="fas fa-moon"></i>
                </button>
            </div>
            <button id="preview-btn" class="bg-blue-600 hover:bg-blue-700 text-white font-medium py-2 px-4 rounded-lg transition flex items-center">
                <i class="fas fa-eye mr-2"></i> Preview Invoice
            </button>
        </div>
        
        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
            <!-- Invoice Form -->
            <div class="lg:col-span-2 bg-white rounded-xl shadow-md p-6">
                <h2 class="text-xl font-semibold mb-4 text-gray-800">Invoice Details</h2>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-6">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Invoice Number</label>
                        <input type="text" id="invoice-number" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" value="INV-0001">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Date</label>
                        <input type="date" id="invoice-date" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Due Date</label>
                        <input type="date" id="due-date" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Currency</label>
                        <select id="currency" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
                            <option value="TSh">Tanzania (TSh)</option>
                            <option value="KSh">Kenya (KSh)</option>
                            <option value="USh">Uganda (USh)</option>
                            <option value="RF">Rwanda (RF)</option>
                            <option value="FBu">Burundi (FBu)</option>
                            <option value="$">USD ($)</option>
                            <option value="€">EUR (€)</option>
                            <option value="£">GBP (£)</option>
                        </select>
                    </div>
                </div>
                
                <!-- Logo Upload -->
                <div class="mb-6">
                    <label class="block text-sm font-medium text-gray-700 mb-2">Company Logo</label>
                    <div id="logo-upload" class="logo-upload-container">
                        <div id="logo-upload-placeholder" class="flex flex-col items-center justify-center">
                            <i class="fas fa-image text-3xl text-gray-400 mb-2"></i>
                            <p class="text-sm text-gray-500">Click to upload logo</p>
                            <p class="text-xs text-gray-400">(Recommended: 300x100px)</p>
                        </div>
                        <img id="logo-preview" class="hidden mx-auto mb-2" alt="Logo Preview">
                        <input type="file" id="logo-input" accept="image/*" class="hidden">
                    </div>
                    <button id="remove-logo-btn" class="mt-2 text-sm text-red-600 hover:text-red-800 hidden">
                        <i class="fas fa-trash mr-1"></i> Remove Logo
                    </button>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
                    <!-- From Section -->
                    <div>
                        <h3 class="text-lg font-medium mb-2 text-gray-800 flex items-center">
                            <i class="fas fa-building mr-2 text-blue-600"></i> From
                        </h3>
                        <div class="space-y-3">
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Business Name</label>
                                <input type="text" id="from-name" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" value="Your Company Name">
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Email</label>
                                <input type="email" id="from-email" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" value="contact@yourcompany.com">
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Phone</label>
                                <input type="text" id="from-phone" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" value="+255 123 456 789">
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Address</label>
                                <textarea id="from-address" rows="3" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">123 Business St, Dar es Salaam
Tanzania</textarea>
                            </div>
                        </div>
                    </div>
                    
                    <!-- To Section -->
                    <div>
                        <h3 class="text-lg font-medium mb-2 text-gray-800 flex items-center">
                            <i class="fas fa-user-tie mr-2 text-blue-600"></i> To
                        </h3>
                        <div class="space-y-3">
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Client Name</label>
                                <input type="text" id="to-name" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" value="Client Name">
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Email</label>
                                <input type="email" id="to-email" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" value="client@example.com">
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Phone</label>
                                <input type="text" id="to-phone" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" value="+255 987 654 321">
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Address</label>
                                <textarea id="to-address" rows="3" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">456 Client Ave, Dar es Salaam
Tanzania</textarea>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- Items Section -->
                <div class="mb-6">
                    <div class="flex justify-between items-center mb-4">
                        <h3 class="text-lg font-medium text-gray-800 flex items-center">
                            <i class="fas fa-list-ul mr-2 text-blue-600"></i> Items
                        </h3>
                        <button id="add-item-btn" class="bg-green-600 hover:bg-green-700 text-white font-medium py-1 px-3 rounded-md text-sm transition flex items-center">
                            <i class="fas fa-plus mr-1"></i> Add Item
                        </button>
                    </div>
                    
                    <div id="invoice-items" class="space-y-4">
                        <!-- Item template will be added here -->
                    </div>
                    
                    <div class="mt-6 grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Notes</label>
                            <textarea id="notes" rows="3" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">Thank you for your business!</textarea>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Terms</label>
                            <textarea id="terms" rows="3" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">Payment due within 15 days. Late payments subject to 5% fee.</textarea>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Preview Section -->
            <div class="lg:col-span-1">
                <div class="bg-white rounded-xl shadow-md p-6 sticky top-6">
                    <h2 class="text-xl font-semibold mb-4 text-gray-800">Invoice Summary</h2>
                    
                    <div class="space-y-4">
                        <div class="flex justify-between">
                            <span class="text-gray-600">Subtotal:</span>
                            <span id="subtotal" class="font-medium">0.00</span>
                        </div>
                        <div class="flex justify-between">
                            <span class="text-gray-600">Tax (%):</span>
                            <div class="flex items-center">
                                <input type="number" id="tax-rate" min="0" max="100" value="18" class="w-16 px-2 py-1 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
                                <span class="ml-2">%</span>
                            </div>
                        </div>
                        <div class="flex justify-between">
                            <span class="text-gray-600">Tax Amount:</span>
                            <span id="tax-amount" class="font-medium">0.00</span>
                        </div>
                        <div class="border-t border-gray-200 pt-2">
                            <div class="flex justify-between font-bold text-lg">
                                <span>Total:</span>
                                <span id="total" class="text-blue-600">0.00</span>
                            </div>
                        </div>
                    </div>
                    
                    <div class="mt-6 space-y-3">
                        <button id="download-pdf-btn" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-medium py-2 px-4 rounded-md transition flex items-center justify-center">
                            <i class="fas fa-file-pdf mr-2"></i> Download PDF
                        </button>
                        <button id="download-png-btn" class="w-full bg-gray-200 hover:bg-gray-300 text-gray-800 font-medium py-2 px-4 rounded-md transition flex items-center justify-center">
                            <i class="fas fa-image mr-2"></i> Download PNG
                        </button>
                        <button id="copy-link-btn" class="w-full bg-gray-200 hover:bg-gray-300 text-gray-800 font-medium py-2 px-4 rounded-md transition flex items-center justify-center">
                            <i class="fas fa-link mr-2"></i> Copy Share Link
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Preview Modal -->
    <div id="preview-modal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden">
        <div class="bg-white rounded-xl shadow-xl max-w-4xl w-full max-h-[90vh] overflow-auto">
            <div class="flex justify-between items-center border-b border-gray-200 p-4">
                <h3 class="text-xl font-semibold text-gray-800">Invoice Preview</h3>
                <button id="close-preview-btn" class="text-gray-500 hover:text-gray-700">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            <div class="p-6">
                <div id="invoice-preview" class="p-8 mx-auto">
                    <!-- Invoice preview will be rendered here -->
                </div>
            </div>
            <div class="border-t border-gray-200 p-4 flex justify-end">
                <button id="download-from-preview-btn" class="bg-blue-600 hover:bg-blue-700 text-white font-medium py-2 px-4 rounded-md transition flex items-center">
                    <i class="fas fa-download mr-2"></i> Download
                </button>
            </div>
        </div>
    </div>
    
    <script>
        // Set today's date as default
        document.addEventListener('DOMContentLoaded', function() {
            const today = new Date().toISOString().split('T')[0];
            document.getElementById('invoice-date').value = today;
            
            // Set due date to 15 days from today
            const dueDate = new Date();
            dueDate.setDate(dueDate.getDate() + 15);
            document.getElementById('due-date').value = dueDate.toISOString().split('T')[0];
            
            // Initialize invoice items
            addInvoiceItem();
            
            // Theme toggle functionality
            const themeToggle = document.getElementById('theme-toggle');
            const themeIcon = document.getElementById('theme-icon');
            
            themeToggle.addEventListener('click', function() {
                document.documentElement.classList.toggle('dark');
                
                if (document.documentElement.classList.contains('dark')) {
                    themeIcon.classList.remove('fa-moon');
                    themeIcon.classList.add('fa-sun');
                    localStorage.setItem('theme', 'dark');
                } else {
                    themeIcon.classList.remove('fa-sun');
                    themeIcon.classList.add('fa-moon');
                    localStorage.setItem('theme', 'light');
                }
            });
            
            // Check for saved theme preference
            if (localStorage.getItem('theme') === 'dark') {
                document.documentElement.classList.add('dark');
                themeIcon.classList.remove('fa-moon');
                themeIcon.classList.add('fa-sun');
            }
            
            // Logo upload functionality
            const logoUpload = document.getElementById('logo-upload');
            const logoInput = document.getElementById('logo-input');
            const logoPreview = document.getElementById('logo-preview');
            const logoPlaceholder = document.getElementById('logo-upload-placeholder');
            const removeLogoBtn = document.getElementById('remove-logo-btn');
            
            logoUpload.addEventListener('click', function() {
                logoInput.click();
            });
            
            logoInput.addEventListener('change', function(e) {
                if (e.target.files.length) {
                    const reader = new FileReader();
                    reader.onload = function(event) {
                        logoPreview.src = event.target.result;
                        logoPreview.classList.remove('hidden');
                        logoPlaceholder.classList.add('hidden');
                        removeLogoBtn.classList.remove('hidden');
                    };
                    reader.readAsDataURL(e.target.files[0]);
                }
            });
            
            removeLogoBtn.addEventListener('click', function(e) {
                e.stopPropagation();
                logoPreview.src = '';
                logoPreview.classList.add('hidden');
                logoPlaceholder.classList.remove('hidden');
                removeLogoBtn.classList.add('hidden');
                logoInput.value = '';
            });
            
            // Preview button functionality
            const previewBtn = document.getElementById('preview-btn');
            const previewModal = document.getElementById('preview-modal');
            const closePreviewBtn = document.getElementById('close-preview-btn');
            
            previewBtn.addEventListener('click', function() {
                renderInvoicePreview();
                previewModal.classList.remove('hidden');
            });
            
            closePreviewBtn.addEventListener('click', function() {
                previewModal.classList.add('hidden');
            });
            
            // Close modal when clicking outside
            previewModal.addEventListener('click', function(e) {
                if (e.target === previewModal) {
                    previewModal.classList.add('hidden');
                }
            });
            
            // Download buttons functionality
            const downloadPdfBtn = document.getElementById('download-pdf-btn');
            const downloadPngBtn = document.getElementById('download-png-btn');
            const downloadFromPreviewBtn = document.getElementById('download-from-preview-btn');
            
            downloadPdfBtn.addEventListener('click', generatePdf);
            downloadPngBtn.addEventListener('click', generatePng);
            downloadFromPreviewBtn.addEventListener('click', generatePdf);
            
            // Copy link button functionality
            document.getElementById('copy-link-btn').addEventListener('click', function() {
                // In a real app, this would generate a shareable link
                alert('Share link copied to clipboard!');
            });
        });
        
        // Invoice item template
        function getInvoiceItemTemplate(index = 0) {
            return `
                <div class="invoice-item bg-gray-50 p-4 rounded-lg border border-gray-200" data-index="${index}">
                    <div class="grid grid-cols-12 gap-3">
                        <div class="col-span-12 md:col-span-5">
                            <label class="block text-sm font-medium text-gray-700 mb-1">Description</label>
                            <input type="text" class="item-description w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="Item description">
                        </div>
                        <div class="col-span-6 md:col-span-2">
                            <label class="block text-sm font-medium text-gray-700 mb-1">Quantity</label>
                            <input type="number" min="1" value="1" class="item-quantity w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
                        </div>
                        <div class="col-span-6 md:col-span-2">
                            <label class="block text-sm font-medium text-gray-700 mb-1">Price</label>
                            <input type="number" min="0" step="0.01" value="0.00" class="item-price w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
                        </div>
                        <div class="col-span-9 md:col-span-2">
                            <label class="block text-sm font-medium text-gray-700 mb-1">Amount</label>
                            <input type="text" class="item-amount w-full px-3 py-2 border border-gray-300 rounded-md bg-gray-100 font-medium" readonly>
                        </div>
                        <div class="col-span-3 md:col-span-1 flex items-end">
                            <button class="remove-item-btn w-full py-2 text-red-600 hover:text-red-800">
                                <i class="fas fa-trash"></i>
                            </button>
                        </div>
                    </div>
                </div>
            `;
        }
        
        // Add new invoice item
        function addInvoiceItem() {
            const itemsContainer = document.getElementById('invoice-items');
            const index = itemsContainer.children.length;
            const itemTemplate = getInvoiceItemTemplate(index);
            
            itemsContainer.insertAdjacentHTML('beforeend', itemTemplate);
            
            // Add event listeners to the new item
            const newItem = itemsContainer.lastElementChild;
            const quantityInput = newItem.querySelector('.item-quantity');
            const priceInput = newItem.querySelector('.item-price');
            const amountInput = newItem.querySelector('.item-amount');
            const removeBtn = newItem.querySelector('.remove-item-btn');
            
            quantityInput.addEventListener('input', calculateItemAmount);
            priceInput.addEventListener('input', calculateItemAmount);
            
            removeBtn.addEventListener('click', function() {
                newItem.remove();
                calculateInvoiceTotal();
            });
            
            // Calculate initial amount
            calculateItemAmount.call(quantityInput);
        }
        
        // Calculate item amount
        function calculateItemAmount() {
            const item = this.closest('.invoice-item');
            const quantity = parseFloat(item.querySelector('.item-quantity').value) || 0;
            const price = parseFloat(item.querySelector('.item-price').value) || 0;
            const amount = quantity * price;
            
            item.querySelector('.item-amount').value = amount.toFixed(2);
            calculateInvoiceTotal();
        }
        
        // Calculate invoice total
        function calculateInvoiceTotal() {
            const items = document.querySelectorAll('.invoice-item');
            let subtotal = 0;
            
            items.forEach(item => {
                const amount = parseFloat(item.querySelector('.item-amount').value) || 0;
                subtotal += amount;
            });
            
            const taxRate = parseFloat(document.getElementById('tax-rate').value) || 0;
            const taxAmount = subtotal * (taxRate / 100);
            const total = subtotal + taxAmount;
            
            document.getElementById('subtotal').textContent = subtotal.toFixed(2);
            document.getElementById('tax-amount').textContent = taxAmount.toFixed(2);
            document.getElementById('total').textContent = total.toFixed(2);
        }
        
        // Add item button
        document.getElementById('add-item-btn').addEventListener('click', addInvoiceItem);
        
        // Tax rate change
        document.getElementById('tax-rate').addEventListener('input', calculateInvoiceTotal);
        
        // Render invoice preview
        function renderInvoicePreview() {
            const previewContainer = document.getElementById('invoice-preview');
            const currency = document.getElementById('currency').value;
            const invoiceNumber = document.getElementById('invoice-number').value;
            const invoiceDate = document.getElementById('invoice-date').value;
            const dueDate = document.getElementById('due-date').value;
            const fromName = document.getElementById('from-name').value;
            const fromEmail = document.getElementById('from-email').value;
            const fromPhone = document.getElementById('from-phone').value;
            const fromAddress = document.getElementById('from-address').value;
            const toName = document.getElementById('to-name').value;
            const toEmail = document.getElementById('to-email').value;
            const toPhone = document.getElementById('to-phone').value;
            const toAddress = document.getElementById('to-address').value;
            const notes = document.getElementById('notes').value;
            const terms = document.getElementById('terms').value;
            const subtotal = document.getElementById('subtotal').textContent;
            const taxAmount = document.getElementById('tax-amount').textContent;
            const total = document.getElementById('total').textContent;
            const taxRate = document.getElementById('tax-rate').value;
            
            // Format dates
            const formattedInvoiceDate = formatDate(invoiceDate);
            const formattedDueDate = formatDate(dueDate);
            
            // Get items
            let itemsHtml = '';
            document.querySelectorAll('.invoice-item').forEach(item => {
                const description = item.querySelector('.item-description').value;
                const quantity = item.querySelector('.item-quantity').value;
                const price = parseFloat(item.querySelector('.item-price').value).toFixed(2);
                const amount = item.querySelector('.item-amount').value;
                
                itemsHtml += `
                    <tr class="border-b border-gray-200">
                        <td class="py-3 px-2">${description || 'No description'}</td>
                        <td class="py-3 px-2 text-right">${quantity}</td>
                        <td class="py-3 px-2 text-right">${currency} ${price}</td>
                        <td class="py-3 px-2 text-right">${currency} ${amount}</td>
                    </tr>
                `;
            });
            
            // Check if logo exists
            const logoPreview = document.getElementById('logo-preview');
            let logoHtml = '';
            if (!logoPreview.classList.contains('hidden')) {
                logoHtml = `<img src="${logoPreview.src}" alt="Company Logo" class="h-16">`;
            }
            
            // Generate preview HTML
            previewContainer.innerHTML = `
                <div class="max-w-4xl mx-auto">
                    <div class="flex justify-between items-start mb-8">
                        <div>
                            ${logoHtml}
                            <h1 class="text-3xl font-bold text-gray-800 mt-4">INVOICE</h1>
                        </div>
                        <div class="text-right">
                            <p class="text-lg font-semibold">${fromName}</p>
                            <p class="text-sm text-gray-600">${fromAddress.replace(/\n/g, '<br>')}</p>
                            <p class="text-sm text-gray-600 mt-2">${fromEmail}</p>
                            <p class="text-sm text-gray-600">${fromPhone}</p>
                        </div>
                    </div>
                    
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-8 mb-8">
                        <div>
                            <h2 class="text-sm font-semibold text-gray-500 uppercase mb-2">Bill To</h2>
                            <p class="font-medium">${toName}</p>
                            <p class="text-sm text-gray-600">${toAddress.replace(/\n/g, '<br>')}</p>
                            <p class="text-sm text-gray-600 mt-2">${toEmail}</p>
                            <p class="text-sm text-gray-600">${toPhone}</p>
                        </div>
                        <div>
                            <div class="grid grid-cols-2 gap-4">
                                <div>
                                    <h2 class="text-sm font-semibold text-gray-500 uppercase mb-2">Invoice #</h2>
                                    <p>${invoiceNumber}</p>
                                </div>
                                <div>
                                    <h2 class="text-sm font-semibold text-gray-500 uppercase mb-2">Date</h2>
                                    <p>${formattedInvoiceDate}</p>
                                </div>
                                <div>
                                    <h2 class="text-sm font-semibold text-gray-500 uppercase mb-2">Due Date</h2>
                                    <p>${formattedDueDate}</p>
                                </div>
                                <div>
                                    <h2 class="text-sm font-semibold text-gray-500 uppercase mb-2">Currency</h2>
                                    <p>${currency}</p>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="mb-8">
                        <table class="w-full">
                            <thead>
                                <tr class="border-b border-gray-200 text-left">
                                    <th class="py-2 px-2 font-semibold">Description</th>
                                    <th class="py-2 px-2 font-semibold text-right">Qty</th>
                                    <th class="py-2 px-2 font-semibold text-right">Price</th>
                                    <th class="py-2 px-2 font-semibold text-right">Amount</th>
                                </tr>
                            </thead>
                            <tbody>
                                ${itemsHtml}
                            </tbody>
                        </table>
                    </div>
                    
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
                        <div>
                            <h2 class="text-sm font-semibold text-gray-500 uppercase mb-2">Notes</h2>
                            <p class="text-gray-600">${notes}</p>
                            
                            <h2 class="text-sm font-semibold text-gray-500 uppercase mb-2 mt-4">Terms</h2>
                            <p class="text-gray-600">${terms}</p>
                        </div>
                        <div>
                            <div class="ml-auto max-w-xs">
                                <div class="flex justify-between py-2">
                                    <span class="text-gray-600">Subtotal:</span>
                                    <span class="font-medium">${currency} ${subtotal}</span>
                                </div>
                                <div class="flex justify-between py-2">
                                    <span class="text-gray-600">Tax (${taxRate}%):</span>
                                    <span class="font-medium">${currency} ${taxAmount}</span>
                                </div>
                                <div class="flex justify-between py-2 border-t border-gray-200 font-bold text-lg">
                                    <span>Total:</span>
                                    <span>${currency} ${total}</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            `;
        }
        
        // Format date
        function formatDate(dateString) {
            if (!dateString) return '';
            
            const options = { year: 'numeric', month: 'short', day: 'numeric' };
            const date = new Date(dateString);
            return date.toLocaleDateString('en-US', options);
        }
        
        // Generate PDF
        function generatePdf() {
            const { jsPDF } = window.jspdf;
            const doc = new jsPDF('p', 'pt', 'a4');
            const preview = document.getElementById('invoice-preview');
            
            html2canvas(preview, {
                scale: 2,
                logging: false,
                useCORS: true
            }).then(canvas => {
                const imgData = canvas.toDataURL('image/png');
                const imgWidth = doc.internal.pageSize.getWidth() - 40;
                const imgHeight = (canvas.height * imgWidth) / canvas.width;
                
                doc.addImage(imgData, 'PNG', 20, 20, imgWidth, imgHeight);
                
                // Get invoice number for filename
                const invoiceNumber = document.getElementById('invoice-number').value || 'invoice';
                doc.save(`${invoiceNumber}.pdf`);
            });
        }
        
        // Generate PNG
        function generatePng() {
            const preview = document.getElementById('invoice-preview');
            
            html2canvas(preview, {
                scale: 2,
                logging: false,
                useCORS: true
            }).then(canvas => {
                const link = document.createElement('a');
                link.download = `${document.getElementById('invoice-number').value || 'invoice'}.png`;
                link.href = canvas.toDataURL('image/png');
                link.click();
            });
        }
    </script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=THEMONEYHOLLIC/invoice-generator" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>