<divclass="row g-3 align-items-center"><divclass="col-xl-5"><divclass="input-group"><spanclass="input-group-text bg-transparent"><iclass="bi bi-search"></i></span><inputid="solSearch"class="form-control"type="search"placeholder="Søg i titel, problem, løsning, sag eller kunde…"></div></div>
<divclass="row g-3 mb-3"><divclass="col-md-6"><labelclass="form-label">Tags</label><inputclass="form-control"id="editTags"></div><divclass="col-md-6"><labelclass="form-label">Produkter/systemer</label><inputclass="form-control"id="editProducts"></div></div><divclass="mb-4"><labelclass="form-label">Ændringsnote</label><inputclass="form-control"id="editNote"placeholder="Hvad er ændret?"></div>
<divclass="modal fade"id="archiveSolutionModal"tabindex="-1"data-bs-backdrop="static"><divclass="modal-dialog modal-dialog-centered"><divclass="modal-content border-0 shadow"><divclass="modal-body p-4"><divclass="d-flex gap-3"><divclass="fs-2 text-danger"><iclass="bi bi-archive"></i></div><div><h2class="h4">Arkivér løsningen?</h2><pclass="text-muted mb-2">Løsningen fjernes fra sagen og en udgivet vidensartikel skjules. Historik og data bevares.</p><divclass="fw-semibold"id="archiveSolutionName"></div></div></div></div><divclass="modal-footer border-0 pt-0"><buttonclass="btn btn-outline-secondary"data-bs-dismiss="modal">Annuller</button><buttonclass="btn btn-danger"id="archiveConfirmBtn">Ja, arkivér</button></div></div></div></div>
function renderEditorCustomer(){const selected=document.getElementById('editCustomerSelected'),input=document.getElementById('editCustomerSearch');if(editorCustomer){selected.innerHTML=`<iclass="bi bi-building me-1"></i><strong>${esc(editorCustomer.name)}</strong><buttonclass="btn btn-link btn-sm p-0 ms-1"type="button"id="changeEditorCustomer">Skift</button>`;input.value='';document.getElementById('changeEditorCustomer').onclick=()=>{editorCustomer=null;renderEditorCustomer();input.focus();};}else selected.textContent='Ingen kunde valgt endnu';}
async function searchEditorCustomers(){const input=document.getElementById('editCustomerSearch'),results=document.getElementById('editCustomerResults'),q=input.value.trim();if(q.length<2){results.classList.add('d-none');results.innerHTML='';return;}try{constrows=awaitapi(`/api/v1/search/customers?q=${encodeURIComponent(q)}`);if(input.value.trim()!==q)return;results.innerHTML=rows.length?rows.map(c=>`<buttontype="button"class="list-group-item list-group-item-action"data-customer-id="${Number(c.id)}"data-customer-name="${esc(c.name)}"><strong>${esc(c.name)}</strong><smallclass="d-block text-muted">${esc(c.email||c.cvr_nummer||'')}</small></button>`).join(''):'<divclass="list-group-item text-muted">Ingen kunder fundet</div>';results.classList.remove('d-none');results.querySelectorAll('[data-customer-id]').forEach(button=>button.onclick=()=>{editorCustomer={id:Number(button.dataset.customerId),name:button.dataset.customerName};results.classList.add('d-none');renderEditorCustomer();});}catch(e){results.innerHTML=`<divclass="list-group-item text-danger">${esc(e.message)}</div>`;results.classList.remove('d-none');}}
window.saveManagedSolution=async()=>{const id=document.getElementById('editSagId').value,payload={title:document.getElementById('editTitle').value.trim(),solution_type:document.getElementById('editType').value,result:document.getElementById('editResult').value,visibility:document.getElementById('editVisibility').value,problem:document.getElementById('editProblem').value.trim(),root_cause:document.getElementById('editCause').value.trim(),investigation:document.getElementById('editInvestigation').value.trim(),description:document.getElementById('editDescription').value.trim(),workaround:document.getElementById('editWorkaround').value.trim(),tags:document.getElementById('editTags').value.split(',').map(x=>x.trim()).filter(Boolean),products:document.getElementById('editProducts').value.split(',').map(x=>x.trim()).filter(Boolean),change_note:document.getElementById('editNote').value.trim()||'Redigeret fra Løsninger'};if(!payload.title||!payload.description){alert('Titel og endelig løsning skal udfyldes');return;}try{if(editorCustomer?.id&&editorCustomer.id!==editorOriginalCustomerId)await api(`/api/v1/sag/${id}/customer/replace`,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({customer_id:editorCustomer.id})});await api(`/api/v1/sag/${id}/solution`,{method:'PATCH',headers:{'Content-Type':'application/json'},body:JSON.stringify(payload)});bootstrap.Offcanvas.getInstance(document.getElementById('solutionEditor'))?.hide();load();}catch(e){alert(e.message);}};