-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
634 lines (557 loc) · 31.5 KB
/
index.html
File metadata and controls
634 lines (557 loc) · 31.5 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Global Payments - Developer Example</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,100..1000;1,9..40,100..1000&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://globalpayments-samples.github.io/css/styles.css">
</head>
<body>
<!-- Header -->
<header class="gp-header">
<div class="gp-container">
<div class="gp-header-content">
<div class="gp-logo">
<picture>
<source media="(min-width: 768px)" srcset="https://globalpayments-samples.github.io/assets/img/GP_logo.svg">
<img src="https://globalpayments-samples.github.io/assets/img/GP_favicon.svg" alt="Global Payments" style="height: 32px; width: auto;">
</picture>
</div>
<nav class="gp-header-nav">
<a href="https://developer.globalpay.com/">Documentation</a>
<a href="https://developer.globalpay.com/api/references-overview">API Reference</a>
<a href="https://developer.globalpay.com/docs/integration-options/sdk/overview">SDKs</a>
</nav>
</div>
</div>
</header>
<div class="gp-container">
<!-- Page Title -->
<h1 class="gp-page-title">SDK Reporting Service</h1>
<p class="gp-page-subtitle">Comprehensive reporting service for Global Payments transactions, providing search, filtering, and export capabilities.</p>
<!-- Tabs -->
<div class="gp-tabs">
<div class="gp-tab-list">
<button class="gp-tab-button active" data-tab="payment-form">Payment Form</button>
<button class="gp-tab-button" data-tab="reporting">Reporting Documentation</button>
<button class="gp-tab-button" data-tab="reporting-table">Transaction Report</button>
</div>
<!-- Payment Form Tab -->
<div class="gp-tab-content active" id="payment-form">
<div class="gp-card">
<div class="gp-card-header">
<h2 class="gp-card-title">Credit Card Payment Form</h2>
<p class="gp-card-description">
Secure payment processing using Global Payments tokenization with client-side form validation.
</p>
</div>
<form id="payment-form" class="gp-form" action="/process-payment" method="POST">
<div class="gp-form-group">
<label for="amount" class="gp-label">Amount ($):</label>
<input type="number" id="amount" name="amount" class="gp-input" min="0.01" step="0.01" value="10.00" required>
</div>
<div class="gp-form-group">
<label for="billing_zip" class="gp-label">Billing Zip Code:</label>
<input type="text" id="billing_zip" name="billing_zip" class="gp-input" placeholder="12345" required>
</div>
<div class="gp-form-group">
<label class="gp-label">Credit Card Information:</label>
<div id="credit-card"></div>
</div>
<button type="submit" class="gp-button gp-button-primary gp-button-full">
Process Payment
</button>
</form>
</div>
</div>
<!-- Reporting Documentation Tab -->
<div class="gp-tab-content" id="reporting">
<div class="gp-card">
<div class="gp-card-header">
<h2 class="gp-card-title">Reporting Operations</h2>
<p class="gp-card-description">
Access comprehensive transaction reporting, search, and export functionality.
</p>
</div>
<div class="gp-alert gp-alert-info">
<strong>Documentation:</strong> For detailed information on performing reporting operations, please refer to the REPORTING_README.md document.
</div>
<div style="margin-top: 1.5rem;">
<h3 class="gp-card-title">Available Reporting Features:</h3>
<ul style="margin-top: 1rem; margin-left: 1.5rem; line-height: 1.8;">
<li><strong>Transaction Search:</strong> Search and filter transactions by various criteria</li>
<li><strong>Transaction Details:</strong> View detailed information for specific transactions</li>
<li><strong>Summary Reports:</strong> Generate summaries with totals and statistics</li>
<li><strong>Declined Transactions:</strong> Filter and analyze declined payments</li>
<li><strong>Export Functionality:</strong> Export reports in CSV, JSON, and XML formats</li>
</ul>
</div>
<div style="margin-top: 1.5rem;">
<a href="documentation.php" class="gp-button gp-button-primary">
View Full Documentation
</a>
</div>
</div>
</div>
<!-- Transaction Report Tab -->
<div class="gp-tab-content" id="reporting-table">
<div class="gp-card">
<div class="gp-card-header">
<h2 class="gp-card-title">Transaction Report</h2>
<p class="gp-card-description">
View recent transaction details in a simple table format.
</p>
</div>
<!-- Filters -->
<div id="filters-container" style="margin-bottom: 1.5rem; padding: 1.5rem; background: #f8f9fa; border-radius: 8px; display: none;">
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; margin-bottom: 1rem;">
<div>
<label style="display: block; margin-bottom: 0.5rem; font-weight: 600; font-size: 0.875rem;">Start Date</label>
<input type="date" id="filter-start-date" style="width: 100%; padding: 0.5rem; border: 1px solid #ddd; border-radius: 4px;">
</div>
<div>
<label style="display: block; margin-bottom: 0.5rem; font-weight: 600; font-size: 0.875rem;">End Date</label>
<input type="date" id="filter-end-date" style="width: 100%; padding: 0.5rem; border: 1px solid #ddd; border-radius: 4px;">
</div>
<div>
<label style="display: block; margin-bottom: 0.5rem; font-weight: 600; font-size: 0.875rem;">Status</label>
<select id="filter-status" style="width: 100%; padding: 0.5rem; border: 1px solid #ddd; border-radius: 4px;">
<option value="">All</option>
<option value="CAPTURED">Captured</option>
<option value="DECLINED">Declined</option>
<option value="PENDING">Pending</option>
<option value="REVERSED">Reversed</option>
</select>
</div>
<div>
<label style="display: block; margin-bottom: 0.5rem; font-weight: 600; font-size: 0.875rem;">Page Size</label>
<select id="filter-page-size" style="width: 100%; padding: 0.5rem; border: 1px solid #ddd; border-radius: 4px;">
<option value="5">5</option>
<option value="10" selected>10</option>
<option value="25">25</option>
<option value="50">50</option>
</select>
</div>
</div>
<div style="display: flex; gap: 0.5rem;">
<button id="apply-filters-btn" class="gp-button gp-button-primary" style="padding: 0.5rem 1rem;">Apply Filters</button>
<button id="clear-filters-btn" class="gp-button" style="padding: 0.5rem 1rem; background: #6c757d; color: white; border: none;">Clear</button>
</div>
</div>
<!-- Action Buttons -->
<div style="display: flex; gap: 0.5rem; margin-bottom: 1rem; flex-wrap: wrap;">
<button id="refresh-btn" class="gp-button" style="padding: 0.5rem 1rem; background: #007bff; color: white; border: none; border-radius: 4px;">↻ Refresh</button>
<button id="toggle-filters-btn" class="gp-button" style="padding: 0.5rem 1rem; background: #6c757d; color: white; border: none; border-radius: 4px;">⚙ Filters</button>
<button id="export-json-btn" class="gp-button" style="padding: 0.5rem 1rem; background: #28a745; color: white; border: none; border-radius: 4px;">↓ JSON</button>
<button id="export-csv-btn" class="gp-button" style="padding: 0.5rem 1rem; background: #28a745; color: white; border: none; border-radius: 4px;">↓ CSV</button>
</div>
<!-- Report Container -->
<div id="report-container">
<div style="text-align: center; padding: 3rem;">
<div class="spinner" style="margin: 0 auto 1rem;"></div>
<div style="color: #666;">Loading transaction data...</div>
</div>
</div>
<!-- Pagination -->
<div id="pagination-container" style="display: none; margin-top: 1.5rem; text-align: center;">
<div style="display: inline-flex; gap: 0.5rem; align-items: center;">
<button id="prev-page-btn" class="gp-button" style="padding: 0.5rem 1rem; background: #007bff; color: white; border: none; border-radius: 4px;" disabled>← Previous</button>
<span id="page-info" style="padding: 0 1rem; font-weight: 600;"></span>
<button id="next-page-btn" class="gp-button" style="padding: 0.5rem 1rem; background: #007bff; color: white; border: none; border-radius: 4px;" disabled>Next →</button>
</div>
</div>
<!-- Transaction Detail Modal -->
<div id="transaction-modal" style="display: none; position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.5); z-index: 1000; padding: 2rem; overflow-y: auto;">
<div style="max-width: 800px; margin: 0 auto; background: white; border-radius: 8px; padding: 2rem; position: relative;">
<button id="close-modal-btn" style="position: absolute; top: 1rem; right: 1rem; background: none; border: none; font-size: 1.5rem; cursor: pointer; color: #666;">×</button>
<h3 style="margin-bottom: 1.5rem; padding-bottom: 0.5rem; border-bottom: 2px solid #007bff; color: #007bff;">Transaction Details</h3>
<div id="modal-content"></div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Footer -->
<!--
DEVELOPER NOTE: Update the following footer information to match your business:
- Company name and copyright year
- Update all links to point to your actual Terms of Service, Privacy Policy, and Refund Policy pages
- Refund Policy link is typically required by payment processor underwriting in addition to Terms and Privacy
- Ensure all linked policies are accessible and up-to-date before going live
-->
<footer class="gp-footer">
<div class="gp-container">
<div class="gp-footer-content">
<div class="gp-footer-section">
<p class="gp-footer-copyright">
© 2024 Your Company Name. All rights reserved.
</p>
</div>
<div class="gp-footer-section">
<nav class="gp-footer-nav">
<a href="/terms-of-service" class="gp-footer-link">Terms of Service</a>
<a href="/privacy-policy" class="gp-footer-link">Privacy Policy</a>
<a href="/refund-policy" class="gp-footer-link">Refund Policy</a>
</nav>
</div>
</div>
</div>
</footer>
<script src="https://globalpayments-samples.github.io/js/script.js"></script>
<style>
.spinner {
width: 40px;
height: 40px;
border: 4px solid #f3f3f3;
border-top: 4px solid #007bff;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.clickable-id {
color: #007bff;
cursor: pointer;
text-decoration: underline;
}
.clickable-id:hover {
color: #0056b3;
}
@media (max-width: 768px) {
#filters-container > div:first-child {
grid-template-columns: 1fr !important;
}
}
</style>
<script>
// Global state
let currentPage = 1;
let pageSize = 10;
let totalPages = 1;
let currentFilters = {};
let sortBy = '';
let sortOrder = 'desc';
let reportLoaded = false;
// Load transaction report when tab is clicked
document.addEventListener('DOMContentLoaded', function() {
const reportTab = document.querySelector('[data-tab="reporting-table"]');
if (reportTab) {
reportTab.addEventListener('click', function() {
if (!reportLoaded) {
loadTransactionReport();
reportLoaded = true;
}
});
}
// Setup event listeners - check elements exist first
const refreshBtn = document.getElementById('refresh-btn');
const toggleFiltersBtn = document.getElementById('toggle-filters-btn');
const applyFiltersBtn = document.getElementById('apply-filters-btn');
const clearFiltersBtn = document.getElementById('clear-filters-btn');
const exportJsonBtn = document.getElementById('export-json-btn');
const exportCsvBtn = document.getElementById('export-csv-btn');
const prevPageBtn = document.getElementById('prev-page-btn');
const nextPageBtn = document.getElementById('next-page-btn');
const closeModalBtn = document.getElementById('close-modal-btn');
const transactionModal = document.getElementById('transaction-modal');
if (refreshBtn) refreshBtn.addEventListener('click', () => loadTransactionReport());
if (toggleFiltersBtn) toggleFiltersBtn.addEventListener('click', toggleFilters);
if (applyFiltersBtn) applyFiltersBtn.addEventListener('click', applyFilters);
if (clearFiltersBtn) clearFiltersBtn.addEventListener('click', clearFilters);
if (exportJsonBtn) exportJsonBtn.addEventListener('click', () => exportData('json'));
if (exportCsvBtn) exportCsvBtn.addEventListener('click', () => exportData('csv'));
if (prevPageBtn) prevPageBtn.addEventListener('click', () => changePage(currentPage - 1));
if (nextPageBtn) nextPageBtn.addEventListener('click', () => changePage(currentPage + 1));
if (closeModalBtn) closeModalBtn.addEventListener('click', closeModal);
if (transactionModal) {
transactionModal.addEventListener('click', (e) => {
if (e.target.id === 'transaction-modal') closeModal();
});
}
// Event delegation for clickable transaction IDs
const reportContainer = document.getElementById('report-container');
if (reportContainer) {
reportContainer.addEventListener('click', function(e) {
if (e.target.classList.contains('clickable-id')) {
const transactionId = e.target.getAttribute('data-transaction-id');
if (transactionId) {
viewTransactionDetail(transactionId);
}
}
});
}
});
function toggleFilters() {
const container = document.getElementById('filters-container');
if (container) {
container.style.display = container.style.display === 'none' ? 'block' : 'none';
}
}
function applyFilters() {
const startDateEl = document.getElementById('filter-start-date');
const endDateEl = document.getElementById('filter-end-date');
const statusEl = document.getElementById('filter-status');
const pageSizeEl = document.getElementById('filter-page-size');
if (startDateEl && endDateEl && statusEl && pageSizeEl) {
currentFilters = {
start_date: startDateEl.value,
end_date: endDateEl.value,
status: statusEl.value
};
pageSize = parseInt(pageSizeEl.value) || 10;
currentPage = 1;
loadTransactionReport();
}
}
function clearFilters() {
const startDateEl = document.getElementById('filter-start-date');
const endDateEl = document.getElementById('filter-end-date');
const statusEl = document.getElementById('filter-status');
const pageSizeEl = document.getElementById('filter-page-size');
if (startDateEl) startDateEl.value = '';
if (endDateEl) endDateEl.value = '';
if (statusEl) statusEl.value = '';
if (pageSizeEl) pageSizeEl.value = '10';
currentFilters = {};
pageSize = 10;
currentPage = 1;
loadTransactionReport();
}
function changePage(page) {
if (page < 1 || page > totalPages) return;
currentPage = page;
loadTransactionReport();
}
function buildQueryString() {
const params = {
page: currentPage,
page_size: pageSize,
...currentFilters
};
return Object.keys(params)
.filter(key => params[key] !== '' && params[key] !== null && params[key] !== undefined)
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
.join('&');
}
function loadTransactionReport() {
const container = document.getElementById('report-container');
showLoading(container);
const queryString = buildQueryString();
fetch(`reports.php?action=search&${queryString}`)
.then(response => response.json())
.then(data => {
if (data.success && data.data.transactions && data.data.transactions.length > 0) {
// Calculate total pages from pagination data
const pagination = data.data.pagination || {};
// Use original_total_count which is the total across all pages
const totalCount = pagination.original_total_count || pagination.total_count || 0;
const currentPageSize = pagination.page_size || pageSize;
totalPages = Math.ceil(totalCount / currentPageSize) || 1;
displayTransactionReport(data.data.transactions, container);
updatePagination(data.data);
} else {
container.innerHTML = '<div style="text-align: center; padding: 2rem; color: #666;">No transactions found.</div>';
const paginationContainer = document.getElementById('pagination-container');
if (paginationContainer) paginationContainer.style.display = 'none';
}
})
.catch(error => {
console.error('Error loading report:', error);
container.innerHTML = '<div style="text-align: center; padding: 2rem; color: #dc3545;">Error loading transaction data. Check console for details.</div>';
const paginationContainer = document.getElementById('pagination-container');
if (paginationContainer) paginationContainer.style.display = 'none';
});
}
function showLoading(container) {
container.innerHTML = `
<div style="text-align: center; padding: 3rem;">
<div class="spinner" style="margin: 0 auto 1rem;"></div>
<div style="color: #666;">Loading transaction data...</div>
</div>
`;
}
function updatePagination(data) {
const paginationContainer = document.getElementById('pagination-container');
const pageInfo = document.getElementById('page-info');
const prevBtn = document.getElementById('prev-page-btn');
const nextBtn = document.getElementById('next-page-btn');
if (!paginationContainer || !pageInfo || !prevBtn || !nextBtn) return;
// Use pagination data from API response
const pagination = data.pagination || {};
// Use original_total_count for display (total across all pages)
const totalCount = pagination.original_total_count || pagination.total_count || 0;
pageInfo.textContent = `Page ${currentPage} of ${totalPages} (${totalCount} total)`;
prevBtn.disabled = currentPage <= 1;
nextBtn.disabled = currentPage >= totalPages;
paginationContainer.style.display = totalPages > 1 ? 'block' : 'none';
}
function displayTransactionReport(transactions, container) {
const ignoredProperties = ['rawResponse', 'balanceAmount', 'token'];
let html = '';
transactions.forEach((transaction, index) => {
html += '<div style="margin-bottom: 2rem; background: #f8f9fa; padding: 1.5rem; border-radius: 8px;">';
html += `<h3 style="margin-bottom: 1rem; padding-bottom: 0.5rem; border-bottom: 2px solid #007bff; color: #007bff;">Transaction ${(currentPage - 1) * pageSize + index + 1}</h3>`;
html += '<table style="width: 100%; border-collapse: collapse; background: white; border-radius: 4px; overflow: hidden;">';
html += '<tbody>';
// Sort properties alphabetically
const sortedKeys = Object.keys(transaction).sort();
sortedKeys.forEach(property => {
if (ignoredProperties.includes(property)) {
return;
}
let value = transaction[property];
// Handle timestamp objects
if (property === 'timestamp' && typeof value === 'object' && value.date) {
value = value.date;
}
// Skip complex objects, null, or undefined values
if (value === null || value === undefined || typeof value === 'object') {
return;
}
// Format property name
const formattedProperty = property
.replace(/_/g, ' ')
.replace(/([A-Z])/g, ' $1')
.trim()
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
// Format value - make transaction ID clickable
let formattedValue;
if (property === 'transaction_id' && value) {
// Use data attribute instead of inline onclick to prevent XSS
formattedValue = `<span class="clickable-id" data-transaction-id="${escapeHtml(String(value))}" style="cursor: pointer;">${escapeHtml(String(value))}</span>`;
} else {
formattedValue = value === '' ? '-' : escapeHtml(String(value));
}
html += '<tr style="border-bottom: 1px solid #e9ecef;">';
html += `<td style="padding: 0.75rem; font-weight: 600; width: 40%;"><strong>${escapeHtml(formattedProperty)}</strong></td>`;
html += `<td style="padding: 0.75rem;">${formattedValue}</td>`;
html += '</tr>';
});
html += '</tbody>';
html += '</table>';
html += '</div>';
});
container.innerHTML = html;
}
function viewTransactionDetail(transactionId) {
const modal = document.getElementById('transaction-modal');
const modalContent = document.getElementById('modal-content');
modalContent.innerHTML = `
<div style="text-align: center; padding: 2rem;">
<div class="spinner" style="margin: 0 auto 1rem;"></div>
<div style="color: #666;">Loading transaction details...</div>
</div>
`;
modal.style.display = 'block';
fetch(`reports.php?action=detail&transaction_id=${encodeURIComponent(transactionId)}`)
.then(response => response.json())
.then(data => {
if (data.success && data.data) {
displayTransactionDetail(data.data, modalContent);
} else {
modalContent.innerHTML = '<div style="text-align: center; padding: 2rem; color: #dc3545;">Transaction details not found.</div>';
}
})
.catch(error => {
console.error('Error loading transaction details:', error);
modalContent.innerHTML = '<div style="text-align: center; padding: 2rem; color: #dc3545;">Error loading transaction details.</div>';
});
}
function displayTransactionDetail(transaction, container) {
const ignoredProperties = ['rawResponse', 'balanceAmount', 'token'];
let html = '<table style="width: 100%; border-collapse: collapse;">';
html += '<tbody>';
const sortedKeys = Object.keys(transaction).sort();
sortedKeys.forEach(property => {
if (ignoredProperties.includes(property)) {
return;
}
let value = transaction[property];
if (property === 'timestamp' && typeof value === 'object' && value.date) {
value = value.date;
}
if (value === null || value === undefined || typeof value === 'object') {
return;
}
const formattedProperty = property
.replace(/_/g, ' ')
.replace(/([A-Z])/g, ' $1')
.trim()
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
const formattedValue = value === '' ? '-' : escapeHtml(String(value));
html += '<tr style="border-bottom: 1px solid #e9ecef;">';
html += `<td style="padding: 0.75rem; font-weight: 600; width: 40%;"><strong>${escapeHtml(formattedProperty)}</strong></td>`;
html += `<td style="padding: 0.75rem;">${formattedValue}</td>`;
html += '</tr>';
});
html += '</tbody>';
html += '</table>';
container.innerHTML = html;
}
function closeModal() {
const modal = document.getElementById('transaction-modal');
if (modal) {
modal.style.display = 'none';
}
}
function exportData(format) {
const exportBtn = format === 'json' ? document.getElementById('export-json-btn') : document.getElementById('export-csv-btn');
if (!exportBtn) return;
const originalText = exportBtn.textContent;
exportBtn.textContent = format === 'json' ? '⏳ Exporting...' : '⏳ Exporting...';
exportBtn.disabled = true;
const queryString = Object.keys(currentFilters)
.filter(key => currentFilters[key] !== '' && currentFilters[key] !== null)
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(currentFilters[key])}`)
.join('&');
const url = `reports.php?action=export&format=${format}&page_size=1000&${queryString}`;
if (format === 'csv') {
// For CSV, trigger download
window.location.href = url;
setTimeout(() => {
exportBtn.textContent = originalText;
exportBtn.disabled = false;
}, 2000);
} else {
// For JSON, fetch and download
fetch(url)
.then(response => response.json())
.then(data => {
if (data.success) {
const blob = new Blob([JSON.stringify(data.data, null, 2)], { type: 'application/json' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = `transactions-${new Date().toISOString().split('T')[0]}.json`;
link.click();
} else {
alert('Export failed: ' + (data.error?.message || 'Unknown error'));
}
})
.catch(error => {
console.error('Export error:', error);
alert('Export failed. Check console for details.');
})
.finally(() => {
exportBtn.textContent = originalText;
exportBtn.disabled = false;
});
}
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
</script>
</body>
</html>