<?php
error_reporting(0);
ini_set('display_errors', 0);
    include "session.php";


header("Cache-Control: no-cache, must-revalidate");

require_once __DIR__ . '/../db.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['toggle_featured'])) {
    $product_id = intval($_POST['product_id']);
    $pdo->prepare("UPDATE products SET is_featured = NOT is_featured WHERE id = ?")->execute([$product_id]);
    $_SESSION['message'] = "تم تحديث حالة التميز بنجاح";
    header("Location: index.php"); exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['toggle_active'])) {
    $product_id = intval($_POST['product_id']);
    $pdo->prepare("UPDATE products SET is_active = NOT is_active WHERE id = ?")->execute([$product_id]);
    $_SESSION['message'] = "تم تحديث حالة المنتج بنجاح";
    header("Location: index.php"); exit;
}

$search      = trim($_GET['search']      ?? '');
$filter_cat  = intval($_GET['filter_cat']  ?? 0);
$filter_stat = $_GET['filter_stat'] ?? 'all';
$sort        = $_GET['sort']        ?? 'newest';

$categories = $pdo->query("SELECT id, name FROM categories ORDER BY name")->fetchAll();

$sql = "
    SELECT p.*,
           c.name AS category_name,
           GROUP_CONCAT(DISTINCT dc.name SEPARATOR ', ') AS delivery_companies,
           pi.image_url AS image,
           COALESCE(SUM(pv.stock), 0) AS calculated_stock
    FROM products p
    LEFT JOIN categories c ON p.category_id = c.id
    LEFT JOIN product_delivery_companies pdc ON p.id = pdc.product_id
    LEFT JOIN delivery_companies dc ON pdc.delivery_company_id = dc.id
    LEFT JOIN product_images pi ON p.id = pi.product_id AND pi.is_primary = 1
    LEFT JOIN product_variants pv ON p.id = pv.product_id
    WHERE 1=1
";
$params = [];

if (!empty($search)) {
    $sql .= " AND (p.name LIKE :search OR c.name LIKE :search)";
    $params[':search'] = '%' . $search . '%';
}
if ($filter_cat > 0) {
    $sql .= " AND p.category_id = :cat";
    $params[':cat'] = $filter_cat;
}
if ($filter_stat === 'active')   $sql .= " AND p.is_active = 1";
if ($filter_stat === 'inactive') $sql .= " AND p.is_active = 0";

$sql .= " GROUP BY p.id";
$sql .= match($sort) {
    'price_asc'  => " ORDER BY p.price ASC",
    'price_desc' => " ORDER BY p.price DESC",
    'stock_asc'  => " ORDER BY calculated_stock ASC",
    default      => " ORDER BY p.created_at DESC",
};

$stmt = $pdo->prepare($sql);
foreach ($params as $k => $v) $stmt->bindValue($k, $v);
$stmt->execute();
$products = $stmt->fetchAll();

$total     = count($products);
$active    = count(array_filter($products, fn($p) => $p['is_active']));
$featured  = count(array_filter($products, fn($p) => $p['is_featured']));
$low_stock = count(array_filter($products, fn($p) => $p['calculated_stock'] <= 3 && $p['calculated_stock'] > 0));
$out_stock = count(array_filter($products, fn($p) => $p['calculated_stock'] == 0));
?>
<!DOCTYPE html>
<html dir="rtl" lang="ar">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>إدارة المنتجات</title>
<link rel="icon" href="<?php echo BASE_URL . '/' . htmlspecialchars($store['logo']); ?>" type="image/jpg">
    <link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@300;400;500;700;800;900&display=swap" rel="stylesheet">
 <?php include __DIR__ . '/pwa_head.php'; ?>
