<?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';

$id = (int)($_GET['id'] ?? 0);
$issues = infl_schema_issues();
$record = !$issues ? infl_find_influencer($id) : null;
if (!$record && !$issues) die('المؤثر غير موجود');
$stats = !$issues ? infl_get_order_stats_for_one($id) : ['total_orders'=>0,'delivered_orders'=>0,'earned_commission'=>0,'paid_commission'=>0,'remaining_commission'=>0];
$orders = !$issues ? infl_get_recent_orders($id, 25) : [];
$payments = !$issues ? infl_get_payments($id, 10) : [];
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;"><?php echo $record ? infl_h($record['name']) : ''; ?></p></div><div style="display:flex;gap:10px;flex-wrap:wrap;"><a href="edit.php?id=<?php echo $id; ?>" style="display:inline-block;background:#1d4ed8;color:#fff;text-decoration:none;padding:10px 16px;border-radius:10px;">تعديل</a><a href="payments.php?id=<?php echo $id; ?>" style="display:inline-block;background:#16a34a;color:#fff;text-decoration:none;padding:10px 16px;border-radius:10px;">دفعات</a><a href="list.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 && $record): ?>
  <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:16px;margin-bottom:18px;"><div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;"><div style="color:#6b7280;font-size:13px;">الطلبات المسلّمة فقط</div><div style="font-size:28px;font-weight:700;margin-top:8px;"><?php echo number_format($stats['delivered_orders']); ?></div></div><div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;"><div style="color:#6b7280;font-size:13px;">إجمالي الطلبات</div><div style="font-size:28px;font-weight:700;margin-top:8px;"><?php echo number_format($stats['total_orders']); ?></div></div><div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;"><div style="color:#6b7280;font-size:13px;">المستحق</div><div style="font-size:28px;font-weight:700;margin-top:8px;"><?php echo infl_money($stats['earned_commission']); ?></div></div><div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;"><div style="color:#6b7280;font-size:13px;">المدفوع</div><div style="font-size:28px;font-weight:700;margin-top:8px;"><?php echo infl_money($stats['paid_commission']); ?></div></div><div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;"><div style="color:#6b7280;font-size:13px;">المتبقي</div><div style="font-size:28px;font-weight:700;margin-top:8px;"><?php echo infl_money($stats['remaining_commission']); ?></div></div></div>
  <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(340px,1fr));gap:18px;">
    <div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;">
      <h3 style="margin-top:0;">بيانات المؤثر</h3>
      <div style="display:grid;gap:10px;line-height:1.8;"><div><strong>الاسم:</strong> <?php echo infl_h($record['name']); ?></div><div><strong>الهاتف:</strong> <?php echo infl_h($record['phone']); ?></div><div><strong>معرف الانستغرام:</strong> <?php echo infl_h($record['instagram_username'] ? '@'.$record['instagram_username'] : '-'); ?></div><div><strong>كود الدخول:</strong> <?php echo infl_h($record['login_code']); ?></div><div><strong>الحالة:</strong> <?php echo infl_status_badge($record['status']); ?></div><div><strong>ملاحظات:</strong> <?php echo nl2br(infl_h($record['notes'] ?: '-')); ?></div></div>
    </div>
    <div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;">
      <h3 style="margin-top:0;">اتفاق الخصومات والعمولة</h3>
      <div style="display:grid;gap:10px;line-height:1.8;"><div><strong>كود الخصم:</strong> <?php echo infl_h($record['promo_code'] ?: '-'); ?></div><div><strong>عنوان الكود:</strong> <?php echo infl_h($record['promo_title'] ?: '-'); ?></div><div><strong>خصم الزبون:</strong> <?php echo infl_h($record['discount_type']==='percent' ? rtrim(rtrim((string)$record['discount_value'],'0'),'.').'%' : infl_money($record['discount_value'])); ?></div><div><strong>عمولة المؤثر:</strong> <?php echo infl_h($record['influencer_commission_type']==='percent' ? rtrim(rtrim((string)$record['influencer_commission_value'],'0'),'.').'%' : infl_money($record['influencer_commission_value'])); ?></div><div><strong>حد أدنى للطلب:</strong> <?php echo infl_money($record['minimum_order_amount']); ?></div><div><strong>الحالة:</strong> <?php echo (int)$record['promo_status']===1 ? 'فعال' : 'غير فعال'; ?></div></div>
    </div>
  </div>
  <div style="display:grid;grid-template-columns:1.2fr .8fr;gap:18px;margin-top:18px;">
    <div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;overflow:auto;"><h3 style="margin-top:0;">الطلبات المرتبطة</h3><table style="width:100%;border-collapse:collapse;min-width:760px;"><thead style="background:#f9fafb;"><tr><th style="padding:10px;text-align:right;border-bottom:1px solid #e5e7eb;">الطلب</th><th style="padding:10px;text-align:right;border-bottom:1px solid #e5e7eb;">الزبون</th><th style="padding:10px;text-align:right;border-bottom:1px solid #e5e7eb;">الحالة</th><th style="padding:10px;text-align:right;border-bottom:1px solid #e5e7eb;">الإجمالي</th><th style="padding:10px;text-align:right;border-bottom:1px solid #e5e7eb;">العمولة المحتسبة</th></tr></thead><tbody><?php if (!$orders): ?><tr><td colspan="5" style="padding:12px;text-align:center;color:#6b7280;">لا توجد طلبات.</td></tr><?php else: foreach ($orders as $order): ?><tr><td style="padding:10px;border-bottom:1px solid #f1f5f9;">#<?php echo infl_h($order['order_number']); ?></td><td style="padding:10px;border-bottom:1px solid #f1f5f9;"><?php echo infl_h($order['customer_name']); ?></td><td style="padding:10px;border-bottom:1px solid #f1f5f9;"><?php echo infl_h($order['order_status']); ?></td><td style="padding:10px;border-bottom:1px solid #f1f5f9;"><?php echo infl_money($order['total_amount']); ?></td><td style="padding:10px;border-bottom:1px solid #f1f5f9;"><?php echo infl_money($order['commission_amount']); ?></td></tr><?php endforeach; endif; ?></tbody></table></div>
    <div style="background:#fff;border:1px solid #e5e7eb;border-radius:16px;padding:18px;overflow:auto;"><h3 style="margin-top:0;">آخر الدفعات</h3><table style="width:100%;border-collapse:collapse;min-width:420px;"><thead style="background:#f9fafb;"><tr><th style="padding:10px;text-align:right;border-bottom:1px solid #e5e7eb;">التاريخ</th><th style="padding:10px;text-align:right;border-bottom:1px solid #e5e7eb;">المبلغ</th><th style="padding:10px;text-align:right;border-bottom:1px solid #e5e7eb;">ملاحظة</th></tr></thead><tbody><?php if (!$payments): ?><tr><td colspan="3" style="padding:12px;text-align:center;color:#6b7280;">لا توجد دفعات.</td></tr><?php else: foreach ($payments as $p): ?><tr><td style="padding:10px;border-bottom:1px solid #f1f5f9;"><?php echo infl_h($p['pay_date']); ?></td><td style="padding:10px;border-bottom:1px solid #f1f5f9;"><?php echo infl_money($p['amount']); ?></td><td style="padding:10px;border-bottom:1px solid #f1f5f9;"><?php echo infl_h($p['note'] ?: '-'); ?></td></tr><?php endforeach; endif; ?></tbody></table></div>
  </div>
  <?php endif; ?>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
