(function () { 'use strict'; const importModal = new bootstrap.Modal(document.getElementById('importModal')); let currentLibraryItemId = null; // Open preview modal when any "Preview & Import" button is clicked document.getElementById('libraryGrid')?.addEventListener('click', function (e) { const btn = e.target.closest('.btn-import'); if (!btn) return; currentLibraryItemId = parseInt(btn.dataset.itemId, 10); const itemName = btn.dataset.itemName; document.getElementById('importModalLabel').textContent = 'Import — ' + itemName; document.getElementById('importModalBody').innerHTML = '
' + '

Loading formula details…

'; document.getElementById('btnConfirmImport').disabled = true; importModal.show(); fetch('/FormulaLibrary/Detail/' + currentLibraryItemId) .then(r => r.json()) .then(renderDetail) .catch(() => { document.getElementById('importModalBody').innerHTML = '
Failed to load formula details.
'; }); }); function renderDetail(d) { let fields = []; try { fields = JSON.parse(d.fieldsJson || '[]'); } catch (_) { } const alreadyBadge = d.alreadyImported ? 'Already in your library' : ''; const inspiredRow = (d.inspiredByName) ? `
Inspired by “${escHtml(d.inspiredByName)}” from ${escHtml(d.inspiredByCompanyName)}
` : ''; const modeBadge = d.outputMode === 'FixedRate' ? 'Fixed Rate' : 'Surface Area (sq ft)'; const fieldRows = fields.map(f => `${escHtml(f.label || f.name)}${escHtml(f.unit || '')}${escHtml(String(f.defaultValue ?? ''))}` ).join(''); const diagramHtml = d.diagramImagePath ? `
Formula diagram
` : ''; document.getElementById('importModalBody').innerHTML = `

${escHtml(d.name)}${alreadyBadge}

${escHtml(d.sourceCompanyName)}

${inspiredRow} ${d.description ? `

${escHtml(d.description)}

` : ''}
${modeBadge} ${d.industryHint ? `${escHtml(d.industryHint)}` : ''}
${d.defaultRate != null ? `

Default rate: ${escHtml(String(d.defaultRate))} ${escHtml(d.rateLabel || '')}

` : ''} ${d.notes ? `

${escHtml(d.notes)}

` : ''}
${diagramHtml} ${fields.length > 0 ? `
Input Fields (${fields.length})
${fieldRows}
FieldUnitDefault
` : '

No fields defined.

'}
Formula Expression
${escHtml(d.formula)}
`; const importBtn = document.getElementById('btnConfirmImport'); if (d.alreadyImported) { importBtn.disabled = true; importBtn.innerHTML = 'Already Imported'; importBtn.classList.replace('btn-primary', 'btn-success'); } else { importBtn.disabled = false; } } // Confirm import document.getElementById('btnConfirmImport')?.addEventListener('click', function () { if (!currentLibraryItemId) return; this.disabled = true; this.innerHTML = 'Importing…'; const form = new FormData(); form.append('libraryItemId', currentLibraryItemId); form.append('__RequestVerificationToken', document.querySelector('input[name="__RequestVerificationToken"]')?.value ?? ''); fetch('/FormulaLibrary/Import', { method: 'POST', body: form }) .then(r => r.json()) .then(res => { if (res.success) { importModal.hide(); showToast('Formula imported to your library!', 'success'); // Mark button on the card const card = document.querySelector(`.btn-import[data-item-id="${currentLibraryItemId}"]`); if (card) { card.classList.replace('btn-outline-primary', 'btn-outline-success'); card.innerHTML = 'Already Imported'; card.disabled = true; card.closest('.card')?.classList.add('border-start', 'border-success', 'border-3'); } } else { showToast(res.message || 'Import failed.', 'danger'); this.disabled = false; this.innerHTML = 'Import to My Formulas'; } }) .catch(() => { showToast('Import failed. Please try again.', 'danger'); this.disabled = false; this.innerHTML = 'Import to My Formulas'; }); }); function escHtml(str) { return String(str) .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"'); } function showToast(msg, type) { const container = document.getElementById('toastContainer') || (() => { const c = document.createElement('div'); c.id = 'toastContainer'; c.className = 'toast-container position-fixed bottom-0 end-0 p-3'; c.style.zIndex = '1100'; document.body.appendChild(c); return c; })(); const el = document.createElement('div'); el.className = `toast align-items-center text-white bg-${type} border-0`; el.setAttribute('role', 'alert'); el.innerHTML = `
${escHtml(msg)}
`; container.appendChild(el); new bootstrap.Toast(el, { delay: 4000 }).show(); el.addEventListener('hidden.bs.toast', () => el.remove()); } })();