<?php
require_once __DIR__ . '/../includes/config.php';
if (file_exists(__DIR__ . '/../includes/auth.php')) require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/functions.php';

$issues = infl_schema_issues();
$page = max(1, (int)($_GET['page'] ?? 1));
$data = !$issues ? infl_get_influencers($_GET, $page, 20) : ['rows'=>[], 'filters'=>['q'=>'','status'=>''], 'pages'=>1, 'page'=>1];
$rows = $data['rows']; $filters = $data['filters']; $pages = $data['pages'];
include __DIR__ . '/../includes/header.php'; if (file_exists(__DIR__ . '/../includes/sidebar.php')) include __DIR__ . '/../includes/sidebar.php';
?>
<div class="content-wrapper" style="padding:20px;">
  <div style="display:flex;justify-content:space-between;align-items:center;gap:15px;flex-wrap:wrap;margin-bottom:18px;">
    <div><h1 style="margin:0 0 8px;">قائمة المؤثرين</h1><p style="margin:0;color:#6b7280;">المعلّق يظهر أولاً، والعمولة محسوبة فقط على الطلبات المسلّمة.</p></div>
    <div style="display:flex;gap:10px;flex-wrap:wrap;"><a href="create.php" style="display:inline-block;background:#16a34a;color:#fff;text-decoration:none;padding:10px 16px;border-radius:10px;">إضافة مؤثر</a><a href="index.php" style="display:inline-block;background:#111827;color:#fff;text-decoration:none;padding:10px 16px;border-radius:10px;">رجوع</a></div>
  </div>
  <?php if (!empty($_GET['msg'])): ?><div style="margin-bottom:16px;padding:12px 14px;border-radius:12px;background:#ecfeff;color:#155e75;border:1px solid #a5f3fc;"><?php echo infl_h($_GET['msg']); ?></div><?php endif; ?>
  <?php if ($issues): ?><div style="padding:16px;border-radius:14px;background:#fff7ed;border:1px solid #fdba74;color:#9a3412;">أكمل SQL أولاً.</div><?php else: ?>
  <form method="get" style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:16px;margin-bottom:18px;display:grid;grid-template-columns:repeat(auto-fit,minmax(180px,1fr));gap:12px;">
    <div><label style="display:block;margin-bottom:6px;font-size:13px;">بحث</label><input type="text" name="q" value="<?php echo infl_h($filters['q']); ?>" placeholder="اسم / كود دخول / هاتف / انستغرام / بروموكود" style="width:100%;padding:10px 12px;border:1px solid #d1d5db;border-radius:10px;"></div>
    <div><label style="display:block;margin-bottom:6px;font-size:13px;">الحالة</label><select name="status" style="width:100%;padding:10px 12px;border:1px solid #d1d5db;border-radius:10px;"><option value="">الكل</option><option value="2" <?php echo $filters['status']==='2'?'selected':''; ?>>معلّق</option><option value="1" <?php echo $filters['status']==='1'?'selected':''; ?>>نشط</option><option value="0" <?php echo $filters['status']==='0'?'selected':''; ?>>معطل</option></select></div>
    <div style="display:flex;align-items:end;gap:10px;"><button type="submit" style="background:#1d4ed8;color:#fff;border:none;padding:10px 16px;border-radius:10px;cursor:pointer;">بحث</button><a href="list.php" style="display:inline-block;background:#f3f4f6;color:#111827;text-decoration:none;padding:10px 16px;border-radius:10px;">إعادة ضبط</a></div>
  </form>
  <div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;overflow:auto;"><table style="width:100%;border-collapse:collapse;min-width:1100px;"><thead style="background:#f9fafb;"><tr><th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">#</th><th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">الاسم</th><th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">الهاتف</th><th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">كود الدخول</th><th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">انستغرام</th><th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">كود الخصم</th><th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">خصم الزبون</th><th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">عمولة المؤثر</th><th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">طلبات مسلّمة</th><th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">المتبقي</th><th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">الحالة</th><th style="padding:12px;text-align:right;border-bottom:1px solid #e5e7eb;">إجراءات</th></tr></thead><tbody>
  <?php if (!$rows): ?><tr><td colspan="12" style="padding:18px;text-align:center;color:#6b7280;">لا يوجد مؤثرون حالياً.</td></tr><?php else: foreach ($rows as $row): ?>
    <tr>
      <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo (int)$row['id']; ?></td>
      <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo infl_h($row['name']); ?></td>
      <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo infl_h($row['phone']); ?></td>
      <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo infl_h($row['login_code']); ?></td>
      <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo infl_h($row['instagram_username'] ? '@'.$row['instagram_username'] : '-'); ?></td>
      <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo infl_h($row['promo_code'] ?: '-'); ?></td>
      <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo infl_h(($row['discount_type']==='percent' ? rtrim(rtrim((string)$row['discount_value'],'0'),'.').'%' : infl_money($row['discount_value']))); ?></td>
      <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo infl_h(($row['influencer_commission_type']==='percent' ? rtrim(rtrim((string)$row['influencer_commission_value'],'0'),'.').'%' : infl_money($row['influencer_commission_value']))); ?></td>
      <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo number_format((int)$row['delivered_orders']); ?></td>
      <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo infl_money($row['remaining_commission']); ?></td>
      <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><?php echo infl_status_badge($row['status']); ?></td>
      <td style="padding:12px;border-bottom:1px solid #f1f5f9;"><div style="display:flex;gap:8px;flex-wrap:wrap;"><a href="view.php?id=<?php echo (int)$row['id']; ?>">عرض</a><a href="edit.php?id=<?php echo (int)$row['id']; ?>">تعديل</a><a href="payments.php?id=<?php echo (int)$row['id']; ?>">دفعات</a><?php if ((int)$row['status'] !== 1): ?><?= admin_post_action_form('set_status.php', 'تفعيل', '', 'influencer_status', ['id' => (int)$row['id'], 'status' => 1], 'تفعيل هذا المؤثر؟'); ?><?php endif; ?><?php if ((int)$row['status'] !== 2): ?><?= admin_post_action_form('set_status.php', 'تعليق', '', 'influencer_status', ['id' => (int)$row['id'], 'status' => 2], 'إرجاعه إلى معلّق؟'); ?><?php endif; ?><?php if ((int)$row['status'] !== 0): ?><?= admin_post_action_form('set_status.php', 'تعطيل', '', 'influencer_status', ['id' => (int)$row['id'], 'status' => 0], 'تعطيل هذا المؤثر؟'); ?><?php endif; ?></div></td>
    </tr>
  <?php endforeach; endif; ?>
  </tbody></table></div>
  <?php endif; ?>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
