js/konfigurator-varmepump.js.bak-20260401

Code: DEV-DCFF7650 Size: 4.8 KB Lines: 100 Path: /home/prodconfig.wenesthosting.com/dev.solargroup.wenest.se/js/konfigurator-varmepump.js.bak-20260401

Task / Comment

Open report form
// konfigurator-varmepump.js
// Visar: varmepump

// === VÄRMEPUMP KONFIGURATOR (kopplad till Produktkatalog) ===
let _vpSelected=null, _vpPrice=0, _vpInstall=0, _vpDeduction='rot', _vpOwners=1, _vpFinYears=15;
let _vpProducts=[];

function initVpConfig(){ var _lbl=document.getElementById('cfgFileLabel');if(_lbl)_lbl.textContent='konfigurator-varmepump.js';
    // Reset state
    _vpSelected=null; _vpPrice=0; _vpInstall=0; _vpDeduction='rot'; _vpOwners=1; _vpFinYears=15;
    _vpProducts = (typeof catalogProducts !== 'undefined' ? catalogProducts : []).filter(function(p){ return p.cat === 'varmepump'; });
    renderVpPumps();
    updateVpCalc();
}
function renderVpPumps(){
    var c=document.getElementById('vpPumpCards');if(!c)return;
    if(!_vpProducts.length) { c.innerHTML='<div style="padding:16px;color:#94a3b8;font-size:13px">Inga värmepump-produkter i katalogen</div>'; return; }
    c.innerHTML=_vpProducts.map(function(p){
        var selected = _vpSelected === p.id;
        var desc = p.desc || '';
        var rotBadge = p.rotEligible ? '<div style="font-size:10px;color:#059669;margin-top:4px">✓ ROT-avdrag</div>' : '';
        var greenBadge = p.greenTechEligible ? '<div style="font-size:10px;color:#059669;margin-top:4px">✓ Grön teknik-avdrag</div>' : '';
        return '<div onclick="selectVpPump(\''+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+'</div>'
            +rotBadge+greenBadge
            +'</div>'
            +'<div style="text-align:right">'
            +'<div style="font-size:18px;font-weight:700">'+fmt(p.price)+'</div>'
            +'<div style="font-size:11px;color:#64748b">inkl. installation</div>'
            +(selected?'<div style="color:#3b82f6;font-size:18px;margin-top:2px">✓</div>':'')
            +'</div></div></div>';
    }).join('');
}
function selectVpPump(id,price){if(_vpSelected===id){_vpSelected=null;_vpPrice=0;}else{_vpSelected=id;_vpPrice=price;}renderVpPumps();updateVpCalc();}
function setVpDeduction(t){_vpDeduction=t;updateVpCalc();}
function setVpOwners(n){_vpOwners=n;updateVpCalc();}
function setVpFinYears(y){_vpFinYears=y;updateVpCalc();}
function updateVpCalc(){
    var subtotal=_vpPrice;
    var prod = _vpProducts.find(function(p){ return p.id === _vpSelected; });
    var hasRot = prod ? prod.rotEligible : false;
    var hasGreen = prod ? prod.greenTechEligible : false;
    var deductType = hasRot ? (_vpDeduction === 'rot' ? 'rot' : _vpDeduction) : (hasGreen ? (_vpDeduction === 'green' ? 'green' : _vpDeduction) : 'none');
    var laborRatio = 0.5;
    var deduct=cfgDeduct(subtotal,laborRatio,deductType,_vpOwners);
    var total=subtotal-deduct;
    var monthly=cfgMonthly(total,_vpFinYears);
    var deductLabel = deductType==='green'?'GRÖNT TEKNIK-AVDRAG':deductType==='rot'?'ROT-AVDRAG':'INGET AVDRAG';
    renderPrisSidebar('vpPrisSidebar', {
        lines: [
            {label:'Värmepump', value:subtotal},
            {label:'Installation', detail:'ingår', value:0}
        ],
        subtotal: subtotal,
        deductType: deductType,
        deductLabel: deductLabel,
        deductAmount: deduct,
        owners: _vpOwners,
        total: total,
        finYears: _vpFinYears,
        monthly: monthly,
        category: 'varmepump',
        onDeductChange: 'setVpDeduction',
        onOwnerChange: 'setVpOwners',
        onFinChange: 'setVpFinYears'
    });
    if(typeof cfgAddItem==='function') { var vpName = prod ? prod.name : 'Värmepump'; cfgAddItem('varmepump', vpName, subtotal, deductType, laborRatio); }
}


// Restore sparade värmepump-värden vid öppning av befintlig kalkyl
function restoreVpConfig(quote) {
    try {
        var cd = quote.config_data ? (typeof quote.config_data === 'string' ? JSON.parse(quote.config_data) : quote.config_data) : {};
        var vpData = cd.items ? cd.items.varmepump : cd;
        if(!vpData) return;

        // Återställ vald produkt
        if(vpData.product_id) {
            var prod = _vpProducts.find(function(p){ return p.id === vpData.product_id; });
            if(prod) {
                _vpSelected = prod.id;
                _vpPrice = prod.price;
                renderVpPumps();
            }
        }

        // Återställ avdrag/ägare/finans
        if(vpData.deduction) _vpDeduction = vpData.deduction;
        if(vpData.owners) _vpOwners = vpData.owners;
        if(vpData.finYears !== undefined) _vpFinYears = vpData.finYears;

        updateVpCalc();
        _cfgDirty = false;
    } catch(e) { console.error('restoreVpConfig:', e); }
}