File size: 12,220 Bytes
19c7b35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f30e632
19c7b35
 
 
 
 
 
 
 
 
 
 
f30e632
19c7b35
 
f30e632
19c7b35
 
f30e632
19c7b35
 
f30e632
19c7b35
 
3c2f652
19c7b35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Initialize Lucide icons
lucide.createIcons();

// State management
let isSidebarCollapsed = false;
let isBillingExpanded = true;
let lineItemCount = 0;
let currentPatient = null;

// Mock patient data for search
const mockPatients = [
    { id: 'PT-1001', name: 'John Smith', phone: '+1 (555) 123-4567', image: 'https://static.photos/people/100x100/10' },
    { id: 'PT-1002', name: 'Emma Wilson', phone: '+1 (555) 234-5678', image: 'https://static.photos/people/100x100/20' },
    { id: 'PT-1003', name: 'Michael Brown', phone: '+1 (555) 345-6789', image: 'https://static.photos/people/100x100/30' },
    { id: 'PT-1004', name: 'Sarah Johnson', phone: '+1 (555) 456-7890', image: 'https://static.photos/people/100x100/40' },
    { id: 'PT-1005', name: 'David Lee', phone: '+1 (555) 567-8901', image: 'https://static.photos/people/100x100/50' }
];

// Initialize on load
document.addEventListener('DOMContentLoaded', () => {
    // Set default dates
    const today = new Date().toISOString().split('T')[0];
    document.getElementById('invoiceDate').value = today;
    
    const dueDate = new Date();
    dueDate.setDate(dueDate.getDate() + 30);
    document.getElementById('dueDate').value = dueDate.toISOString().split('T')[0];
    
    // Add initial line item
    addLineItem();
});

// Sidebar toggle for mobile
function toggleSidebar() {
    const sidebar = document.getElementById('sidebar');
    const overlay = document.getElementById('mobileOverlay');
    const isOpen = !sidebar.classList.contains('-translate-x-full');
    
    if (isOpen) {
        sidebar.classList.add('-translate-x-full');
        overlay.classList.add('hidden');
        document.body.style.overflow = '';
    } else {
        sidebar.classList.remove('-translate-x-full');
        overlay.classList.remove('hidden');
        document.body.style.overflow = 'hidden';
    }
}

// Sidebar toggle for desktop
function toggleSidebarDesktop() {
    const sidebar = document.getElementById('sidebar');
    const mainWrapper = document.getElementById('mainWrapper');
    const desktopToggle = document.getElementById('desktopToggle');
    
    isSidebarCollapsed = !isSidebarCollapsed;
    
    if (isSidebarCollapsed) {
        sidebar.classList.add('sidebar-collapsed');
        sidebar.style.width = '5rem';
        mainWrapper.style.marginLeft = '5rem';
        desktopToggle.innerHTML = '<i data-lucide="panel-right" class="w-5 h-5"></i>';
    } else {
        sidebar.classList.remove('sidebar-collapsed');
        sidebar.style.width = '16rem';
        mainWrapper.style.marginLeft = '16rem';
        desktopToggle.innerHTML = '<i data-lucide="panel-left" class="w-5 h-5"></i>';
    }
    
    lucide.createIcons();
    
    // Handle billing submenu visibility
    const submenu = document.getElementById('billingSubmenu');
    if (isSidebarCollapsed) {
        submenu.style.display = 'none';
    } else if (isBillingExpanded) {
        submenu.style.display = 'block';
    }
}

// Billing submenu toggle
function toggleBillingSubmenu() {
    if (isSidebarCollapsed) return;
    
    const submenu = document.getElementById('billingSubmenu');
    const chevron = document.getElementById('billingChevron');
    
    isBillingExpanded = !isBillingExpanded;
    
    if (isBillingExpanded) {
        submenu.style.display = 'block';
        chevron.style.transform = 'rotate(0deg)';
    } else {
        submenu.style.display = 'none';
        chevron.style.transform = 'rotate(-90deg)';
    }
}

