(function() { const BrandCatalog = { currentBrand: null, currentCategory: null, state: 'brands', _lastItems: [], el: function(id) { return document.getElementById(id); }, show: function() { this.el('brandCatalogOverlay').style.display = 'block'; document.body.style.overflow = 'hidden'; this.loadBrands(); }, hide: function() { this.el('brandCatalogOverlay').style.display = 'none'; document.body.style.overflow = ''; this.reset(); }, reset: function() { this.currentBrand = null; this.currentCategory = null; this.state = 'brands'; this._lastItems = []; }, loading: function(on) { this.el('brandCatalogLoading').style.display = on ? 'block' : 'none'; }, setContent: function(html) { this.el('brandCatalogContent').innerHTML = html; }, setBreadcrumb: function(html) { this.el('brandCatalogBreadcrumb').innerHTML = html; }, loadBrands: function() { this.loading(true); this.state = 'brands'; this.setBreadcrumb('Marcas de vehiculo'); fetch('/pos/api/catalog/vehicle-brands') .then(r => r.json()) .then(data => { this.loading(false); if (!data.brands || !data.brands.length) { this.setContent('
No se encontraron marcas.
'); return; } let html = ''; data.brands.forEach(b => { html += 'Error al cargar marcas: ' + escapeHtml(err.message) + '
'); }); }, selectBrand: function(brandName) { this.currentBrand = brandName; this.loadCategories(brandName); }, loadCategories: function(brandName) { this.loading(true); this.state = 'categories'; this.setBreadcrumb( 'Marcas › ' + escapeHtml(brandName) + '' ); fetch('/pos/api/catalog/brand-categories?brand=' + encodeURIComponent(brandName)) .then(r => r.json()) .then(data => { this.loading(false); if (!data.categories || !data.categories.length) { this.setContent('No se encontraron categorias para esta marca.
'); return; } let html = ''; data.categories.forEach(c => { html += 'Error al cargar categorias: ' + escapeHtml(err.message) + '
'); }); }, selectCategory: function(catId, catName) { this.currentCategory = { id: catId, name: catName }; this.loadParts(this.currentBrand, catId); }, loadParts: function(brandName, categoryId) { this.loading(true); this.state = 'parts'; this.setBreadcrumb( 'Marcas › ' + '' + escapeHtml(brandName) + ' › ' + '' + escapeHtml(this.currentCategory.name) + '' ); fetch('/pos/api/catalog/brand-parts?brand=' + encodeURIComponent(brandName) + '&category_id=' + encodeURIComponent(categoryId)) .then(r => r.json()) .then(data => { this.loading(false); this._lastItems = data.items || []; if (!data.items || !data.items.length) { this.setContent('No se encontraron refacciones.
'); return; } let html = 'Error al cargar refacciones: ' + escapeHtml(err.message) + '
'); }); }, addToCart: function(partId, event) { if (event) event.stopPropagation(); const part = this._lastItems.find(function(p) { return p.id === partId; }); if (!part) { alert('Error: no se encontro la refaccion'); return; } if (window.CatalogApp && CatalogApp.addToCart) { CatalogApp.addToCart({ id: part.id, part_number: part.oem_part_number || 'N/A', name: part.name || 'Refaccion', brand: '', price: part.local_price || 0, tax_rate: 0.16, unit: 'PZA', stock: part.local_stock || 0, source: 'oem-brand', inventory_id: null }, 1); const btn = event.target; const oldText = btn.textContent; btn.textContent = 'Agregado!'; btn.style.background = 'var(--color-success)'; setTimeout(function() { btn.textContent = oldText; btn.style.background = ''; }, 1500); return; } alert('Carrito no disponible. Asegurate de que la pagina haya cargado completamente.'); } }; function escapeHtml(text) { if (!text) return ''; const div = document.createElement('div'); div.textContent = text; return div.innerHTML; } window.BrandCatalog = BrandCatalog; })();