<?php if (!defined('DASHBOARD_ROOT')) { http_response_code(403); exit; } RBAC::enforce('view.marketing'); try { $lastSync = Database::scalar("SELECT MAX(last_synced_at) FROM marketing_campaigns") ?: 'Never'; } catch (\Throwable $e) { $lastSync = 'Never'; } try { $accountCount = (int)Database::scalar("SELECT COUNT(*) FROM meta_ad_accounts"); } catch (\Throwable $e) { $accountCount = 0; } try { $campaignCount = (int)Database::scalar("SELECT COUNT(*) FROM marketing_campaigns"); } catch (\Throwable $e) { $campaignCount = 0; } ?> <div id="meta-collector" class="panel"> <span class="section-label">Meta Data Sync</span> <div class="detail-grid"> <div><span class="kpi-label">Connected Accounts</span><span class="kpi-value"><?= $accountCount ?> </span></div> <div><span class="kpi-label">Campaigns Tracked</span><span class="kpi-value"><?= $campaignCount ?> </span></div> <div><span class="kpi-label">Last Sync</span><span><?= UI::e($lastSync) ?> </span></div> </div> <div class="form-actions"> <a id="sync-campaigns" class="btn btn-primary" href="javascript:void(0)"><span>Sync Campaigns</span></a> <a id="sync-insights" class="btn" href="javascript:void(0)"><span>Sync Insights</span></a> <a id="sync-all" class="btn btn-warning" href="javascript:void(0)"><span>Full Sync</span></a> </div> <div id="sync-log" class="term-output" style="display:none"></div> </div> <script nonce="<?= $cspNonce ?? '' ?> "> ['sync-campaigns','sync-insights','sync-all'].forEach(function(id){ document.getElementById(id).addEventListener('click',function(){ var log=document.getElementById('sync-log');log.style.display='block';log.innerHTML='<div class="term-line">Starting sync...</div>'; var endpoint=id.replace('sync-',''); var csrf=document.querySelector('meta[name=csrf-token]').content; fetch('api/system/bridge.php?target=meta',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded','X-CSRF-Token':csrf},body:'target=meta&endpoint='+endpoint+'&_csrf='+csrf}).then(function(r){return r.json();}).then(function(d){log.innerHTML+='<div class="term-result">'+JSON.stringify(d)+'</div>';if(d.ok)log.innerHTML+='<div class="term-line">Sync complete.</div>';}); }); }); </script>