// Patient type toggle
function setPatientType(type) {
    const btnExisting = document.getElementById('btnExisting');
    const btnNew = document.getElementById('btnNew');
    const existingForm = document.getElementById('existingPatientForm');
    const newForm = document.getElementById('newPatientForm');
    
    if (type === 'existing') {
        btnExisting.classList.add('bg-white', 'text-gray-900', 'shadow-sm');
        btnExisting.classList.remove('text-gray-600');
        btnNew.classList.remove('bg-white', 'text-gray-900', 'shadow-sm');
        btnNew.classList.add('text-gray-600');
        
        existingForm.classList.remove('hidden');
        newForm.classList.add('hidden');
    } else {
        btnNew.classList.add('bg-white', 'text-gray-900', 'shadow-sm');
        btnNew.classList.remove('text-gray-600');
        btnExisting.classList.remove('bg-white', 'text-gray-900', 'shadow-sm');
        btnExisting.classList.add('text-gray-600');
        
        newForm.classList.remove('hidden');
        existingForm.classList.add('hidden');
    }
}

// Patient search functionality
function searchPatients(query) {
    const resultsDiv = document.getElementById('searchResults');
    
    if (query.length < 2) {
        resultsDiv.classList.add('hidden');
        return;
    }
    
    const filtered = mockPatients.filter(p => 
        p.name.toLowerCase().includes(query.toLowerCase()) ||
        p.id.toLowerCase().includes(query.toLowerCase()) ||
        p.phone.includes(query)
    );
    
    if (filtered.length === 0) {
        resultsDiv.innerHTML = '<div class="p-3 text-sm text-gray-500">No patients found</div>';
    } else {
        resultsDiv.innerHTML = filtered.map(patient => `
            <div onclick="selectPatient('${patient.id}')" class="p-3 hover:bg-gray-50 cursor-pointer border-b border-gray-100 last:border-0 transition-colors">
                <div class="flex items-center gap-3">
                    <img src="${patient.image}" class="w-8 h-8 rounded-full object-cover">
                    <div>
                        <p class="font-medium text-gray-900 text-sm">${patient.name}</p>
                        <p class="text-xs text-gray-500">${patient.id}${patient.phone}</p>
                    </div>
                </div>
            </div>
        `).join('');
    }
    
    resultsDiv.classList.remove('hidden');
}

// Select patient from search
function selectPatient(patientId) {
    const patient = mockPatients.find(p => p.id === patientId);
    if (!patient) return;
    
    currentPatient = patient;
    
    document.getElementById('searchResults').classList.add('hidden');
    document.getElementById('patientSearch').value = '';
    
    // Show selected patient card
    document.getElementById('spImage').src = patient.image;
    document.getElementById('spName').textContent = patient.name;
    document.getElementById('spId').textContent = patient.id;
    document.getElementById('spPhone').textContent = patient.phone;
    document.getElementById('selectedPatientCard').classList.remove('hidden');
}

// Clear selected patient
function clearPatient() {
    currentPatient = null;
    document.getElementById('selectedPatientCard').classList.add('hidden');
}

