<?php if (!defined('DASHBOARD_ROOT')) { http_response_code(403); exit; } RBAC::enforce('modify.listing'); $mode = isset($listingId) && $listingId > 0 ? 'edit' : 'create'; ?> <div class="form-actions"> <a id="listing-save-btn" class="btn btn-primary" href="javascript:void(0)" data-mode="<?= $mode ?> " data-id="<?= $listingId ?? 0 ?> "><span><?= $mode === 'edit' ? 'Save Changes' : 'Create Listing' ?> </span></a><?php if ($mode === 'edit'): ?> <a id="listing-preview" class="btn" href="javascript:void(0)"><span>Preview</span></a><?php endif; ?> </div> <script nonce="<?= $cspNonce ?? '' ?> "> document.getElementById('listing-save-btn').addEventListener('click', function() { var btn  = this; var mode = btn.dataset.mode; var id   = btn.dataset.id; var fields = ['title','slug','short_desc','description','price','sale_price','category_id', 'display_order','status_code','visibility_code','image_url','unit_pricing_code', 'unit_increment','unit_label','unit_price_per','unit_tiers']; var payload = { entity: 'listing' }; if (mode === 'edit') payload.id = id; fields.forEach(function(f) { var el = document.getElementById('listing-' + f); if (el) payload[f] = el.value; }); var action = mode === 'edit' ? 'modify' : 'post'; btn.textContent = 'Saving...'; window.dashFetch(action, payload).then(function(r) { if (r && r.ok) { btn.textContent = 'Saved ✓'; setTimeout(function() { location.reload(); }, 800); } else { var msg = 'Save failed'; if (r && r.phase) msg += ' at ' + r.phase; if (r && r.error) msg += ': ' + r.error; alert(msg); btn.textContent = 'Save Changes'; } }).catch(function(e) { alert('Network error: ' + e.message); btn.textContent = 'Save Changes'; }); }); </script>