<?php
include '_head.php';
if(isset($_GET['status']) && isset($_GET['id'])){
  $st=mysqli_real_escape_string($conn,$_GET['status']);
  mysqli_query($conn,"UPDATE orders SET status='$st' WHERE id=".(int)$_GET['id']);
}
if(isset($_GET['view'])){
  $id=(int)$_GET['view'];
  $oq=mysqli_query($conn,"SELECT * FROM orders WHERE id=$id LIMIT 1");
  $o=$oq && mysqli_num_rows($oq)?mysqli_fetch_assoc($oq):null;
  if($o):
    $items=mysqli_query($conn,"SELECT * FROM order_items WHERE order_id=$id");
    $addressText = 'طلب رقم '.$id.'%0Aالزبون: '.rawurlencode($o['customer_name']).'%0Aالهاتف: '.rawurlencode($o['phone']).'%0Aالمحافظة: '.rawurlencode(isset($o['province'])?$o['province']:$o['city']).'%0Aالعنوان: '.rawurlencode($o['address']).'%0Aالخريطة: '.rawurlencode(isset($o['map_link'])?$o['map_link']:'');
?>
<h1>تفاصيل الطلب #<?php echo $id; ?></h1>
<div class="order-detail-grid">
  <div class="admin-card"><h2>معلومات الزبون</h2><p><b>الاسم:</b> <?php echo e($o['customer_name']); ?></p><p><b>الهاتف:</b> <?php echo e($o['phone']); ?></p><p><b>المحافظة:</b> <?php echo e(isset($o['province'])?$o['province']:$o['city']); ?></p><p><b>العنوان:</b> <span id="copyAddress"><?php echo e($o['address']); ?></span></p><?php if(isset($o['map_link']) && $o['map_link']): ?><p><b>الخريطة:</b> <a target="_blank" href="<?php echo e($o['map_link']); ?>">فتح الموقع</a></p><?php endif; ?><button class="primary-btn" onclick="navigator.clipboard.writeText(document.getElementById('copyAddress').innerText);alert('تم نسخ العنوان')">نسخ العنوان</button> <a class="primary-btn whats" target="_blank" href="https://wa.me/?text=<?php echo $addressText; ?>">إرسال للمندوب واتساب</a></div>
  <div class="admin-card"><h2>المبالغ</h2><p>مجموع المنتجات: <?php echo money_iqd(isset($o['subtotal'])?$o['subtotal']:($o['total']-$o['delivery_fee'])); ?></p><p>التوصيل: <?php echo money_iqd(isset($o['delivery_fee'])?$o['delivery_fee']:0); ?></p><h3>الكلي: <?php echo money_iqd($o['total']); ?></h3><p>الحالة: <?php echo e($o['status']); ?></p><p><a href="?id=<?php echo $id; ?>&status=processing">قيد التجهيز</a> | <a href="?id=<?php echo $id; ?>&status=delivered">تم التسليم</a> | <a href="?id=<?php echo $id; ?>&status=cancelled">ملغي</a></p></div>
</div>
<div class="admin-card"><h2>المنتجات</h2><table class="table"><tr><th>المنتج</th><th>العدد</th><th>السعر</th><th>المجموع</th></tr><?php if($items): while($it=mysqli_fetch_assoc($items)): ?><tr><td><?php echo e($it['product_name']); ?></td><td><?php echo (int)$it['qty']; ?></td><td><?php echo money_iqd($it['price']); ?></td><td><?php echo money_iqd($it['price']*$it['qty']); ?></td></tr><?php endwhile; endif; ?></table></div>
<a href="orders.php">رجوع</a>
<?php endif; include '_foot.php'; exit; }
$rs=mysqli_query($conn,"SELECT * FROM orders ORDER BY id DESC");
?>
<h1>إدارة الطلبات</h1>
<table class="table"><tr><th>#</th><th>الزبون</th><th>الهاتف</th><th>المحافظة</th><th>المجموع</th><th>الحالة</th><th>التاريخ</th><th>إجراءات</th></tr><?php while($o=mysqli_fetch_assoc($rs)): ?><tr><td><?php echo (int)$o['id']; ?></td><td><?php echo e($o['customer_name']); ?></td><td><?php echo e($o['phone']); ?></td><td><?php echo e(isset($o['province'])?$o['province']:$o['city']); ?></td><td><?php echo money_iqd($o['total']); ?></td><td><?php echo e($o['status']); ?></td><td><?php echo e($o['created_at']); ?></td><td><a href="?view=<?php echo (int)$o['id']; ?>">التفاصيل</a> | <a href="?id=<?php echo (int)$o['id']; ?>&status=processing">تجهيز</a> | <a href="?id=<?php echo (int)$o['id']; ?>&status=delivered">تسليم</a></td></tr><?php endwhile; ?></table>
<?php include '_foot.php'; ?>
