Spaces:
Running
Running
File size: 6,812 Bytes
7b78975 | 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 | document.addEventListener('DOMContentLoaded', function() {
// Initialize camera functionality
const scanBtn = document.getElementById('scanReceiptBtn');
const uploadBtn = document.getElementById('uploadReceiptBtn');
const retakeBtn = document.getElementById('retakeBtn');
const processBtn = document.getElementById('processBtn');
const receiptPreview = document.getElementById('receiptPreview');
const previewImage = document.getElementById('previewImage');
const receiptForm = document.getElementById('receiptForm');
if (scanBtn) {
scanBtn.addEventListener('click', async () => {
try {
// Check for mobile device
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
const stream = await navigator.mediaDevices.getUserMedia({
video: { facingMode: 'environment' },
audio: false
});
// Create camera preview
const video = document.createElement('video');
video.srcObject = stream;
video.setAttribute('playsinline', '');
video.style.width = '100%';
video.style.height = 'auto';
// Replace button with camera preview
scanBtn.replaceWith(video);
video.play();
// Add capture button
const captureBtn = document.createElement('button');
captureBtn.className = 'w-full bg-green-600 text-white py-3 px-4 rounded-lg hover:bg-green-700 transition duration-200 mt-2 flex items-center justify-center';
captureBtn.innerHTML = '<i data-feather="aperture" class="mr-2"></i> Capture';
video.after(captureBtn);
feather.replace();
captureBtn.addEventListener('click', () => {
// Create canvas to capture image
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0);
// Stop camera stream
stream.getTracks().forEach(track => track.stop());
// Show preview
previewImage.src = canvas.toDataURL('image/jpeg');
receiptPreview.classList.remove('hidden');
video.remove();
captureBtn.remove();
});
} else {
alert('Camera access is only available on mobile devices. Please use the upload option.');
}
} catch (err) {
console.error('Error accessing camera:', err);
alert('Could not access camera. Please check permissions.');
}
});
}
if (uploadBtn) {
uploadBtn.addEventListener('click', () => {
const input = document.createElement('input');
input.type = 'file';
input.accept = 'image/*';
input.onchange = (e) => {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (event) => {
previewImage.src = event.target.result;
receiptPreview.classList.remove('hidden');
};
reader.readAsDataURL(file);
}
};
input.click();
});
}
if (retakeBtn) {
retakeBtn.addEventListener('click', () => {
receiptPreview.classList.add('hidden');
previewImage.src = '';
});
}
if (processBtn) {
processBtn.addEventListener('click', async () => {
// Show loading state
processBtn.innerHTML = '<i data-feather="loader" class="animate-spin mr-1"></i> Processing...';
feather.replace();
try {
// In a real app, you would send the image to an OCR API here
// For demo purposes, we'll simulate processing with a timeout
await new Promise(resolve => setTimeout(resolve, 1500));
// Simulate extracted data
document.querySelector('input[name="amount"]').value = '45.67';
document.querySelector('select[name="category"]').value = 'Food';
document.querySelector('input[name="date"]').value = new Date().toISOString().split('T')[0];
document.querySelector('input[name="merchant"]').value = 'Burger Palace';
receiptPreview.classList.add('hidden');
receiptForm.classList.remove('hidden');
} catch (err) {
console.error('Error processing receipt:', err);
alert('Failed to process receipt. Please try again.');
} finally {
processBtn.innerHTML = '<i data-feather="zap" class="mr-1"></i> Process';
feather.replace();
}
});
}
// Form submission
if (receiptForm) {
receiptForm.addEventListener('submit', function(e) {
e.preventDefault();
alert('Receipt saved successfully!');
receiptForm.reset();
receiptForm.classList.add('hidden');
});
}
// Initialize tooltips
const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl);
});
});
// Chart data for the spending chart component
const chartData = {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
datasets: [
{
label: 'Food',
data: [120, 190, 150, 200, 180, 220],
backgroundColor: '#6366F1',
},
{
label: 'Transport',
data: [80, 90, 70, 85, 75, 95],
backgroundColor: '#10B981',
},
{
label: 'Shopping',
data: [200, 150, 180, 220, 250, 200],
backgroundColor: '#8B5CF6',
},
{
label: 'Entertainment',
data: [50, 60, 70, 65, 80, 75],
backgroundColor: '#F59E0B',
}
]
}; |