// kalkyl-ny.js — Ny Kalkyl startsida
// Visar: head + kategorigrid, INGEN konfigurator, INGEN sidebar
var _newCalcActive = false;
function newCalc(kundData) {
_newCalcActive = true; _kalkylItems = {}; currentQuoteId = null; _cfgDirty = false; if(typeof _vpPumps!=="undefined") _vpPumps=[]; if(typeof _fkWindows!=="undefined") _fkWindows=[]; if(typeof _lbTillval!=="undefined") _lbTillval=[]; if(typeof _lbSelected!=="undefined"){_lbSelected=null;_lbPrice=0;} if(typeof _vpSelected!=="undefined"){_vpSelected=null;_vpPrice=0;} var _t=document.getElementById("kalkylTitle"); if(_t) _t.textContent="Ny Kalkyl"; if(typeof _batInitialized!=="undefined") _batInitialized=false;
var _lbl = document.getElementById('cfgFileLabel'); if(_lbl) _lbl.textContent = 'kalkyl-ny.js';
// Visa konfigurator-sidan
currentPage = 'konfigurator';
document.querySelectorAll('.page-content').forEach(function(p){p.classList.remove('active');});
var pageEl = document.getElementById('page-konfigurator'); if(pageEl) pageEl.classList.add('active');
document.querySelectorAll('.nav-item').forEach(function(n){
if(n.dataset && n.dataset.page === 'konfigurator') n.classList.add('active');
else n.classList.remove('active');
});
// Dölj lista, visa config-area
var listView = document.getElementById('kalkylListView'); if(listView) listView.style.display = 'none';
var configView = document.getElementById('kalkylConfigView'); if(configView) configView.style.display = 'block';
// Rensa ALLT
_resetCalcView();
// Kunddata
pendingKalkylCustomer = kundData || null;
if(typeof populateKalkylFromCustomer === 'function') populateKalkylFromCustomer();
// Visa prospektbilder
var photosSection = document.getElementById('kalkylPhotosSection'); if(photosSection) photosSection.style.display = '';
// Visa kategorigrid
var catGrid = document.getElementById('cfgCategoryGrid');
if(catGrid) {
if(typeof setKalkylViewMode === 'function') setKalkylViewMode('new');
catGrid.style.display = 'grid';
_loadCategoryCards(catGrid);
}
// Säkerställ att inget async-anrop ångrar vårt reset
setTimeout(_resetCalcView, 100);
setTimeout(_resetCalcView, 500);
}
function _resetCalcView() {
// Rensa select
var cs = document.getElementById('categorySelect');
if(cs) { cs.value = ''; }
// Dölj ALLA konfiguratorer
['solarConfigView','genericConfigView','fonsterConfigView','batteriConfigView',
'laddboxConfigView','taktvatConfigView','varmepumpConfigView','takConfigView',
'isoleringConfigView'].forEach(function(v){
var el = document.getElementById(v); if(el) el.style.display = 'none';
});
// Dölj affärvyn
var afv = document.getElementById('affarView'); if(afv) afv.style.display = 'none';
var pqv = document.getElementById('productQuoteView'); if(pqv) pqv.remove();
// Visa grid
var catGrid = document.getElementById('cfgCategoryGrid');
if(typeof setKalkylViewMode === 'function') setKalkylViewMode(_newCalcActive ? 'new' : 'config');
if(catGrid && _newCalcActive) catGrid.style.display = 'grid';
}
function _loadCategoryCards(el) {
fetch('/api/categories.php?all=1').then(function(r){return r.json();}).then(function(d){
var cats = (d.success && d.categories) ? d.categories.filter(function(c){return !parseInt(c.is_tillval);}) : [];
if(!cats.length) {
cats = [
{id:'solceller', label:'Solpaneler', description:'Solpaneler och solcellsanläggningar'},
{id:'batteri', label:'Batteri', description:'Batterilösningar för energilagring'},
{id:'laddbox', label:'Laddbox', description:'Elbilsladdning'},
{id:'varmepump', label:'Värmepump', description:'Luft/luft och luft/vatten värmepumpar'},
{id:'taktvatt', label:'Taktvätt', description:'Högtryckstvätt och mossbehandling'},
{id:'tak', label:'Tak', description:'Takbyten'},
{id:'fonster', label:'Fönster', description:'Energifönster'},
{id:'dorrar', label:'Dörrar', description:'Ytterdörrar och balkongdörrar'},
{id:'isolering', label:'Isolering', description:'Tilläggsisolering'}
];
}
var catCounts = {};
if(typeof catalogProducts !== 'undefined') {
catalogProducts.forEach(function(p) { catCounts[p.cat] = (catCounts[p.cat]||0) + 1; });
}
el.innerHTML = cats.map(function(c) {
var count = catCounts[c.id] || 0;
var img = c.image ? '<img src="'+c.image+'" style="width:100%;height:120px;object-fit:cover;border-radius:8px 8px 0 0">'
: '<div style="height:80px;background:linear-gradient(135deg,#024550,#059669);border-radius:8px 8px 0 0;display:flex;align-items:center;justify-content:center"><span style="font-size:28px;opacity:.5">☀</span></div>';
return '<div onclick="_newCalcActive=false;document.getElementById(\'categorySelect\').value=\''+c.id+'\';changeCategory()" style="border:1.5px solid #e5e7eb;border-radius:10px;cursor:pointer;overflow:hidden;transition:all .15s;background:#fff" onmouseover="this.style.borderColor=\'#024550\';this.style.transform=\'translateY(-2px)\';this.style.boxShadow=\'0 4px 12px rgba(0,0,0,.1)\'" onmouseout="this.style.borderColor=\'#e5e7eb\';this.style.transform=\'none\';this.style.boxShadow=\'none\'">'
+img
+'<div style="padding:12px"><div style="display:flex;justify-content:space-between;align-items:center"><div style="font-weight:700;font-size:14px;color:#1a1a1a">'+c.label+'</div>'+(count?'<span style="font-size:10px;background:#e0f2fe;color:#0284c7;padding:2px 8px;border-radius:10px;font-weight:600">'+count+'</span>':'')+'</div>'
+'<div style="font-size:11px;color:#64748b;margin-top:2px">'+(c.description||c.desc||'')+'</div></div></div>';
}).join('');
}).catch(function(){});
}