// Add line item to invoice
function addLineItem() {
    const tbody = document.getElementById('lineItemsBody');
    const row = document.createElement('tr');
    row.className = 'line-item border-b border-gray-100 hover:bg-gray-50 transition-colors';
    row.id = `lineItem-${lineItemCount}`;
    
    row.innerHTML = `
        <td class="py-3 pl-2">
            <select class="w-full px-3 py-2 border border-gray-200 rounded-md text-sm focus-verify-teal outline-none bg-white" onchange="updateCalculation('${lineItemCount}')">
                <option value="">Select Service</option>
                <option value="consultation">Consultation</option>
                <option value="lab_test">Laboratory Test</option>
                <option value="xray">X-Ray</option>
                <option value="surgery">Surgery</option>
                <option value="medication">Medication</option>
                <option value="therapy">Physical Therapy</option>
                <option value="vaccination">Vaccination</option>
            </select>
        </td>
        <td class="py-3">
            <input type="text" class="w-full px-3 py-2 border border-gray-200 rounded-md text-sm focus-verify-teal outline-none" placeholder="Description">
        </td>
        <td class="py-3">
            <input type="number" id="qty-${lineItemCount}" value="1" min="1" class="w-full px-3 py-2 border border-gray-200 rounded-md text-sm focus-verify-teal outline-none text-center" onchange="updateCalculation('${lineItemCount}')">
        </td>
        <td class="py-3">
            <input type="number" id="rate-${lineItemCount}" value="0.00" min="0" step="0.01" class="w-full px-3 py-2 border border-gray-200 rounded-md text-sm focus-verify-teal outline-none" onchange="updateCalculation('${lineItemCount}')">
        </td>
        <td class="py-3">
            <input type="text" id="amount-${lineItemCount}" value="$0.00" readonly class="w-full px-3 py-2 bg-gray-50 border border-gray-200 rounded-md text-sm text-gray-700 font-medium cursor-not-allowed font-variant-numeric">
        </td>
        <td class="py-3 text-right">
            <button onclick="removeLineItem('${lineItemCount}')" class="text-gray-400 hover:text-red-500 transition-colors p-1 rounded hover:bg-red-50/50">
                <i data-lucide="trash-2" class="w-4 h-4"></i>
            </button>
        </td>
    `;
    
    tbody.appendChild(row);
    lineItemCount++;
    
    // Refresh icons
    lucide.createIcons();
}

// Remove line item
function removeLineItem(id) {
    const row = document.getElementById(`lineItem-${id}`);
    if (row) {
        row.style.opacity = '0';
        row.style.transform = 'translateX(-20px)';
        setTimeout(() => {
            row.remove();
            calculateTotals();
        }, 200);
    }
}

// Update line item calculation
function updateCalculation(id) {
    const qty = parseFloat(document.getElementById(`qty-${id}`).value) || 0;
    const rate = parseFloat(document.getElementById(`rate-${id}`).value) || 0;
    const amount = qty * rate;
    
    document.getElementById(`amount-${id}`).value = '$' + amount.toFixed(2);
    calculateTotals();
}

// Calculate invoice totals
function calculateTotals() {
    let subtotal = 0;
    
    // Sum all line items
    for (let i = 0; i < lineItemCount; i++) {
        const amountField = document.getElementById(`amount-${i}`);
        if (amountField && amountField.parentElement.parentElement.style.display !== 'none') {
            const value = parseFloat(amountField.value.replace('$', '')) || 0;
            subtotal += value;
        }
    }
    
    const tax = subtotal * 0.08; // 8% tax
    const total = subtotal + tax;
    
    // Update summary displays
    document.getElementById('summarySubtotal').textContent = '$' + subtotal.toFixed(2);
    document.getElementById('summaryTax').textContent = '$' + tax.toFixed(2);
    document.getElementById('summaryTotal').textContent = '$' + total.toFixed(2);
    document.getElementById('mobileTotal').textContent = '$' + total.toFixed(2);
}

// Save invoice
function saveInvoice() {
    showToast('Invoice saved successfully!');
    // In real app, would send to backend
}

// Print invoice
function printInvoice() {
    window.print();
}

// Send invoice
function sendInvoice() {
    showToast('Invoice sent to patient email!');
    // In real app, would trigger email/SMS
}

// Toast notification
function showToast(message) {
    const toast = document.getElementById('toast');
    const toastMessage = document.getElementById('toastMessage');
    
    toastMessage.textContent = message;
    toast.classList.remove('translate-y-20', 'opacity-0');
    
    setTimeout(() => {
        toast.classList.add('translate-y-20', 'opacity-0');
    }, 3000);
}

// Close search results when clicking outside
document.addEventListener('click', (e) => {
    const searchContainer = document.getElementById('patientSearch');
    const resultsDiv = document.getElementById('searchResults');
    
    if (!searchContainer.contains(e.target) && !resultsDiv.contains(e.target)) {
        resultsDiv.classList.add('hidden');
    }
});

// Handle window resize
window.addEventListener('resize', () => {
    if (window.innerWidth >= 1024) {
        document.getElementById('mobileOverlay').classList.add('hidden');
        document.body.style.overflow = '';
    } else {
        const sidebar = document.getElementById('sidebar');
        sidebar.classList.add('-translate-x-full');
    }
});