// konfigurator-batteri.js
// Visar: batteri
// === BATTERI (FRISTÅENDE) KONFIGURATOR (kopplad till Produktkatalog) ===
let _batSelected=null, _batPrice=0, _batDeduction='green', _batOwners=1, _batFinYears=15;
let _batProducts=[];
// Keep backward compat vars
let _batBrand=null, _batSize=null, _batSizePrice=0, _batAddons={};
function initBatConfig(){ var _lbl=document.getElementById('cfgFileLabel');if(_lbl)_lbl.textContent='konfigurator-batteri.js';
_batProducts = (typeof catalogProducts !== 'undefined' ? catalogProducts : []).filter(function(p){ return p.cat === 'batteri' || p.cat === 'batteri_utbyggnad'; });
renderBatBrands();
updateBatCalc();
}
function renderBatBrands(){
var c=document.getElementById('batBrandCards');if(!c)return;
if(!_batProducts.length) { c.innerHTML='<div style="padding:16px;color:#94a3b8;font-size:13px">Inga batteri-produkter i katalogen</div>'; return; }
c.innerHTML=_batProducts.map(function(p){
var selected = _batSelected === p.id;
var desc = p.desc || '';
var kwh = p.kwhCapacity ? p.kwhCapacity + ' kWh' : '';
var greenBadge = p.greenTechEligible ? '<div style="font-size:10px;color:#059669;margin-top:4px">✓ Grön teknik-avdrag</div>' : '';
return '<div onclick="selectBatProduct(\''+p.id+'\','+p.price+')" style="padding:16px;border:2px solid '+(selected?'#3b82f6':'#e5e7eb')+';border-radius:10px;cursor:pointer;background:'+(selected?'#f0f9ff':'#fff')+'">'
+'<div style="display:flex;align-items:center;justify-content:space-between">'
+'<div>'
+'<div style="font-weight:600;font-size:15px">'+p.name+'</div>'
+'<div style="font-size:12px;color:#64748b;margin-top:2px">'+desc+(kwh?' — '+kwh:'')+'</div>'
+greenBadge
+'</div>'
+'<div style="text-align:right">'
+'<div style="font-size:18px;font-weight:700">'+fmt(p.price)+'</div>'
+(selected?'<div style="color:#3b82f6;font-size:18px;margin-top:2px">✓</div>':'')
+'</div></div></div>';
}).join('');
// Hide size cards since products are complete units
var sizeC=document.getElementById('batSizeCards');if(sizeC)sizeC.style.display='none';
var sizeL=document.getElementById('batSizeLabel');if(sizeL)sizeL.style.display='none';
}
function selectBatProduct(id,price){if(_batSelected===id){_batSelected=null;_batPrice=0;}else{_batSelected=id;_batPrice=price;}_batSizePrice=_batPrice;renderBatBrands();updateBatCalc();}
// Keep old functions as aliases
function selectBatBrand(id){selectBatProduct(id,0);}
function selectBatSize(kwh,price){_batSizePrice=price;updateBatCalc();}
function renderBatSizes(){}
function renderBatAddons(){var c=document.getElementById('batAddonCards');if(c)c.style.display='none';}
function toggleBatAddon(id,price,checked){}
function setBatDeduction(t){_batDeduction=t;cfgSetBtns('deduct','bat-deduct-btn',t);updateBatCalc();}
function setBatOwners(n){_batOwners=n;cfgSetBtns('owner','bat-owner-btn',n);updateBatCalc();}
function setBatFinYears(y){_batFinYears=y;cfgSetBtns('fin','bat-fin-btn',y);updateBatCalc();}
function updateBatCalc(){
var subtotal=_batPrice || _batSizePrice;
var prod = _batProducts.find(function(p){ return p.id === _batSelected; });
var hasGreen = prod ? prod.greenTechEligible : false;
var deductType = hasGreen ? _batDeduction : 'none';
var deduct=cfgDeduct(subtotal,0.35,deductType,_batOwners);
var total=subtotal-deduct;
var monthly=cfgMonthly(total,_batFinYears);
document.getElementById('batPrBattery').textContent=fmt(subtotal);
var addonsEl=document.getElementById('batPrAddons');if(addonsEl)addonsEl.textContent='0 kr';
document.getElementById('batPrSubtotal').textContent=fmt(subtotal);
document.getElementById('batDeductLabel').textContent=deductType==='green'?'GRÖNT TEKNIK-AVDRAG':'INGET AVDRAG';
document.getElementById('batDeductKr').textContent=deduct?'-'+fmt(deduct):'0 kr';
document.getElementById('batPrTotal').textContent=fmt(total);
document.getElementById('batFinInfo').textContent='('+_batFinYears+' år, 4.9%)';
document.getElementById('batPrMonthly').textContent=fmt(monthly)+'/mån';
// Update shared summary
if(typeof cfgAddItem==='function') { var batName = prod ? prod.name : 'Batteri'; cfgAddItem('batteri', batName, subtotal, deductType, 0.35); }
}