/** * Benchmark Data Loader * Handles AJAX fetch and display of benchmark data */ (function() { 'use strict'; // Wait for DOM ready if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initBenchmark); } else { initBenchmark(); } function initBenchmark() { const section = document.querySelector('.pd-benchmark-section'); if (!section) return; const button = document.getElementById('btn-load-benchmark'); const content = document.getElementById('benchmark-content'); const productId = section.dataset.productId; if (!button || !content || !productId) return; let isLoading = false; let dataLoaded = false; button.addEventListener('click', function() { if (isLoading || dataLoaded) return; loadBenchmarkData(productId, button, content); }); } function loadBenchmarkData(productId, button, container) { // Update button state button.disabled = true; button.classList.add('loading'); const originalText = button.querySelector('.btn-text').textContent; button.querySelector('.btn-text').textContent = 'Memuat data...'; // Show loading state container.style.display = 'block'; container.innerHTML = `

Mengambil data benchmark...

`; // Fetch data fetch('/api/benchmark/fetch.php?product_id=' + productId) .then(function(response) { if (!response.ok) { throw new Error('HTTP error ' + response.status); } return response.json(); }) .then(function(result) { if (result.success && result.data) { renderBenchmarkData(result.data, container); button.style.display = 'none'; // Hide button after successful load } else { throw new Error(result.error || 'Data tidak tersedia'); } }) .catch(function(error) { console.error('Benchmark fetch error:', error); container.innerHTML = `

⚠️ Data Benchmark Tidak Tersedia

${error.message || 'Terjadi kesalahan saat memuat data'}

`; // Reset button button.disabled = false; button.classList.remove('loading'); button.querySelector('.btn-text').textContent = originalText; }); } function renderBenchmarkData(data, container) { let html = '
'; // Chipset info if available if (data.chipset) { html += `
Chipset
${escapeHtml(data.chipset)}
`; } // Main benchmark scores if (data.antutu || data.geekbench_multi || data.overall_score) { html += '
'; if (data.antutu) { html += `
ANTUTU
${formatNumber(data.antutu)}
${getScoreBadge(data.antutu, 'antutu')}
`; } if (data.geekbench_multi) { html += `
GEEKBENCH MULTI
${formatNumber(data.geekbench_multi)}
${data.geekbench_single ? '
Single: ' + formatNumber(data.geekbench_single) + '
' : ''}
`; } if (data.gaming_score) { html += `
GAMING
${data.gaming_score}/100
`; } if (data.battery_score) { html += `
BATTERY
${data.battery_score}/100
`; } if (data.overall_score) { html += `
OVERALL
${data.overall_score}/100
`; } html += '
'; } // AI Performance Description if (data.notes) { html += `
💡 Analisis Performa
${escapeHtml(data.notes)}
`; } // Attribution html += `

Data benchmark dianalisis menggunakan AI dari spesifikasi produk

${data.fetched_at ? '

Terakhir diperbarui: ' + formatDate(data.fetched_at) + '

' : ''}
`; html += '
'; container.innerHTML = html; } function formatNumber(num) { if (!num) return '-'; return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.'); } function escapeHtml(text) { if (!text) return ''; const div = document.createElement('div'); div.textContent = text; return div.innerHTML; } function formatDate(dateStr) { try { const date = new Date(dateStr); const options = { year: 'numeric', month: 'long', day: 'numeric' }; return date.toLocaleDateString('id-ID', options); } catch (e) { return dateStr; } } function getScoreBadge(score, type) { let category = ''; let label = ''; if (type === 'antutu') { if (score >= 500000) { category = 'excellent'; label = 'Flagship'; } else if (score >= 350000) { category = 'good'; label = 'High-End'; } else if (score >= 200000) { category = 'average'; label = 'Mid-Range'; } else { category = 'average'; label = 'Entry-Level'; } } if (label) { return '
' + label + '
'; } return ''; } })();