<style>
:root {
    --primary:     #1E40AF;
    --primary-l:   #3B82F6;
    --primary-d:   #1E3A8A;
    --success:     #059669;
    --success-l:   #D1FAE5;
    --danger:      #DC2626;
    --danger-l:    #FEE2E2;
    --warning:     #D97706;
    --warning-l:   #FEF3C7;
    --info:        #0284C7;
    --info-l:      #E0F2FE;
    --purple:      #7C3AED;
    --purple-l:    #EDE9FE;
    --bg:          #F1F5F9;
    --surface:     #FFFFFF;
    --surface-2:   #F8FAFC;
    --border:      #E2E8F0;
    --text:        #0F172A;
    --text-2:      #475569;
    --text-3:      #94A3B8;
    --shadow-sm:   0 1px 3px rgba(0,0,0,0.06);
    --shadow-md:   0 4px 16px rgba(0,0,0,0.08);
    --radius:      14px;
    --radius-sm:   8px;
    --tr:          all 0.22s cubic-bezier(0.4,0,0.2,1);
}
*,*::before,*::after{margin:0;padding:0;box-sizing:border-box;font-family:'Tajawal',sans-serif}
body{background:var(--bg);color:var(--text);padding:20px;min-height:100vh}
::-webkit-scrollbar{width:5px;height:5px}
::-webkit-scrollbar-track{background:#f1f5f9}
::-webkit-scrollbar-thumb{background:var(--primary-l);border-radius:10px}
.container{max-width:1400px;margin:0 auto}

.header{
    display:flex;justify-content:space-between;align-items:center;
    padding:18px 24px;background:var(--surface);border-radius:var(--radius);
    box-shadow:var(--shadow-sm);border:1px solid var(--border);
    margin-bottom:20px;animation:fadeDown 0.4s ease;
}
@keyframes fadeDown{from{opacity:0;transform:translateY(-12px)}to{opacity:1;transform:translateY(0)}}
.header h1{color:var(--primary);font-size:21px;font-weight:900;display:flex;align-items:center;gap:10px}
.icon-wrap{background:linear-gradient(135deg,var(--primary),var(--primary-l));color:white;width:38px;height:38px;border-radius:10px;display:flex;align-items:center;justify-content:center}

.msg{padding:13px 18px;border-radius:var(--radius-sm);display:flex;align-items:center;gap:10px;font-weight:600;margin-bottom:18px;animation:slideIn 0.3s ease}
@keyframes slideIn{from{opacity:0;transform:translateY(-8px)}to{opacity:1;transform:translateY(0)}}
.msg-success{background:var(--success-l);color:var(--success);border:1px solid #A7F3D0}
.msg-error  {background:var(--danger-l);color:var(--danger);border:1px solid #FECACA}

.stats-grid{
    display:grid;grid-template-columns:repeat(5,1fr);gap:14px;
    margin-bottom:20px;animation:fadeUp 0.4s ease 0.05s both;
}
@keyframes fadeUp{from{opacity:0;transform:translateY(12px)}to{opacity:1;transform:translateY(0)}}
@media(max-width:1100px){.stats-grid{grid-template-columns:repeat(3,1fr)}}
@media(max-width:600px) {.stats-grid{grid-template-columns:repeat(2,1fr)}}

.stat-card{
    background:var(--surface);border-radius:var(--radius);
    padding:16px 18px;border:1px solid var(--border);
    box-shadow:var(--shadow-sm);transition:var(--tr);
    position:relative;overflow:hidden;
}
.stat-card::after{content:'';position:absolute;bottom:0;left:0;right:0;height:3px;border-radius:0 0 var(--radius) var(--radius)}
.stat-card:hover{transform:translateY(-3px);box-shadow:var(--shadow-md)}
.s-total::after  {background:var(--primary)}
.s-active::after {background:var(--success)}
.s-feat::after   {background:var(--purple)}
.s-low::after    {background:var(--warning)}
.s-out::after    {background:var(--danger)}

.stat-icon{width:38px;height:38px;border-radius:var(--radius-sm);display:flex;align-items:center;justify-content:center;font-size:22px;margin-bottom:10px}
.s-total  .stat-icon{background:#EFF6FF;color:var(--primary)}
.s-active .stat-icon{background:var(--success-l);color:var(--success)}
.s-feat   .stat-icon{background:var(--purple-l);color:var(--purple)}
.s-low    .stat-icon{background:var(--warning-l);color:var(--warning)}
.s-out    .stat-icon{background:var(--danger-l);color:var(--danger)}

.stat-value{font-size:26px;font-weight:900;color:var(--text);line-height:1;margin-bottom:3px;letter-spacing:-0.5px}
.stat-label{font-size:11px;color:var(--text-3);font-weight:700;text-transform:uppercase;letter-spacing:0.5px}

.filters-bar{
    background:var(--surface);border-radius:var(--radius);
    padding:14px 18px;border:1px solid var(--border);
    box-shadow:var(--shadow-sm);margin-bottom:18px;
    animation:fadeUp 0.4s ease 0.1s both;
}
.filters-form{display:flex;flex-wrap:wrap;gap:10px;align-items:center}
.filter-search{display:flex;border-radius:var(--radius-sm);overflow:hidden;border:1.5px solid var(--border);transition:var(--tr);flex:1;min-width:200px}
.filter-search:focus-within{border-color:var(--primary-l);box-shadow:0 0 0 3px rgba(59,130,246,0.12)}
.filter-search input{flex:1;padding:9px 13px;border:none;outline:none;font-size:13px;color:var(--text);font-family:'Tajawal',sans-serif}
.filter-search button{padding:0 14px;background:var(--primary);color:white;border:none;cursor:pointer;font-size:13px;transition:var(--tr);display:flex;align-items:center;gap:5px}
.filter-search button:hover{background:var(--primary-d)}

.filter-select{padding:9px 13px;border:1.5px solid var(--border);border-radius:var(--radius-sm);font-size:13px;color:var(--text);font-family:'Tajawal',sans-serif;background:white;cursor:pointer;transition:var(--tr);outline:none}
.filter-select:focus{border-color:var(--primary-l);box-shadow:0 0 0 3px rgba(59,130,246,0.12)}
.filters-count{font-size:12px;color:var(--text-3);font-weight:700;margin-right:auto}

.card{
    background:var(--surface);border-radius:var(--radius);
    box-shadow:var(--shadow-sm);border:1px solid var(--border);
    overflow:hidden;animation:fadeUp 0.4s ease 0.15s both;
}
.table-wrap{overflow-x:auto}
table{width:100%;border-collapse:collapse;font-size:13px}
th{background:linear-gradient(135deg,var(--primary),var(--primary-d));color:white;padding:13px 14px;text-align:right;font-weight:700;font-size:12px;white-space:nowrap}
td{padding:12px 14px;border-bottom:1px solid #F1F5F9;color:var(--text-2);vertical-align:middle}
tr:last-child td{border-bottom:none}
tr{transition:var(--tr)}
tr:hover td{background:#F8FAFC}

.prod-img{width:52px;height:52px;object-fit:cover;border-radius:var(--radius-sm);border:1px solid var(--border)}
.prod-img-placeholder{width:52px;height:52px;background:var(--surface-2);border-radius:var(--radius-sm);border:1px solid var(--border);display:flex;align-items:center;justify-content:center;color:var(--text-3);font-size:22px}

.badge{padding:4px 10px;border-radius:20px;font-size:11px;font-weight:800;display:inline-flex;align-items:center;gap:4px;white-space:nowrap}
.b-active  {background:var(--success-l);color:var(--success);border:1px solid #A7F3D0}
.b-inactive{background:var(--danger-l);color:var(--danger);border:1px solid #FECACA}
.b-feat    {background:var(--purple-l);color:var(--purple);border:1px solid #DDD6FE}
.b-info    {background:var(--info-l);color:var(--info);border:1px solid #BAE6FD}

.stock-ok  {color:var(--success);font-weight:800}
.stock-low {color:var(--warning);font-weight:800}
.stock-out {color:var(--danger);font-weight:800;animation:pulse 1.5s infinite}
@keyframes pulse{0%,100%{opacity:1}50%{opacity:0.5}}

.delivery-tag{font-size:11px;color:var(--text-3);display:flex;align-items:center;gap:4px;margin-top:2px}

.btn{padding:8px 13px;border-radius:var(--radius-sm);font-weight:700;cursor:pointer;transition:var(--tr);text-decoration:none;display:inline-flex;align-items:center;gap:5px;font-size:12px;border:none}
.btn-primary      {background:var(--primary);color:white}
.btn-primary:hover{background:var(--primary-d);transform:translateY(-1px);box-shadow:0 4px 14px rgba(30,64,175,0.3)}
.btn-warning      {background:var(--warning);color:white}
.btn-warning:hover{background:#B45309;transform:translateY(-1px)}
.btn-danger       {background:var(--danger);color:white}
.btn-danger:hover {background:#B91C1C;transform:translateY(-1px)}
.btn-gray         {background:var(--surface-2);color:var(--text-2);border:1.5px solid var(--border)}
.btn-gray:hover   {background:var(--border)}
.btn-icon         {padding:7px 10px}
.btn-feat-on      {background:var(--purple-l);color:var(--purple);border:1px solid #DDD6FE}
.btn-feat-on:hover{background:var(--purple);color:white}
.btn-feat-off     {background:var(--surface-2);color:var(--text-3);border:1.5px solid var(--border)}
.btn-feat-off:hover{background:var(--purple-l);color:var(--purple)}

.action-btns{display:flex;gap:4px;flex-wrap:nowrap}

.empty{text-align:center;padding:50px 20px;color:var(--text-3)}
.empty .empty-icon{font-size:48px;margin-bottom:12px;display:block}
.empty p{font-size:14px}

/* SVG icons inline */
.svg-icon{display:inline-flex;align-items:center;justify-content:center;vertical-align:middle}

@media(max-width:768px){
    body{padding:12px}
    .header{flex-direction:column;align-items:flex-start;gap:12px}
    .filters-form{flex-direction:column}
    .filter-search{min-width:100%}
    .action-btns{flex-wrap:wrap}
    th,td{padding:10px 8px;font-size:11px}
}
</style>
<link rel="manifest" href="manifest.php">
<meta name="theme-color" content="#1a56db">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-title" content="Neo Link">
<script>
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('sw.js');
}
</script>
    <?php include __DIR__ . '/darkmode.php'; ?>
    </head>
<body>

<button onclick="toggleSidebar()" style="position:fixed;top:15px;right:15px;z-index:900;background:var(--primary);color:white;border:none;padding:10px 13px;border-radius:var(--radius-sm);cursor:pointer;display:flex;align-items:center" title="القائمة">
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2.5" stroke-linecap="round"><line x1="3" y1="6" x2="21" y2="6"/><line x1="3" y1="12" x2="21" y2="12"/><line x1="3" y1="18" x2="21" y2="18"/></svg>
</button>
<div class="sidebar-overlay" onclick="toggleSidebar()"></div>
<?php include 'sidebar.php'; ?>

<div class="container">

    <!-- HEADER -->
    <div class="header">
        <h1>
            <span class="icon-wrap">
                <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round"><path d="M21 16V8a2 2 0 00-1-1.73l-7-4a2 2 0 00-2 0l-7 4A2 2 0 003 8v8a2 2 0 001 1.73l7 4a2 2 0 002 0l7-4A2 2 0 0021 16z"/><polyline points="3.27 6.96 12 12.01 20.73 6.96"/><line x1="12" y1="22.08" x2="12" y2="12"/></svg>
            </span>
            إدارة المنتجات
        </h1>
        <a href="add_product.php" class="btn btn-primary">
            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
            إضافة منتج جديد
        </a>
    </div>

    <!-- MESSAGES -->
    <?php if (isset($_SESSION['message'])): ?>
        <div class="msg msg-success">✅ <?= htmlspecialchars($_SESSION['message']) ?></div>
        <?php unset($_SESSION['message']); ?>
    <?php endif; ?>
    <?php if (isset($_SESSION['error'])): ?>
        <div class="msg msg-error">❌ <?= htmlspecialchars($_SESSION['error']) ?></div>
        <?php unset($_SESSION['error']); ?>
    <?php endif; ?>

    <!-- STATS -->
    <div class="stats-grid">
        <div class="stat-card s-total">
            <div class="stat-icon">
                <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 16V8a2 2 0 00-1-1.73l-7-4a2 2 0 00-2 0l-7 4A2 2 0 003 8v8a2 2 0 001 1.73l7 4a2 2 0 002 0l7-4A2 2 0 0021 16z"/></svg>
            </div>
            <div class="stat-value"><?= $total ?></div>
            <div class="stat-label">إجمالي المنتجات</div>
        </div>
        <div class="stat-card s-active">
            <div class="stat-icon">✅</div>
            <div class="stat-value"><?= $active ?></div>
            <div class="stat-label">منتجات نشطة</div>
        </div>
        <div class="stat-card s-feat">
            <div class="stat-icon">⭐</div>
            <div class="stat-value"><?= $featured ?></div>
            <div class="stat-label">منتجات مميزة</div>
        </div>
        <div class="stat-card s-low">
            <div class="stat-icon">⚠️</div>
            <div class="stat-value"><?= $low_stock ?></div>
            <div class="stat-label">مخزون منخفض</div>
        </div>
        <div class="stat-card s-out">
            <div class="stat-icon">❌</div>
            <div class="stat-value"><?= $out_stock ?></div>
            <div class="stat-label">نفد المخزون</div>
        </div>
    </div>

    <!-- FILTERS -->
    <div class="filters-bar">
        <form method="GET" class="filters-form">
            <div class="filter-search">
                <input type="text" name="search" placeholder="ابحث باسم المنتج أو الفئة..." value="<?= htmlspecialchars($search) ?>">
                <button type="submit">
                    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><circle cx="11" cy="11" r="8"/><path d="m21 21-4.35-4.35"/></svg>
                    بحث
                </button>
            </div>

            <select name="filter_cat" class="filter-select" onchange="this.form.submit()">
                <option value="0">كل الفئات</option>
                <?php foreach ($categories as $cat): ?>
                    <option value="<?= $cat['id'] ?>" <?= $filter_cat==$cat['id']?'selected':'' ?>>
                        <?= htmlspecialchars($cat['name']) ?>
                    </option>
                <?php endforeach; ?>
            </select>

            <select name="filter_stat" class="filter-select" onchange="this.form.submit()">
                <option value="all"      <?= $filter_stat==='all'     ?'selected':'' ?>>كل الحالات</option>
                <option value="active"   <?= $filter_stat==='active'  ?'selected':'' ?>>نشط فقط</option>
                <option value="inactive" <?= $filter_stat==='inactive'?'selected':'' ?>>غير نشط فقط</option>
            </select>

            <select name="sort" class="filter-select" onchange="this.form.submit()">
                <option value="newest"     <?= $sort==='newest'    ?'selected':'' ?>>الأحدث أولاً</option>
                <option value="price_asc"  <?= $sort==='price_asc' ?'selected':'' ?>>السعر: الأقل</option>
                <option value="price_desc" <?= $sort==='price_desc'?'selected':'' ?>>السعر: الأعلى</option>
                <option value="stock_asc"  <?= $sort==='stock_asc' ?'selected':'' ?>>المخزون: الأقل</option>
            </select>

            <?php if (!empty($search) || $filter_cat || $filter_stat !== 'all' || $sort !== 'newest'): ?>
                <a href="index.php" class="btn btn-gray">
                    <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>
                    مسح
                </a>
            <?php endif; ?>

            <span class="filters-count"><?= $total ?> منتج</span>
        </form>
    </div>

    <!-- TABLE -->
    <div class="card">
        <div class="table-wrap">
            <table>
                <thead>
                    <tr>
                        <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($products)): ?>
                    <tr><td colspan="10">
                        <div class="empty">
                            <span class="empty-icon">📦</span>
                            <p>لا توجد منتجات<?= !empty($search) ? ' تطابق البحث' : '' ?></p>
                        </div>
                    </td></tr>
                <?php else: ?>
                <?php foreach ($products as $p):
                    $stock = intval($p['calculated_stock']);
                    $stockClass = $stock == 0 ? 'stock-out' : ($stock <= 3 ? 'stock-low' : 'stock-ok');
                    $stockIcon  = $stock == 0 ? '❌' : ($stock <= 3 ? '⚠️' : '✔');
                ?>
                <tr>
                    <td style="font-weight:800;color:var(--text)">#<?= $p['id'] ?></td>
                    <td>
                        <?php if (!empty($p['image'])): ?>
                            <img src="<?= htmlspecialchars($p['image']) ?>" class="prod-img" alt="صورة"
                                 onerror="this.outerHTML='<div class=\'prod-img-placeholder\'>🖼️</div>'">
                        <?php else: ?>
                            <div class="prod-img-placeholder">🖼️</div>
                        <?php endif; ?>
                    </td>
                    <td style="font-weight:700;color:var(--text);max-width:200px">
                        <?= htmlspecialchars($p['name']) ?>
                    </td>
                    <td>
                        <span class="badge b-info"><?= htmlspecialchars($p['category_name'] ?? '--') ?></span>
                    </td>
                    <td style="font-weight:800;color:var(--primary);white-space:nowrap">
                        <?= number_format($p['price'], 2) ?> دج
                    </td>
                    <td>
                        <span class="<?= $stockClass ?>">
                            <?= $stockIcon ?> <?= $stock ?>
                        </span>
                    </td>
                    <td>
                        <form method="POST" style="display:inline">
                            <input type="hidden" name="product_id" value="<?= $p['id'] ?>">
                            <button type="submit" name="toggle_active"
                                class="badge <?= $p['is_active'] ? 'b-active' : 'b-inactive' ?>"
                                style="border:none;cursor:pointer;font-family:'Tajawal',sans-serif"
                                title="اضغط لتغيير الحالة">
                                <?= $p['is_active'] ? '✅ نشط' : '⛔ معطّل' ?>
                            </button>
                        </form>
                    </td>
                    <td>
                        <form method="POST" style="display:inline">
                            <input type="hidden" name="product_id" value="<?= $p['id'] ?>">
                            <button type="submit" name="toggle_featured"
                                class="btn btn-icon <?= $p['is_featured'] ? 'btn-feat-on' : 'btn-feat-off' ?>"
                                title="<?= $p['is_featured'] ? 'إلغاء التميز' : 'تعيين كمميز' ?>">
                                <?= $p['is_featured'] ? '⭐' : '☆' ?>
                            </button>
                        </form>
                    </td>
                    <td>
                        <?php if (!empty($p['delivery_companies'])): ?>
                            <div class="delivery-tag">
                                <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="1" y="3" width="15" height="13"/><path d="M16 8h4l3 3v5h-7V8z"/><circle cx="5.5" cy="18.5" r="2.5"/><circle cx="18.5" cy="18.5" r="2.5"/></svg>
                                <?= htmlspecialchars($p['delivery_companies']) ?>
                            </div>
                        <?php else: ?>
                            <span style="color:var(--text-3);font-size:12px">--</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <div class="action-btns">
                            <!-- تعديل -->
                            <a href="edit_product.php?id=<?= $p['id'] ?>" class="btn btn-warning btn-icon" title="تعديل">
                                <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><path d="M11 4H4a2 2 0 00-2 2v14a2 2 0 002 2h14a2 2 0 002-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 013 3L12 15l-4 1 1-4 9.5-9.5z"/></svg>
                            </a>
                            <!-- حذف -->
                            <a href="delete_product.php?id=<?= $p['id'] ?>"
                               class="btn btn-danger btn-icon" title="حذف"
                               onclick="return confirm('حذف المنتج «<?= htmlspecialchars($p['name'], ENT_QUOTES) ?>»؟')">
                                <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><polyline points="3 6 5 6 21 6"/><path d="M19 6l-1 14a2 2 0 01-2 2H8a2 2 0 01-2-2L5 6"/><path d="M10 11v6M14 11v6"/><path d="M9 6V4a1 1 0 011-1h4a1 1 0 011 1v2"/></svg>
                            </a>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

</div>
</body>
</html>