window.TimeBillingReview = (() => { const labels={draft:'Kladde',operations:'Drift',development:'Udvikling'}; const needsReview=entry=>Object.hasOwn(labels,entry.billing_method || entry._billing_method || ''); const badge=entry=>needsReview(entry)?`${labels[entry.billing_method || entry._billing_method]} · Chefkontrol`:''; async function approve(entry,hours) { const response=await fetch(`/api/v1/timetracking/time/${Number(entry.id)}/settlement-options`,{credentials:'include'}); if(!response.ok)throw new Error('Afregningsmuligheder kunne ikke hentes'); const data=await response.json(); const host=document.createElement('div');host.className='modal fade';host.tabIndex=-1; host.innerHTML='
'; host.querySelector('[data-label]').textContent=labels[entry.billing_method || entry._billing_method]+' · '+(entry.description || entry.beskrivelse || 'Tidsregistrering'); const select=host.querySelector('select');select.add(new Option('Faktura','invoice')); (data.prepaid_cards||[]).forEach(c=>select.add(new Option(`Klippekort #${c.card_number || c.id}`,`card_${c.id}`))); (data.agreements||[]).forEach(a=>select.add(new Option(`Fastpris #${a.agreement_number || a.id}`,`fpa_${a.id}`))); document.body.append(host);const modal=new bootstrap.Modal(host); return new Promise(resolve=>{ let approved=false; host.addEventListener('hidden.bs.modal',()=>{modal.dispose();host.remove();resolve(approved);},{once:true}); host.querySelector('form').onsubmit=async event=>{ event.preventDefault();const button=host.querySelector('[type=submit]');button.disabled=true; const value=select.value,payload={billing_method:value}; if(value.startsWith('card_')){payload.billing_method='prepaid';payload.prepaid_card_id=Number(value.slice(5));} if(value.startsWith('fpa_')){payload.billing_method='subscription';payload.fixed_price_agreement_id=Number(value.slice(4));} if(Number.isFinite(Number(hours))&&Number(hours)>0)payload.fakturerbar_tid_min=Math.round(Number(hours)*60); try { const result=await fetch(`/api/v1/timetracking/time/${Number(entry.id)}/approve`,{method:'POST',credentials:'include',headers:{'Content-Type':'application/json'},body:JSON.stringify(payload)}); const body=await result.json().catch(()=>({}));if(!result.ok)throw new Error(body.detail || 'Kunne ikke godkende'); approved=true;modal.hide(); }catch(error){host.querySelector('[role=status]').textContent=error.message;button.disabled=false;} };modal.show(); }); } return {needsReview,badge,approve}; })();