<?php defined('ABSPATH') || exit;
// This is included from workshop.php when ws_id is set
$oid = absint($_GET['ws_id'] ?? 0);
$o = $oid ? wc_get_order($oid) : null;
if(!$o){ echo '<div class="dza-card" style="text-align:center;padding:40px"><h2>الطلب غير موجود</h2></div>'; return; } // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- safe static HTML
$st = str_replace('wc-','',$o->get_status());
$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'],
];
$wsv = $ws_statuses[$st] ?? ['l'=>$st,'c'=>'#64748b'];
$name = trim($o->get_billing_first_name().' '.$o->get_billing_last_name());
$phone = $o->get_billing_phone();
$dims_json = $o->get_meta('_dzqcod_custom_dimensions');
$opts_json = $o->get_meta('_dzqcod_custom_options');
$uploaded = $o->get_meta('_dzqcod_uploaded_files');
$custom_note = $o->get_meta('_dzqcod_custom_order_note');
$wa = preg_replace('/^0/','213',$phone);
?>
<div class="wrap dza-wrap" dir="rtl">
<?php include __DIR__.'/nav.php'; ?>
<div class="dza-header">
<h1>🏭 طلب ورشة #<?php echo esc_html($oid); ?></h1>
<div style="display:flex;gap:8px;align-items:center">
<span style="background:<?php echo esc_html($wsv['c']); ?>;color:#fff;padding:4px 14px;border-radius:8px;font-size:13px;font-weight:800"><?php echo esc_html($wsv['l']); ?></span>
<a href="<?php echo esc_url(admin_url('admin.php?page=dzqcod-workshop')); ?>" class="dza-btn ghost">↩️ العودة للورشة</a>
</div></div>
<div class="dza-content">
<div style="display:grid;grid-template-columns:1fr 320px;gap:16px;align-items:start">
<!-- LEFT: Work Details -->
<div>

<!-- Products to manufacture -->
<div class="dza-card">
<h3 style="font-size:16px;border:none;margin:0 0 12px">📦 المنتجات المطلوبة</h3>
<?php foreach($o->get_items() as $item):
$pid=$item->get_product_id(); $is_custom=get_post_meta($pid,'_dzqcod_custom_enabled',true)==='yes'; ?>
<div style="padding:12px;background:<?php echo esc_html($is_custom?'#fefce8':'#f8fafc'); ?>;border-radius:10px;margin-bottom:8px;border:1px solid <?php echo esc_html($is_custom?'#fde047':'#e2e8f0'); ?>">
<div style="display:flex;justify-content:space-between;align-items:center">
<strong style="font-size:14px"><?php echo esc_html($item->get_name()); ?></strong>
<span style="background:#111827;color:#fff;padding:2px 10px;border-radius:6px;font-size:13px;font-weight:900">×<?php echo esc_html($item->get_quantity()); ?></span>
</div>
<?php if($is_custom): ?><span style="background:#f97316;color:#fff;padding:2px 8px;border-radius:4px;font-size:10px;font-weight:700;margin-top:4px;display:inline-block">🏭 منتج مخصص</span><?php endif; ?>
</div>
<?php endforeach; ?>
</div>

<!-- Dimensions — BIG and clear -->
<?php if($dims_json): $dims=json_decode($dims_json,true); if(is_array($dims) && !empty($dims)): ?>
<div class="dza-card" style="border:2px solid #667eea">
<h3 style="font-size:16px;border:none;margin:0 0 12px;color:#667eea">📐 الأبعاد المطلوبة</h3>
<div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(120px,1fr));gap:10px">
<?php foreach($dims as $dk=>$dv): ?>
<div style="background:linear-gradient(135deg,#eef2ff,#e0e7ff);border-radius:12px;padding:16px;text-align:center">
<div style="font-size:11px;color:#6366f1;font-weight:600;margin-bottom:4px"><?php echo esc_html(str_replace(['dim_','_'],' ',$dk)); ?></div>
<div style="font-size:28px;font-weight:900;color:#4338ca"><?php echo esc_html($dv); ?></div>
</div>
<?php endforeach; ?>
</div></div>
<?php endif; endif; ?>

<!-- Options -->
<?php if($opts_json): $opts=json_decode($opts_json,true); if(is_array($opts)): $active_opts=array_filter($opts,function($v){return $v!=='no'&&$v!=='';}); if(!empty($active_opts)): ?>
<div class="dza-card" style="border:2px solid #f97316">
<h3 style="font-size:16px;border:none;margin:0 0 12px;color:#f97316">✨ الخيارات المطلوبة</h3>
<div style="display:flex;flex-wrap:wrap;gap:8px">
<?php foreach($active_opts as $ok=>$ov): ?>
<div style="background:linear-gradient(135deg,#fff7ed,#fef3c7);border:1px solid #fed7aa;border-radius:10px;padding:12px 16px;font-size:14px;font-weight:700;color:#9a3412">
<?php echo esc_html(str_replace(['opt_','_'],' ',$ok)); ?>: <strong><?php echo esc_html($ov); ?></strong>
</div>
<?php endforeach; ?>
</div></div>
<?php endif; endif; endif; ?>

