import{labels,isEffective,today,projection,cashflow,preliminary}from'./engine.mjs';import{appUrl,$,money,tabs,el,source,get,field}from'./tool-common.mjs';tabs();let plans=[],page=0;$('#asof').value=today();$('#elig-form [name=date]').value=today(); function current(date){const map=new Map();for(const p of plans){if(!isEffective(p,date))continue;const key=p.plan_key||p.name+'|'+p.insurer;if(!map.has(key)||map.get(key).version(!category||p.category===category)&&(p.name+' '+p.insurer).toLowerCase().includes(search));page=Math.min(page,Math.max(0,Math.ceil(list.length/10)-1));$('#catalog-prev').disabled=page===0;$('#catalog-next').disabled=(page+1)*10>=list.length;$('#catalog-page').textContent='Page '+(page+1)+' of '+Math.max(1,Math.ceil(list.length/10));const body=$('#plan-rows');body.replaceChildren();for(const p of list.slice(page*10,page*10+10)){const row=document.createElement('tr'),name=el('td','');name.append(el('strong',p.name),el('small',`v${p.version} · ${p.status==='reference'?'Website reference — dates unconfirmed':'Effective '+p.effective_from}`));row.append(name,el('td',labels[p.category]),el('td',p.insurer));const action=el('td','');action.append(source(p.source_url));const a=el('a','Request guidance');a.className='button';a.href=appUrl('/')+'?interest='+({ulip:'ulip',income:'moneyback',health:'health',term:'life',savings:'savings',retirement:'savings',group:'guidance'}[p.category])+'#enquiry';action.append(document.createElement('br'),a);row.append(action);body.append(row)}$('#catalog-summary').textContent=list.length+' plan references shown for '+date+'.';if(!list.length){const row=el('tr',''),td=el('td','No plans match these filters.');td.colSpan=4;row.append(td);body.append(row)}} for(const id of ['category','search','asof'])$('#'+id).addEventListener('input',()=>{page=0;render()}); $('#catalog-prev').onclick=()=>{page--;render()};$('#catalog-next').onclick=()=>{page++;render()}; function calcFields(){const kind=$('#calc-kind').value;$('#calc-fields').innerHTML=kind==='ulip'?field('annual','Annual contribution (₹)',120000,1)+field('years','Years',15,1,50,1)+field('rate','Assumed gross rate (% p.a.)',4,-20,20,.1)+field('charge','Assumed annual charge (%)',0,0,20,.1):kind==='income'?field('premium','Annual premium from illustration (₹)','',1)+field('years','Premium-paying years','',1,50,1)+field('payout','Each scheduled payout (₹)','',0)+field('count','Number of payouts','',0,100,1)+field('maturity','Maturity amount from illustration (₹)','',0):field('quote','Annual premium from insurer quote (₹)','',1)+field('years','Budget years',1,1,50,1);$('#calc-result').textContent='Your calculation will appear here.';$('#calc-note').textContent=kind==='ulip'?'Generic annual contribution-at-start model. Assumed rates are not forecasts. A flat charge assumption is not the insurer’s charge structure. No mortality costs, allocation charges, bonuses, riders, taxes or actual plan benefits are modelled. ULIP investment risk is borne by the policyholder; liquidity is restricted during the first five years.':kind==='income'?'Use the actual insurer benefit illustration amounts. This totals your entered cash flows; it does not calculate a guaranteed benefit or investment return. Timing, bonuses, tax, discounting and surrender are excluded.':'Uses the quote you enter, including applicable taxes if included in that quote. Future renewals may change. It does not estimate insurer pricing from age or medical conditions.'}calcFields();$('#calc-kind').onchange=calcFields; $('#calc-form').onsubmit=e=>{e.preventDefault();const v=Object.fromEntries(new FormData(e.currentTarget));try{const kind=$('#calc-kind').value;let text;if(kind==='ulip'){const n=projection(+v.annual,+v.years,+v.rate,+v.charge);text='Total contributions: '+money(n.contributed)+'\nIllustrative scenario value: '+money(n.value)+'\nThis is not an insurer quote or a promise of return.'}else if(kind==='income'){const n=cashflow(+v.premium,+v.years,+v.payout,+v.count,+v.maturity);text='Total entered premiums: '+money(n.contributed)+'\nTotal entered payouts + maturity: '+money(n.receipts)+'\nAmounts come from your inputs; no plan-specific payout is inferred.'}else{if(+v.quote<=0||+v.years<1||+v.years>50||!Number.isInteger(+v.years))throw Error('Check your quote and budget years.');text='Monthly budget at today’s quote: '+money(+v.quote/12)+'\nTotal if the annual quote stayed unchanged: '+money(+v.quote*+v.years)+'\nRenewal premiums can change.'}$('#calc-result').textContent=text}catch(error){$('#calc-result').textContent=error.message}}; $('#elig-form').onsubmit=e=>{e.preventDefault();try{const v=Object.fromEntries(new FormData(e.currentTarget)),p=plans.find(p=>String(p.id)===$('#elig-plan').value);if(!p)throw Error('Select a plan.');const result=preliminary(p,+v.age,+v.term,v.date);$('#elig-result').textContent=result.text+'\nSource checked: '+(p.checked_on||'not recorded')+'\nConfigured limits: '+(Object.entries(p.rules||{}).map(([k,v])=>k.replaceAll('_',' ')+': '+v).join('; ')||'not yet verified')}catch(error){$('#elig-result').textContent=error.message}}; try{plans=await get('/catalogue');render();$('#elig-plan').replaceChildren(new Option('Select a plan',''));for(const p of plans)$('#elig-plan').add(new Option(p.name+' · v'+p.version,String(p.id)))}catch(error){$('#catalog-summary').textContent=error.message} const requestedCalculator=new URLSearchParams(location.search).get("calculator");if(["ulip","income","health"].includes(requestedCalculator)){document.getElementById("calc-tab").click();document.getElementById("calc-kind").value=requestedCalculator;calcFields();}