/** * captura.js — Data entry logic for Nexus Autoparts * 3 sections: OEM Parts, Aftermarket/Interchange, Images */ (function () { 'use strict'; var API = ''; var currentMye = null; // selected vehicle MYE id var currentVehicle = null; // vehicle info object var vehicleParts = []; // existing parts for current vehicle var manufacturers = []; // cached manufacturer list var vehicleStatus = 'pending'; var vehiclePage = 1; // ================================================================ // Utility // ================================================================ function toast(msg, type) { var el = document.createElement('div'); el.className = 'toast ' + (type || 'success'); el.textContent = msg; document.body.appendChild(el); setTimeout(function () { el.remove(); }, 3000); } function api(path, opts) { opts = opts || {}; return fetch(API + path, opts).then(function (r) { if (!r.ok) return r.json().then(function (d) { throw new Error(d.error || 'Error'); }); return r.json(); }); } function esc(s) { if (!s) return ''; var d = document.createElement('div'); d.textContent = s; return d.innerHTML; } // ================================================================ // Tab Switching // ================================================================ document.querySelectorAll('.captura-tab').forEach(function (tab) { tab.addEventListener('click', function () { document.querySelectorAll('.captura-tab').forEach(function (t) { t.classList.remove('active'); }); document.querySelectorAll('.captura-section').forEach(function (s) { s.classList.remove('active'); }); tab.classList.add('active'); var target = tab.getAttribute('data-tab'); document.getElementById('section-' + target).classList.add('active'); if (target === 'aftermarket') loadPartsWithoutAftermarket(); if (target === 'images') loadPartsWithoutImage(); }); }); // ================================================================ // SECTION 1: OEM Parts // ================================================================ // --- Status tabs --- document.querySelectorAll('.status-tab').forEach(function (tab) { tab.addEventListener('click', function () { document.querySelectorAll('.status-tab').forEach(function (t) { t.classList.remove('active'); }); tab.classList.add('active'); vehicleStatus = tab.getAttribute('data-status'); vehiclePage = 1; loadVehicles(); }); }); // --- Brand filter --- function loadBrands() { api('/api/brands').then(function (brands) { var sel = document.getElementById('oem-brand-filter'); brands.forEach(function (b) { var opt = document.createElement('option'); opt.value = b; opt.textContent = b; sel.appendChild(opt); }); }); } document.getElementById('oem-brand-filter').addEventListener('change', function () { vehiclePage = 1; loadVehicles(); }); var modelTimer = null; document.getElementById('oem-model-filter').addEventListener('input', function () { clearTimeout(modelTimer); modelTimer = setTimeout(function () { vehiclePage = 1; loadVehicles(); }, 400); }); // --- Load vehicles --- function loadVehicles() { var brand = document.getElementById('oem-brand-filter').value; var model = document.getElementById('oem-model-filter').value; var list = document.getElementById('oem-vehicle-list'); list.innerHTML = '
Sin intercambios registrados
'; return; } var html = '| Fabricante | # Parte | Nombre | Calidad | Precio | Garantia |
|---|---|---|---|---|---|
| ' + esc(a.manufacturer) + ' | ' + esc(a.part_number) + ' | ' + esc(a.name || '') + ' | ' + esc(a.quality || '') + ' | ' + (a.price_usd ? '$' + a.price_usd : '') + ' | ' + (a.warranty_months || '') + ' |