<?php defined('ABSPATH') || exit;
$_gate=\DZQCOD\Modules::addon_gate(['utm-tracking','meta-capi','tiktok-events','google-ads'],'📈 الحملات'); if($_gate){echo esc_html($_gate);return;}
global $wpdb;$wpdb->suppress_errors(true); $p=$wpdb->prefix.DZQCOD_DB;
$stats=$wpdb->get_results("SELECT utm_campaign,utm_source,utm_medium,SUM(leads) as leads,SUM(confirmed) as confirmed,SUM(delivered) as delivered,SUM(rto) as rto,SUM(revenue) as revenue,SUM(ad_spend) as spend FROM {$p}campaign_stats WHERE stat_date>=DATE_SUB(CURDATE(),INTERVAL 30 DAY) GROUP BY utm_campaign,utm_source,utm_medium ORDER BY leads DESC LIMIT 50");
$totals=$wpdb->get_row("SELECT SUM(leads) as leads,SUM(confirmed) as confirmed,SUM(delivered) as delivered,SUM(revenue) as revenue,SUM(ad_spend) as spend FROM {$p}campaign_stats WHERE stat_date>=DATE_SUB(CURDATE(),INTERVAL 30 DAY)");
?>
<div class="wrap dza-wrap" dir="rtl">
<div class="dza-header"><h1>الحملات التسويقية</h1></div>
<?php include __DIR__.'/nav.php'; ?>
<div class="dza-content">
<div class="dza-kpi-grid">
<div class="dza-kpi"><div class="v"><?php echo (int)($totals->leads??0); ?></div><div class="l">📥 Leads (30 يوم)</div></div>
<div class="dza-kpi"><div class="v"><?php echo (int)($totals->confirmed??0); ?></div><div class="l">✅ مؤكد</div></div>
<div class="dza-kpi"><div class="v"><?php echo (int)($totals->delivered??0); ?></div><div class="l">🎉 موصّل</div></div>
<div class="dza-kpi"><div class="v" style="color:#2ecc71"><?php echo number_format((float)($totals->revenue??0),0); ?> دج</div><div class="l">💰 إيرادات</div></div>
<div class="dza-kpi"><div class="v" style="color:#e74c3c"><?php echo number_format((float)($totals->spend??0),0); ?> دج</div><div class="l">📊 إنفاق إعلاني</div></div>
<div class="dza-kpi"><div class="v"><?php $rev=(float)($totals->revenue??0);$sp=(float)($totals->spend??0);echo $sp>0?round($rev/$sp,1).'x':'∞'; ?></div><div class="l">🎯 ROAS</div></div>
</div>
<h2 class="dza-section-title">📈 أداء الحملات (آخر 30 يوم)</h2>
<table class="dza-table"><thead><tr><th>الحملة</th><th>المصدر</th><th>الوسيط</th><th>Leads</th><th>مؤكد</th><th>موصّل</th><th>RTO</th><th>إيرادات</th><th>CR%</th></tr></thead><tbody>
<?php if(empty($stats)): ?><tr><td colspan="9" class="empty">لا توجد بيانات بعد. أضف UTM parameters لروابط حملاتك.</td></tr>
<?php else: foreach($stats as $s): $cr=$s->leads>0?round($s->confirmed/$s->leads*100,1):0; ?>
<tr><td class="bold"><?php echo esc_html($s->utm_campaign?:'(مباشر)'); ?></td>
<td><?php echo esc_html($s->utm_source?:'-'); ?></td><td><?php echo esc_html($s->utm_medium?:'-'); ?></td>
<td><?php echo esc_html($s->leads); ?></td><td><?php echo esc_html($s->confirmed); ?></td><td><?php echo esc_html($s->delivered); ?></td>
<td style="color:#e74c3c"><?php echo esc_html($s->rto); ?></td>
<td class="bold"><?php echo number_format((float)$s->revenue,0); ?> دج</td>
<td><span class="dza-badge" style="background:<?php echo esc_html($cr>=30?'#27ae60':($cr>=15?'#f39c12':'#e74c3c')); ?>"><?php echo esc_html($cr); ?>%</span></td></tr>
<?php endforeach; endif; ?></tbody></table>
<div class="dza-card" style="margin-top:20px"><h3>📎 كيفية تتبع الحملات</h3>
<p>أضف UTM parameters لروابطك:</p>
<code style="display:block;background:#f0f4ff;padding:10px;border-radius:8px;direction:ltr;font-size:12px">https://yourstore.com/product?utm_source=facebook&utm_medium=cpc&utm_campaign=summer_sale&utm_content=video_ad</code>
<p style="margin-top:8px">يتم تتبع: <strong>fbclid, ttclid, gclid, fbp, fbc</strong> تلقائياً مع كل طلب.</p></div>
</div></div>
</div><!--dza-main-->
</div><!--dza-layout-->