<!-- Files from customer — PROMINENT -->
<?php if($uploaded): $files=array_filter(array_map('trim',explode(',',$uploaded))); if(!empty($files)): ?>
<div class="dza-card" style="border:2px solid #22c55e">
<h3 style="font-size:16px;border:none;margin:0 0 12px;color:#22c55e">📎 ملفات العميل (<?php echo (int)count($files); ?> ملف)</h3>
<div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(200px,1fr));gap:10px">
<?php foreach($files as $fu): $ext=strtolower(pathinfo($fu,PATHINFO_EXTENSION)); $is_img=in_array($ext,['jpg','jpeg','png','gif','webp']); ?>
<a href="<?php echo esc_url($fu); ?>" target="_blank" style="display:block;text-decoration:none;border:1px solid #bbf7d0;border-radius:10px;overflow:hidden;transition:.2s" onmouseenter="this.style.transform='scale(1.02)'" onmouseleave="this.style.transform='none'">
<?php if($is_img): ?>
<div style="height:140px;background:url('<?php echo esc_url($fu); ?>') center/cover;border-bottom:1px solid #bbf7d0"></div>
<?php else: ?>
<div style="height:80px;display:flex;align-items:center;justify-content:center;font-size:40px;background:#f0fdf4">📄</div>
<?php endif; ?>
<div style="padding:8px;font-size:12px;color:#166534;font-weight:600;text-align:center"><?php echo esc_html(basename($fu)); ?> ⬇️</div>
</a>
<?php endforeach; ?>
</div></div>
<?php endif; endif; ?>

<!-- Workshop Note -->
<?php if($custom_note): ?>
<div class="dza-card" style="border:2px solid #eab308">
<h3 style="font-size:16px;border:none;margin:0 0 8px;color:#eab308">📝 ملاحظة العميل للورشة</h3>
<div style="font-size:15px;line-height:1.8;padding:12px;background:#fefce8;border-radius:8px"><?php echo esc_html(nl2br(esc_html($custom_note))); ?></div>
</div>
<?php endif; ?>

</div><!-- end left -->

<!-- RIGHT: Actions -->
<div>

<div class="dza-card"><h3 style="font-size:15px;border:none;margin:0 0 10px">🔄 تحديث الحالة</h3>
<div style="display:flex;flex-direction:column;gap:6px">
<?php foreach($ws_statuses as $wk=>$wv): ?>
<button class="dza-btn <?php echo esc_html($wk===$st?'primary':'ghost'); ?>" style="width:100%;font-size:12px;text-align:right;border-color:<?php echo esc_html($wv['c']); ?>" onclick="if(confirm('تغيير الحالة؟'))jQuery.post(dza.ajax,{action:'dzqcod_update_order_status',nonce:dza.nonce,order_id:<?php echo esc_html($oid); ?>,status:'<?php echo esc_html($wk); ?>'},function(){location.reload()})" <?php echo esc_html($wk===$st?'disabled':''); ?>><?php echo esc_html($wv['l']); ?></button>
<?php endforeach; ?>
</div></div>

<div class="dza-card"><h3 style="font-size:15px;border:none;margin:0 0 10px">👤 العميل</h3>
<div style="font-weight:700;margin-bottom:6px"><?php echo esc_html($name); ?></div>
<a href="tel:<?php echo esc_attr($phone); ?>" style="display:block;padding:8px;background:#f0fdf4;border-radius:8px;text-decoration:none;color:#166534;font-size:13px;margin-bottom:4px">📱 <?php echo esc_html($phone); ?></a>
<a href="https://wa.me/<?php echo esc_html($wa); ?>" target="_blank" style="display:block;padding:8px;background:#dcfce7;border-radius:8px;text-decoration:none;color:#166534;font-size:13px">💬 واتساب</a>
</div>

<div class="dza-card"><h3 style="font-size:15px;border:none;margin:0 0 10px">💰 ملخص</h3>
<div style="font-size:24px;font-weight:900;color:#059669;text-align:center"><?php echo number_format((float)$o->get_total(),0); ?> دج</div>
<div style="font-size:11px;color:#94a3b8;text-align:center;margin-top:4px"><?php echo esc_html($o->get_date_created()?$o->get_date_created()->format('Y-m-d H:i'):'-'); ?></div>
</div>

<a href="<?php echo esc_url(admin_url('admin.php?page=dzqcod-orders&id='.$oid)); ?>" class="dza-btn ghost" style="width:100%;text-align:center;font-size:12px">📋 تفاصيل الطلب الكاملة</a>
</div>
</div></div></div></div>
