<?php defined('ABSPATH') || exit;
$_gate=\DZQCOD\Modules::addon_gate(['fraud-detect','ip-geo'],'🛡️ مكافحة الاحتيال'); if($_gate){echo esc_html($_gate);return;}
global $wpdb;$wpdb->suppress_errors(true); $p=$wpdb->prefix.DZQCOD_DB;
$scores=$wpdb->get_results("SELECT f.*,o.date_created_gmt FROM {$p}fraud_scores f LEFT JOIN {$wpdb->prefix}wc_orders o ON f.order_id=o.id ORDER BY f.id DESC LIMIT 50");
$bl=$wpdb->get_results("SELECT * FROM {$p}blacklist ORDER BY id DESC LIMIT 30");
$threshold=\DZQCOD\Core::opt('fraud_threshold','70'); ?>
<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" style="color:#e74c3c"><?php echo esc_html($wpdb->get_var("SELECT COUNT(*) FROM {$p}fraud_scores WHERE score>=50")); ?></div><div class="l">🛡️ طلبات مشبوهة</div></div>
<div class="dza-kpi"><div class="v"><?php echo esc_html($wpdb->get_var("SELECT COUNT(*) FROM {$p}blacklist")); ?></div><div class="l">🚫 قائمة سوداء</div></div>
<div class="dza-kpi"><div class="v"><?php echo esc_html($threshold); ?>%</div><div class="l">⚙️ حد الحظر</div></div>
</div>
<div class="dza-card" style="margin-bottom:20px">
<h3>➕ إضافة للقائمة السوداء</h3>
<div style="display:flex;flex-wrap:wrap;gap:8px;align-items:end">
<select id="bl-type" style="padding:8px;border:1px solid #ddd;border-radius:6px"><option value="phone">هاتف</option><option value="ip">IP</option></select>
<input type="text" id="bl-value" placeholder="0555123456" style="padding:8px;border:1px solid #ddd;border-radius:6px">
<input type="text" id="bl-reason" placeholder="السبب..." style="padding:8px;border:1px solid #ddd;border-radius:6px;flex:1">
<button class="dza-btn danger" id="add-blacklist">🚫 حظر</button></div></div>
<h2 class="dza-section-title">🛡️ سجل الاحتيال</h2>
<table class="dza-table"><thead><tr><th>الطلب</th><th>النتيجة</th><th>الإشارات</th><th>الإجراء</th><th>التاريخ</th></tr></thead><tbody>
<?php if(empty($scores)): ?><tr><td colspan="5" class="empty">لا توجد تنبيهات</td></tr>
<?php else: foreach($scores as $fs): $sigs=json_decode($fs->signals_json?:'[]',true); ?>
<tr><td><a href="<?php echo esc_url(admin_url('post.php?post='.$fs->order_id.'&action=edit')); ?>">#<?php echo esc_html($fs->order_id); ?></a></td>
<td><span class="dza-badge" style="background:<?php echo esc_html($fs->score>=70?'#e74c3c':($fs->score>=40?'#f39c12':'#27ae60')); ?>"><?php echo esc_html($fs->score); ?>%</span></td>
<td class="sm"><?php echo esc_html(implode(', ',$sigs)); ?></td>
<td><?php echo esc_html($fs->action_taken); ?></td>
<td class="sm"><?php echo esc_html($fs->created_at); ?></td></tr>
<?php endforeach; endif; ?></tbody></table>
<h2 class="dza-section-title" style="margin-top:20px">🚫 القائمة السوداء</h2>
<table class="dza-table"><thead><tr><th>النوع</th><th>القيمة (مشفرة)</th><th>السبب</th><th>الخطورة</th><th>التاريخ</th></tr></thead><tbody>
<?php foreach($bl as $b): ?>
<tr><td><?php echo esc_html($b->type); ?></td><td class="sm ltr"><?php echo esc_html(substr($b->hash_value,0,16).'...'); ?></td>
<td><?php echo esc_html($b->reason); ?></td>
<td><span class="dza-badge" style="background:<?php echo esc_html($b->severity==='permanent'||$b->severity==='high'?'#e74c3c':'#f39c12'); ?>"><?php echo esc_html($b->severity); ?></span></td>
<td class="sm"><?php echo esc_html($b->created_at); ?></td></tr>
<?php endforeach; ?></tbody></table></div></div>
</div><!--dza-main-->
</div><!--dza-layout-->
