<?php defined('ABSPATH') || exit;
$plan = \DZQCOD\Core::opt('license_plan','free');
if($plan==='free'){ include __DIR__.'/nav.php'; echo '<div class="dza-content" style="text-align:center;padding:60px"><div style="font-size:64px;margin-bottom:16px">🏭</div><h2>لوحة الورشة — PRO</h2></div>'; return; } // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- safe static HTML

// Check if viewing single order
$view_id = absint($_GET['ws_id'] ?? 0);
if($view_id){ include(__DIR__.'/workshop-detail.php'); return; }

$statuses = ['wc-confirmed','wc-processing','wc-workshop-received','wc-workshop-progress','wc-workshop-done','wc-ready-ship'];
$orders = wc_get_orders(['status'=>$statuses,'limit'=>50,'orderby'=>'date','order'=>'DESC']);
$workshop_orders = [];
foreach($orders as $o){
    $has_custom = false;
    foreach($o->get_items() as $item){
        if(get_post_meta($item->get_product_id(),'_dzqcod_custom_enabled',true)==='yes'){$has_custom=true;break;}
    }
    if($has_custom) $workshop_orders[] = $o;
}
$ws_statuses = [
    'confirmed'=>['l'=>'🆕 جديد','c'=>'#6366f1'],
    'workshop-received'=>['l'=>'📥 مستلم','c'=>'#f97316'],
    'workshop-progress'=>['l'=>'⚙️ قيد الإنجاز','c'=>'#eab308'],
    'workshop-done'=>['l'=>'✅ منجز','c'=>'#22c55e'],
    'ready-ship'=>['l'=>'📦 جاهز','c'=>'#8b5cf6'],
    'processing'=>['l'=>'🔄 معالجة','c'=>'#64748b'],
];
$filter_st = sanitize_key($_GET['ws_st'] ?? 'all');
?>
<div class="wrap dza-wrap" dir="rtl">
<?php include __DIR__.'/nav.php'; ?>
<div class="dza-header"><h1>🏭 لوحة الورشة</h1><span style="font-size:13px;color:#94a3b8"><?php echo (int)count($workshop_orders); ?> طلب مخصص</span></div>
<div class="dza-content">

<div style="display:flex;gap:6px;margin-bottom:16px;flex-wrap:wrap">
<a href="<?php echo esc_url(admin_url('admin.php?page=dzqcod-workshop')); ?>" class="dza-btn <?php echo esc_html($filter_st==='all'?'primary':'ghost'); ?>" style="font-size:12px">الكل (<?php echo count($workshop_orders); ?>)</a>
<?php $counts=[]; foreach($workshop_orders as $wo){$wst=str_replace('wc-','',$wo->get_status());$counts[$wst]=($counts[$wst]??0)+1;}
foreach($ws_statuses as $wk=>$wv): if(!isset($counts[$wk])) continue; ?>
<a href="<?php echo esc_url(admin_url('admin.php?page=dzqcod-workshop&ws_st='.$wk)); ?>" class="dza-btn <?php echo esc_html($filter_st===$wk?'primary':'ghost'); ?>" style="font-size:12px;border-color:<?php echo esc_html($wv['c']); ?>"><?php echo esc_html($wv['l'].' ('.$counts[$wk].')'); ?></a>
<?php endforeach; ?>
</div>

<?php if(empty($workshop_orders)): ?>
<div class="dza-card" style="text-align:center;padding:48px"><div style="font-size:56px;margin-bottom:12px">🏭</div>
<h3 style="border:none;color:#475569">لا توجد طلبات مخصصة حالياً</h3></div>
<?php else: ?>

<div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(340px,1fr));gap:12px">
<?php foreach($workshop_orders as $wo):
$wst=str_replace('wc-','',$wo->get_status());
if($filter_st!=='all' && $wst!==$filter_st) continue;
$wsv=$ws_statuses[$wst]??['l'=>$wst,'c'=>'#64748b'];
$wname=trim($wo->get_billing_first_name().' '.$wo->get_billing_last_name());
$wphone=$wo->get_billing_phone();
$wdate=$wo->get_date_created()?$wo->get_date_created()->format('m/d H:i'):'-';
$witems=[];foreach($wo->get_items() as $wi) $witems[]=$wi->get_name().' ×'.$wi->get_quantity();
$has_files=!!$wo->get_meta('_dzqcod_uploaded_files');
$has_dims=!!$wo->get_meta('_dzqcod_custom_dimensions');
$has_note=!!$wo->get_meta('_dzqcod_custom_order_note');
?>
<a href="<?php echo esc_url(admin_url('admin.php?page=dzqcod-workshop&ws_id='.$wo->get_id())); ?>" class="dza-card" style="text-decoration:none;color:inherit;cursor:pointer;border-right:4px solid <?php echo esc_html($wsv['c']); ?>;transition:.2s;display:block" onmouseenter="this.style.transform='scale(1.01)'" onmouseleave="this.style.transform='none'">
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:8px">
<span style="font-weight:900;font-size:15px">#<?php echo (int)$wo->get_id(); ?></span>
<span style="background:<?php echo esc_html($wsv['c']); ?>;color:#fff;padding:2px 10px;border-radius:6px;font-size:11px;font-weight:700"><?php echo esc_html($wsv['l']); ?></span>
</div>
<div style="font-size:13px;font-weight:600;margin-bottom:4px"><?php echo esc_html($wname); ?> — <?php echo esc_html($wphone); ?></div>
<div style="font-size:12px;color:#64748b;margin-bottom:6px"><?php echo esc_html(implode(' | ',$witems)); ?></div>
<div style="display:flex;gap:4px;flex-wrap:wrap">
<?php if($has_dims): ?><span style="background:#e0e7ff;color:#4338ca;padding:2px 8px;border-radius:4px;font-size:10px;font-weight:600">📐 أبعاد</span><?php endif; ?>
<?php if($has_files): ?><span style="background:#dcfce7;color:#166534;padding:2px 8px;border-radius:4px;font-size:10px;font-weight:600">📎 ملفات</span><?php endif; ?>
<?php if($has_note): ?><span style="background:#fef3c7;color:#92400e;padding:2px 8px;border-radius:4px;font-size:10px;font-weight:600">📝 ملاحظة</span><?php endif; ?>
<span style="color:#94a3b8;font-size:10px;margin-right:auto"><?php echo esc_html($wdate); ?></span>
</div>
<div style="font-weight:900;font-size:16px;color:#059669;margin-top:8px"><?php echo number_format((float)$wo->get_total(),0); ?> دج</div>
</a>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div></div></div></div>
