-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathindex.html
More file actions
279 lines (251 loc) · 9.7 KB
/
index.html
File metadata and controls
279 lines (251 loc) · 9.7 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Invoice Generator</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.4.1/milligram.min.css">
<style>
body {
padding: 20px;
}
.container {
max-width: 800px;
margin: 0 auto;
}
.dropzone {
border: 2px dashed #ccc;
border-radius: 4px;
padding: 20px;
text-align: center;
cursor: pointer;
}
.dropzone.dragover {
background-color: #f0f0f0;
}
#previewImage {
max-width: 100%;
max-height: 200px;
margin-top: 10px;
}
.invoice-items {
margin-bottom: 20px;
}
.invoice-item {
display: flex;
gap: 10px;
margin-bottom: 10px;
}
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0,0,0,0.4);
}
.modal-content {
background-color: #fefefe;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
cursor: pointer;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<h1>Invoice Generator</h1>
<h2>Upload Image</h2>
<div id="dropzone" class="dropzone">
<p>Drag and drop an image here, or click to select a file</p>
<input type="file" id="fileInput" accept="image/*" style="display: none;">
</div>
<img id="previewImage" src="" alt="Preview" style="display: none;">
<h2>Invoice Details</h2>
<form id="invoiceForm">
<label for="companyInfo">Company Info:</label>
<textarea id="companyInfo" name="companyInfo" required></textarea>
<h3>Invoice Items</h3>
<div id="invoiceItems" class="invoice-items">
<div class="invoice-item">
<input type="text" name="itemName[]" placeholder="Item name" required>
<input type="number" name="itemQuantity[]" placeholder="Quantity" required min="1">
<input type="number" name="itemPrice[]" placeholder="Price" required min="0" step="0.01">
<button type="button" class="button-outline" onclick="removeItem(this)">Remove</button>
</div>
</div>
<button type="button" onclick="addItem()">Add Item</button>
<button type="submit">Generate Invoice</button>
</form>
<h2>Generated Invoices</h2>
<table id="invoiceList">
<thead>
<tr>
<th>ID</th>
<th>Company Info</th>
<th>Total</th>
<th>Actions</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<div id="previewModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<h2>Invoice Preview</h2>
<img id="previewInvoice" src="" alt="Invoice Preview" style="max-width: 100%;">
</div>
</div>
<script>
const API_URL = 'http://localhost:7527';
let uploadedImagePath = '';
// Drag and drop functionality
const dropzone = document.getElementById('dropzone');
const fileInput = document.getElementById('fileInput');
const previewImage = document.getElementById('previewImage');
dropzone.addEventListener('click', () => fileInput.click());
dropzone.addEventListener('dragover', (e) => {
e.preventDefault();
dropzone.classList.add('dragover');
});
dropzone.addEventListener('dragleave', () => dropzone.classList.remove('dragover'));
dropzone.addEventListener('drop', handleDrop);
fileInput.addEventListener('change', handleFileSelect);
function handleDrop(e) {
e.preventDefault();
dropzone.classList.remove('dragover');
const file = e.dataTransfer.files[0];
handleImageUpload(file);
}
function handleFileSelect(e) {
const file = e.target.files[0];
handleImageUpload(file);
}
async function handleImageUpload(file) {
const formData = new FormData();
formData.append('image', file);
try {
const response = await fetch(`${API_URL}/upload_image`, {
method: 'POST',
body: formData
});
const data = await response.json();
uploadedImagePath = data.image_path;
previewImage.src = URL.createObjectURL(file);
previewImage.style.display = 'block';
} catch (error) {
console.error('Error uploading image:', error);
}
}
// Invoice form handling
const invoiceForm = document.getElementById('invoiceForm');
invoiceForm.addEventListener('submit', generateInvoice);
async function generateInvoice(e) {
e.preventDefault();
const formData = new FormData(invoiceForm);
const invoiceData = {
company_info: formData.get('companyInfo'),
items: [],
image_path: uploadedImagePath
};
const itemNames = formData.getAll('itemName[]');
const itemQuantities = formData.getAll('itemQuantity[]');
const itemPrices = formData.getAll('itemPrice[]');
for (let i = 0; i < itemNames.length; i++) {
invoiceData.items.push({
name: itemNames[i],
quantity: parseInt(itemQuantities[i]),
price: parseFloat(itemPrices[i])
});
}
try {
const response = await fetch(`${API_URL}/generate_invoice`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(invoiceData)
});
const data = await response.json();
displayInvoicePreview(data.id);
loadInvoiceList();
} catch (error) {
console.error('Error generating invoice:', error);
}
}
async function displayInvoicePreview(invoiceId) {
const previewModal = document.getElementById('previewModal');
const previewInvoice = document.getElementById('previewInvoice');
const closeBtn = document.getElementsByClassName('close')[0];
previewInvoice.src = `${API_URL}/download/${invoiceId}?format=png`;
previewModal.style.display = 'block';
closeBtn.onclick = () => previewModal.style.display = 'none';
window.onclick = (event) => {
if (event.target == previewModal) {
previewModal.style.display = 'none';
}
};
}
async function loadInvoiceList() {
try {
const response = await fetch(`${API_URL}/invoices`);
const invoices = await response.json();
const tbody = document.querySelector('#invoiceList tbody');
tbody.innerHTML = '';
invoices.forEach(invoice => {
const row = tbody.insertRow();
row.innerHTML = `
<td>${invoice.id}</td>
<td>${invoice.company_info}</td>
<td>$${invoice.total.toFixed(2)}</td>
<td>
<button onclick="downloadInvoice('${invoice.id}', 'pdf')">Download PDF</button>
<button onclick="downloadInvoice('${invoice.id}', 'png')">Download PNG</button>
</td>
`;
});
} catch (error) {
console.error('Error loading invoice list:', error);
}
}
function downloadInvoice(invoiceId, format) {
window.open(`${API_URL}/download/${invoiceId}?format=${format}`, '_blank');
}
function addItem() {
const invoiceItems = document.getElementById('invoiceItems');
const newItem = document.createElement('div');
newItem.className = 'invoice-item';
newItem.innerHTML = `
<input type="text" name="itemName[]" placeholder="Item name" required>
<input type="number" name="itemQuantity[]" placeholder="Quantity" required min="1">
<input type="number" name="itemPrice[]" placeholder="Price" required min="0" step="0.01">
<button type="button" class="button-outline" onclick="removeItem(this)">Remove</button>
`;
invoiceItems.appendChild(newItem);
}
function removeItem(button) {
button.parentElement.remove();
}
// Load invoice list on page load
loadInvoiceList();
</script>
</body>
</html>