<?php defined('ABSPATH') || exit;
$S=\DZQCOD\Core::ST; $fs=sanitize_text_field($_GET['status']??''); $search=sanitize_text_field($_GET['s']??'');
$paged=max(1,absint($_GET['paged']??1)); $pp=25;
$args=['limit'=>$pp,'page'=>$paged,'orderby'=>'date','order'=>'DESC','paginate'=>true];
if($fs)$args['status']='wc-'.$fs; else $args['status']=array_map(fn($s)=>'wc-'.$s,array_keys($S));
$r=wc_get_orders($args); $orders=$r->orders??[]; $total=$r->total??0; $pages=$r->max_num_pages??1;
?>
<div class="wrap dza-wrap" dir="rtl">
<div class="dza-header"><h1>الطلبات <span><?php printf("%d طلب",$total); ?></span></h1></div>
<?php include __DIR__.'/_layout_start.php'; ?>
<div class="dza-content">
<form method="get" class="dza-filters"><input type="hidden" name="page" value="dzqcod-orders">
<input type="text" name="s" value="<?php echo esc_attr($search); ?>" placeholder="🔍 بحث بالاسم أو الهاتف">
<select name="status"><option value="">كل الحالات</option>
<?php foreach($S as $k=>$v): ?><option value="<?php echo esc_html($k); ?>" <?php selected($fs,$k); ?>><?php echo esc_html($v['i'].' '.$v['l']); ?></option><?php endforeach; ?>
</select><button class="dza-btn primary">تصفية</button>
<a href="<?php echo esc_url(wp_nonce_url(admin_url('admin-ajax.php?action=dzqcod_export_orders'.($fs?'&status='.$fs:'')),'dzqcod_admin','nonce')); ?>" class="dza-btn success">📤 تصدير CSV</a></form>
<div class="dza-batch-bar"><select id="batch-action"><option value="">إجراء جماعي</option><option value="print">🖨️ طباعة</option><option value="ship">🚚 شحن</option>
<?php foreach(['confirmed'=>'✅ تأكيد','ready-to-ship'=>'📦 جاهز للشحن','cancelled'=>'🚫 إلغاء','fake'=>'⚠️ مزيف','delivered'=>'🟢 تم التوصيل','rto'=>'🔴 مرتجع','delete'=>'🗑️ حذف'] as $bk=>$bl): ?><option value="<?php echo esc_html($bk); ?>"><?php echo esc_html($bl); ?></option><?php endforeach; ?>
</select><button class="dza-btn sm primary" id="batch-run">تنفيذ</button><label><input type="checkbox" id="check-all"> الكل</label></div>
<table class="dza-table"><thead><tr><th style="width:30px"><input type="checkbox" id="thead-check"></th><th>#</th><th>العميل</th><th>الهاتف</th><th>الولاية</th><th>المنتجات</th><th>المبلغ</th><th>الحالة</th><th>احتيال</th><th>تاريخ</th><th>إجراءات</th></tr></thead><tbody>
<?php if(empty($orders)): ?><tr><td colspan="11" class="empty">لا توجد طلبات</td></tr>
<?php else: foreach($orders as $o): $st=str_replace('wc-','',$o->get_status()); $sc=$S[$st]??['l'=>$st,'c'=>'#999','i'=>''];
$items=[];foreach($o->get_items() as $i) $items[]=$i->get_name().'×'.$i->get_quantity();
$fraud=(int)$o->get_meta('_dzqcod_fraud_score'); ?>
<tr><td><input type="checkbox" class="order-check" value="<?php echo (int)$o->get_id(); ?>"></td>
<td><a href="<?php echo esc_url(admin_url("admin.php?page=dzqcod-orders&id=".$o->get_id())); ?>">#<?php echo (int)$o->get_id(); ?></a></td>
<td><?php echo esc_html($o->get_billing_first_name()); ?></td>
<td class="ltr"><?php echo esc_html($o->get_billing_phone()); ?></td>
<td><?php echo esc_html($o->get_shipping_state()); ?></td>
<td class="sm"><?php echo esc_html(implode(', ',$items)); ?></td>
<td class="bold"><?php echo number_format((float)$o->get_total(),0); ?> دج</td>
<td><span class="dza-badge" style="background:<?php echo esc_html($sc['c']); ?>"><?php echo esc_html($sc['i'].' '.$sc['l']); ?></span></td>
<td><?php if($fraud>=50): ?><span class="dza-badge" style="background:<?php echo esc_html($fraud>=70?'#e74c3c':'#f39c12'); ?>"><?php echo esc_html($fraud); ?>%</span><?php endif; ?></td>
<td class="sm"><?php echo esc_html($o->get_date_created()?$o->get_date_created()->format('m/d H:i'):'-'); ?></td>
<td><a href="<?php echo esc_url(admin_url("admin.php?page=dzqcod-orders&id=".$o->get_id())); ?>" class="dza-btn sm">👁</a> <button class="dza-btn sm print-receipt" data-id="<?php echo (int)$o->get_id(); ?>">🖨️</button> <button class="dza-btn sm order-delete" data-id="<?php echo (int)$o->get_id(); ?>" style="color:#ef4444" title="حذف">🗑️</button></td></tr>
<?php endforeach; endif; ?></tbody></table>
<?php if($pages>1): ?><div class="dza-pag"><?php for($i=1;$i<=min($pages,20);$i++): ?><a href="<?php echo esc_html(add_query_arg(['paged'=>$i])); ?>" class="<?php echo esc_html($i===$paged?'active':''); ?>"><?php echo esc_html($i); ?></a><?php endfor; ?></div><?php endif; ?>
</div></div>
</div><!--dza-main-->
</div><!--dza-layout-->
<?php include __DIR__."/_layout_end.php"; ?>
