File size: 9,315 Bytes
b62701c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
document.addEventListener('DOMContentLoaded', function() {
    // Initialize current date and time
    const now = new Date();
    document.getElementById('transactionDate').valueAsDate = now;
    document.getElementById('transactionTime').value = now.toTimeString().substring(0, 5);
    
    // Update preview when inputs change
    const inputs = document.querySelectorAll('input, textarea, select');
    inputs.forEach(input => {
        input.addEventListener('input', updateReceiptPreview);
    });
    
    // Payment method change handler
    document.querySelectorAll('input[name="paymentMethod"]').forEach(radio => {
        radio.addEventListener('change', function() {
            updateReceiptPreview();
            if (this.value === 'Credit Card' || this.value === 'Debit Card') {
                document.getElementById('cardReferenceContainer').classList.remove('hidden');
            } else {
                document.getElementById('cardReferenceContainer').classList.add('hidden');
            }
        });
    });
    
    // Predefined services handlers
    document.getElementById('xfinityRefill').addEventListener('change', updateReceiptPreview);
    document.getElementById('boostBillPay').addEventListener('change', function() {
        document.getElementById('boostBillPayAmount').classList.toggle('hidden', !this.checked);
        updateReceiptPreview();
    });
    document.getElementById('boostBillPayAmount').addEventListener('input', updateReceiptPreview);
    
    // Add custom item button
    document.getElementById('addCustomItem').addEventListener('click', addCustomItem);
    
    // Print and PDF buttons
    document.getElementById('printReceipt').addEventListener('click', printReceipt);
    document.getElementById('downloadPdf').addEventListener('click', downloadPdf);
    
    // Logo upload handler
    document.getElementById('logoUpload').addEventListener('change', function(e) {
        if (e.target.files && e.target.files[0]) {
            const reader = new FileReader();
            reader.onload = function(event) {
                document.getElementById('logoPreview').src = event.target.result;
            };
            reader.readAsDataURL(e.target.files[0]);
        }
    });
    
    // Initial update
    updateReceiptPreview();
    
    function addCustomItem() {
        const container = document.getElementById('customItemsContainer');
        const itemId = Date.now();
        
        const itemDiv = document.createElement('div');
        itemDiv.className = 'item-row';
        itemDiv.innerHTML = `
            <input type="text" placeholder="Item name" class="item-name px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
            <input type="number" placeholder="Price" min="0" step="0.01" class="item-price px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
            <button class="remove-item bg-red-500 hover:bg-red-600 text-white px-2 py-1 rounded-md">
                <i data-feather="trash-2" class="w-4 h-4"></i>
            </button>
        `;
        
        container.appendChild(itemDiv);
        feather.replace();
        
        // Add event listeners to new inputs
        itemDiv.querySelector('.item-name').addEventListener('input', updateReceiptPreview);
        itemDiv.querySelector('.item-price').addEventListener('input', updateReceiptPreview);
        itemDiv.querySelector('.remove-item').addEventListener('click', function() {
            container.removeChild(itemDiv);
            updateReceiptPreview();
        });
    }
    
    function updateReceiptPreview() {
        // Update header info
        document.getElementById('addressPreview').innerHTML = document.getElementById('storeAddress').value.replace(/\n/g, '<br>');
        document.getElementById('repNamePreview').textContent = document.getElementById('repName').value;
        document.getElementById('transactionIdPreview').textContent = document.getElementById('transactionId').value;
        
        // Format date and time
        const date = document.getElementById('transactionDate').value;
        const time = document.getElementById('transactionTime').value;
        if (date && time) {
            const formattedDate = new Date(`${date}T${time}`).toLocaleString('en-US', {
                month: '2-digit',
                day: '2-digit',
                year: 'numeric',
                hour: '2-digit',
                minute: '2-digit',
                hour12: true
            });
            document.getElementById('dateTimePreview').textContent = `Date: ${formattedDate}`;
        }
        
        // Update payment method
        const paymentMethod = document.querySelector('input[name="paymentMethod"]:checked').value;
        document.getElementById('paymentMethodPreview').textContent = `Payment Method: ${paymentMethod}`;
        
        // Update thank you and policy
        document.getElementById('thankYouPreview').textContent = document.getElementById('thankYouMessage').value;
        document.getElementById('policyPreview').textContent = document.getElementById('storePolicy').value;
        
        // Calculate items and totals
        let subtotal = 0;
        const itemsContainer = document.getElementById('itemsPreview');
        itemsContainer.innerHTML = '';
        
        // Add predefined services
        if (document.getElementById('xfinityRefill').checked) {
            subtotal += 45;
            addItemToPreview('Xfinity Prepaid Refill', 45, false);
        }
        
        if (document.getElementById('boostBillPay').checked) {
            const amount = parseFloat(document.getElementById('boostBillPayAmount').value) || 0;
            if (amount >= 5 && amount <= 250) {
                subtotal += amount;
                addItemToPreview('Boost RTR Bill Pay', amount, false);
            }
        }
        
        // Add custom items
        document.querySelectorAll('.item-row').forEach(row => {
            const name = row.querySelector('.item-name').value;
            const price = parseFloat(row.querySelector('.item-price').value) || 0;
            
            if (name && price > 0) {
                subtotal += price;
                addItemToPreview(name, price, true);
            }
        });
        
        // Calculate tax (8.25% on taxable items)
        const tax = subtotal * 0.0825;
        const total = subtotal + tax;
        
        // Update totals
        document.getElementById('subtotalPreview').textContent = `$${subtotal.toFixed(2)}`;
        document.getElementById('taxPreview').textContent = `$${tax.toFixed(2)}`;
        document.getElementById('totalPreview').textContent = `$${total.toFixed(2)}`;
        
        // Add animation
        itemsContainer.classList.add('receipt-update');
        setTimeout(() => {
            itemsContainer.classList.remove('receipt-update');
        }, 300);
        
        function addItemToPreview(name, price, taxable) {
            const itemDiv = document.createElement('div');
            itemDiv.className = 'flex justify-between py-1';
            
            const nameSpan = document.createElement('span');
            nameSpan.textContent = name;
            
            const priceSpan = document.createElement('span');
            priceSpan.textContent = `$${price.toFixed(2)}`;
            if (taxable) {
                priceSpan.innerHTML += ' <span class="text-xs text-gray-500">*T</span>';
            }
            
            itemDiv.appendChild(nameSpan);
            itemDiv.appendChild(priceSpan);
            itemsContainer.appendChild(itemDiv);
        }
    }
    
    function printReceipt() {
        const receiptElement = document.getElementById('receiptPreview');
        const originalDisplay = receiptElement.style.display;
        
        // Show only the receipt for printing
        document.querySelectorAll('body > *:not(#receiptPreview)').forEach(el => {
            el.style.display = 'none';
        });
        receiptElement.style.display = 'block';
        receiptElement.style.margin = '0 auto';
        receiptElement.style.boxShadow = 'none';
        receiptElement.style.border = 'none';
        
        window.print();
        
        // Restore original display
        document.querySelectorAll('body > *').forEach(el => {
            el.style.display = '';
        });
        receiptElement.style.display = originalDisplay;
        receiptElement.style.boxShadow = '';
        receiptElement.style.border = '';
    }
    
    function downloadPdf() {
        const { jsPDF } = window.jspdf;
        const receiptElement = document.getElementById('receiptPreview');
        
        html2canvas(receiptElement).then(canvas => {
            const imgData = canvas.toDataURL('image/png');
            const pdf = new jsPDF({
                orientation: 'portrait',
                unit: 'mm'
            });
            
            // Calculate PDF dimensions to fit content
            const imgWidth = 80; // mm
            const imgHeight = (canvas.height * imgWidth) / canvas.width;
            
            pdf.addImage(imgData, 'PNG', 10, 10, imgWidth, imgHeight);
            pdf.save('boost_receipt.pdf');
        });
    }
});