(function() { const BrandCatalog = { state: 'brands', _allBrands: [], _lastItems: [], _offset: 0, _limit: 50, _total: 0, // Navigation state nav: { brand: null, brandId: null, model: null, modelId: null, year: null, yearId: null, engine: null, myeId: null, category: null, categoryId: null }, el: function(id) { return document.getElementById(id); }, _getToken: function() { return localStorage.getItem('pos_token'); }, _headers: function() { var token = this._getToken(); return { 'Authorization': 'Bearer ' + (token || ''), 'Content-Type': 'application/json' }; }, _checkAuth: function(resp) { if (resp.status === 401) { localStorage.removeItem('pos_token'); window.location.href = '/pos/login'; return false; } return true; }, show: function() { if (!this._getToken()) { window.location.href = '/pos/login'; return; } 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.state = 'brands'; this.nav = { brand: null, brandId: null, model: null, modelId: null, year: null, yearId: null, engine: null, myeId: null, category: null, categoryId: null }; this._allBrands = []; this._lastItems = []; this._offset = 0; this._total = 0; this.el('brandCatalogSearch').innerHTML = ''; }, loading: function(on) { this.el('brandCatalogLoading').style.display = on ? 'block' : 'none'; }, setContent: function(html) { this.el('brandCatalogContent').innerHTML = html; }, setSearch: function(html) { this.el('brandCatalogSearch').innerHTML = html; }, setBreadcrumb: function(html) { this.el('brandCatalogBreadcrumb').innerHTML = html; }, buildBreadcrumb: function() { var parts = []; parts.push('Marcas'); if (this.nav.brand) { parts.push('' + escapeHtml(this.nav.brand) + ''); } if (this.nav.model) { parts.push('' + escapeHtml(this.nav.model) + ''); } if (this.nav.year) { parts.push('' + this.nav.year + ''); } if (this.nav.engine) { parts.push('' + escapeHtml(this.nav.engine) + ''); } if (this.nav.category) { parts.push('' + escapeHtml(this.nav.category) + ''); } this.setBreadcrumb(parts.join(' › ')); }, // ---------- BRANDS ---------- loadBrands: function() { this.loading(true); this.state = 'brands'; this.reset(); this.setBreadcrumb('Marcas de vehiculo'); this.setSearch( '' ); var self = this; fetch('/pos/api/catalog/vehicle-brands', { headers: this._headers() }) .then(function(r) { if (!self._checkAuth(r)) return null; return r.json(); }) .then(function(data) { if (!data) return; self.loading(false); self._allBrands = data.brands || []; if (!self._allBrands.length) { self.setContent('
No se encontraron marcas.
'); return; } self.renderBrandList(self._allBrands); }) .catch(function(err) { self.loading(false); self.setContent('Error al cargar marcas: ' + escapeHtml(err.message) + '
'); }); }, renderBrandList: function(brands) { var html = ''; brands.forEach(function(b) { html += 'No se encontraron modelos.
'); return; } var html = ''; models.forEach(function(m) { html += 'Error al cargar modelos: ' + escapeHtml(err.message) + '
'); }); }, selectModel: function(modelId, modelName) { this.nav.model = modelName; this.nav.modelId = modelId; this.loadYears(modelId); }, // ---------- YEARS ---------- loadYears: function(modelId) { this.loading(true); this.state = 'years'; this.setSearch(''); this.buildBreadcrumb(); var self = this; fetch('/pos/api/catalog/years?model_id=' + encodeURIComponent(modelId), { headers: this._headers() }) .then(function(r) { if (!self._checkAuth(r)) return null; return r.json(); }) .then(function(data) { if (!data) return; self.loading(false); var years = data.data || []; if (!years.length) { self.setContent('No se encontraron años.
'); return; } var html = ''; years.forEach(function(y) { html += 'Error al cargar años: ' + escapeHtml(err.message) + '
'); }); }, selectYear: function(yearId, yearCar) { this.nav.year = yearCar; this.nav.yearId = yearId; this.loadEngines(this.nav.modelId, yearId); }, // ---------- ENGINES ---------- loadEngines: function(modelId, yearId) { this.loading(true); this.state = 'engines'; this.setSearch(''); this.buildBreadcrumb(); var self = this; fetch('/pos/api/catalog/engines?model_id=' + encodeURIComponent(modelId) + '&year_id=' + encodeURIComponent(yearId), { headers: this._headers() }) .then(function(r) { if (!self._checkAuth(r)) return null; return r.json(); }) .then(function(data) { if (!data) return; self.loading(false); var engines = data.data || []; if (!engines.length) { self.setContent('No se encontraron motores.
'); return; } var html = ''; engines.forEach(function(e) { html += 'Error al cargar motores: ' + escapeHtml(err.message) + '
'); }); }, selectEngine: function(myeId, engineName) { this.nav.engine = engineName; this.nav.myeId = myeId; this.loadCategories(myeId); }, // ---------- CATEGORIES ---------- loadCategories: function(myeId) { this.loading(true); this.state = 'categories'; this.setSearch(''); this.buildBreadcrumb(); var self = this; fetch('/pos/api/catalog/categories?mye_id=' + encodeURIComponent(myeId) + '&mode=oem', { headers: this._headers() }) .then(function(r) { if (!self._checkAuth(r)) return null; return r.json(); }) .then(function(data) { if (!data) return; self.loading(false); var categories = data.data || []; if (!categories.length) { self.setContent('No se encontraron categorias.
'); return; } var html = ''; categories.forEach(function(c) { html += 'Error al cargar categorias: ' + escapeHtml(err.message) + '
'); }); }, selectCategory: function(catId, catName) { this.nav.category = catName; this.nav.categoryId = catId; this._offset = 0; this.loadParts(this.nav.myeId, catId, ''); }, // ---------- PARTS ---------- loadParts: function(myeId, categoryId, searchTerm) { this.loading(true); this.state = 'parts'; this.buildBreadcrumb(); this.setSearch( 'Error al cargar refacciones: ' + escapeHtml(err.message) + '
'); }); }, renderPartsList: function(items, searchTerm) { var html = ''; if (!items.length) { html += 'No se encontraron refacciones.
' + '' + '