Devco1
Home
Console
Upload
New File
New Folder
Tools
Info
About
/
home
/
wifiyecu
/
www
/
admin
/
Filename :
products.php
back
Copy
<?php session_start(); include '../includes/config.php'; // التحقق من صلاحيات المدير if (!isset($_SESSION['admin_logged_in'])) { header('Location: login.php'); exit; } // معالجة حذف المنتج if (isset($_GET['delete_id'])) { $product_id = intval($_GET['delete_id']); try { // بداية transaction $pdo->beginTransaction(); // حذف الصور المرتبطة $stmt = $pdo->prepare("DELETE FROM product_images WHERE product_id = ?"); $stmt->execute([$product_id]); // حذف الألوان المرتبطة $stmt = $pdo->prepare("DELETE FROM colors WHERE product_id = ?"); $stmt->execute([$product_id]); // حذف التقييمات المرتبطة $stmt = $pdo->prepare("DELETE FROM reviews WHERE product_id = ?"); $stmt->execute([$product_id]); // حذف المنتج من سلة التسوق $stmt = $pdo->prepare("DELETE FROM cart WHERE product_id = ?"); $stmt->execute([$product_id]); // حذف المنتج $stmt = $pdo->prepare("DELETE FROM products WHERE id = ?"); $stmt->execute([$product_id]); $pdo->commit(); $_SESSION['message'] = [ 'text' => 'تم حذف المنتج بنجاح', 'type' => 'success' ]; } catch (PDOException $e) { $pdo->rollBack(); $_SESSION['message'] = [ 'text' => 'خطأ في حذف المنتج: ' . $e->getMessage(), 'type' => 'danger' ]; } header('Location: products.php'); exit; } // الحصول على قائمة المنتجات مع معلومات إضافية try { $search = $_GET['search'] ?? ''; $brand = $_GET['brand'] ?? ''; $sql = "SELECT p.*, COUNT(pi.id) as image_count, COUNT(r.id) as review_count, COALESCE(AVG(r.rating), 0) as avg_rating FROM products p LEFT JOIN product_images pi ON p.id = pi.product_id LEFT JOIN reviews r ON p.id = r.product_id WHERE 1=1"; $params = []; if (!empty($search)) { $sql .= " AND (p.name LIKE ? OR p.description LIKE ?)"; $search_term = "%$search%"; $params[] = $search_term; $params[] = $search_term; } if (!empty($brand)) { $sql .= " AND p.brand = ?"; $params[] = $brand; } $sql .= " GROUP BY p.id ORDER BY p.created_at DESC"; $stmt = $pdo->prepare($sql); $stmt->execute($params); $products = $stmt->fetchAll(); // الحصول على الماركات المتاحة $brands_stmt = $pdo->query("SELECT DISTINCT brand FROM products WHERE brand IS NOT NULL ORDER BY brand"); $brands = $brands_stmt->fetchAll(); } catch (PDOException $e) { die("خطأ في قاعدة البيانات: " . $e->getMessage()); } ?> <!DOCTYPE html> <html lang="ar" dir="rtl"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>إدارة المنتجات - لوحة التحكم</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-icons/1.8.1/font/bootstrap-icons.min.css"> <style> .product-image { width: 60px; height: 60px; object-fit: cover; border-radius: 4px; } .table-actions { white-space: nowrap; } .status-badge { font-size: 0.75rem; } .search-box { max-width: 300px; } </style> </head> <body> <!-- شريط التنقل --> <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <div class="container"> <a class="navbar-brand" href="index.php"> <i class="fas fa-cog"></i> لوحة التحكم </a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav me-auto"> <li class="nav-item"> <a class="nav-link" href="index.php"> <i class="fas fa-tachometer-alt"></i> الرئيسية </a> </li> <li class="nav-item"> <a class="nav-link active" href="products.php"> <i class="fas fa-mobile-alt"></i> المنتجات </a> </li> <li class="nav-item"> <a class="nav-link" href="orders.php"> <i class="fas fa-shopping-cart"></i> الطلبات </a> </li> </ul> <ul class="navbar-nav"> <li class="nav-item"> <span class="nav-link text-light"> <i class="fas fa-user"></i> <?php echo $_SESSION['admin_username']; ?> </span> </li> <li class="nav-item"> <a class="nav-link" href="logout.php"> <i class="fas fa-sign-out-alt"></i> تسجيل الخروج </a> </li> </ul> </div> </div> </nav> <div class="container-fluid mt-4"> <!-- رسائل التنبيه --> <?php if (isset($_SESSION['message'])): ?> <div class="alert alert-<?php echo $_SESSION['message']['type']; ?> alert-dismissible fade show" role="alert"> <?php echo $_SESSION['message']['text']; ?> <button type="button" class="btn-close" data-bs-dismiss="alert"></button> </div> <?php unset($_SESSION['message']); ?> <?php endif; ?> <!-- رأس الصفحة --> <div class="row mb-4"> <div class="col-12"> <div class="d-flex justify-content-between align-items-center"> <div> <h2 class="mb-1"> <i class="fas fa-mobile-alt me-2"></i>إدارة المنتجات </h2> <p class="text-muted mb-0">إدارة وعرض جميع منتجات المتجر</p> </div> <a href="add_product.php" class="btn btn-primary"> <i class="fas fa-plus me-2"></i>إضافة منتج جديد </a> </div> </div> </div> <!-- أدوات البحث والتصفية --> <div class="row mb-4"> <div class="col-12"> <div class="card"> <div class="card-body"> <form method="GET" class="row g-3"> <div class="col-md-4"> <div class="input-group"> <span class="input-group-text"> <i class="fas fa-search"></i> </span> <input type="text" class="form-control" name="search" placeholder="ابحث عن منتج..." value="<?php echo htmlspecialchars($search); ?>"> </div> </div> <div class="col-md-3"> <select class="form-select" name="brand"> <option value="">جميع الماركات</option> <?php foreach ($brands as $brand_item): ?> <option value="<?php echo htmlspecialchars($brand_item['brand']); ?>" <?php echo ($brand == $brand_item['brand']) ? 'selected' : ''; ?>> <?php echo htmlspecialchars($brand_item['brand']); ?> </option> <?php endforeach; ?> </select> </div> <div class="col-md-2"> <button type="submit" class="btn btn-primary w-100"> <i class="fas fa-filter me-2"></i>تصفية </button> </div> <div class="col-md-3 text-start"> <a href="products.php" class="btn btn-outline-secondary"> <i class="fas fa-refresh me-2"></i>إعادة تعيين </a> </div> </form> </div> </div> </div> </div> <!-- إحصائيات سريعة --> <div class="row mb-4"> <div class="col-md-3"> <div class="card bg-primary text-white"> <div class="card-body"> <div class="d-flex justify-content-between"> <div> <h4 class="mb-0"><?php echo count($products); ?></h4> <span>إجمالي المنتجات</span> </div> <i class="fas fa-mobile-alt fa-2x opacity-50"></i> </div> </div> </div> </div> <div class="col-md-3"> <div class="card bg-success text-white"> <div class="card-body"> <div class="d-flex justify-content-between"> <div> <h4 class="mb-0"> <?php $in_stock = array_filter($products, function($product) { return $product['stock_quantity'] > 0; }); echo count($in_stock); ?> </h4> <span>منتجات متوفرة</span> </div> <i class="fas fa-check-circle fa-2x opacity-50"></i> </div> </div> </div> </div> <div class="col-md-3"> <div class="card bg-warning text-white"> <div class="card-body"> <div class="d-flex justify-content-between"> <div> <h4 class="mb-0"> <?php $out_of_stock = array_filter($products, function($product) { return $product['stock_quantity'] == 0; }); echo count($out_of_stock); ?> </h4> <span>منتجات غير متوفرة</span> </div> <i class="fas fa-exclamation-triangle fa-2x opacity-50"></i> </div> </div> </div> </div> <div class="col-md-3"> <div class="card bg-info text-white"> <div class="card-body"> <div class="d-flex justify-content-between"> <div> <h4 class="mb-0"> <?php $with_discount = array_filter($products, function($product) { return $product['old_price'] && $product['old_price'] > $product['price']; }); echo count($with_discount); ?> </h4> <span>عروض خاصة</span> </div> <i class="fas fa-tag fa-2x opacity-50"></i> </div> </div> </div> </div> </div> <!-- جدول المنتجات --> <div class="row"> <div class="col-12"> <div class="card"> <div class="card-header"> <h5 class="card-title mb-0"> <i class="fas fa-list me-2"></i>قائمة المنتجات </h5> </div> <div class="card-body"> <?php if (empty($products)): ?> <div class="text-center py-5"> <i class="fas fa-mobile-alt fa-3x text-muted mb-3"></i> <h4 class="text-muted">لا توجد منتجات</h4> <p class="text-muted mb-4">لم يتم إضافة أي منتجات بعد</p> <a href="add_product.php" class="btn btn-primary"> <i class="fas fa-plus me-2"></i>إضافة أول منتج </a> </div> <?php else: ?> <div class="table-responsive"> <table class="table table-hover"> <thead class="table-light"> <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 foreach ($products as $index => $product): ?> <tr> <td><?php echo $index + 1; ?></td> <td> <?php // الحصول على الصورة الرئيسية $image_stmt = $pdo->prepare("SELECT image_url FROM product_images WHERE product_id = ? AND is_main = 1 LIMIT 1"); $image_stmt->execute([$product['id']]); $main_image = $image_stmt->fetch(); ?> <img src="../<?php echo $main_image['image_url'] ?? 'images/placeholder.jpg'; ?>" alt="<?php echo htmlspecialchars($product['name']); ?>" class="product-image" onerror="this.src='../images/placeholder.jpg'"> </td> <td> <div> <strong><?php echo htmlspecialchars($product['name']); ?></strong> <?php if ($product['image_count'] > 1): ?> <span class="badge bg-info ms-1" title="<?php echo $product['image_count']; ?> صورة"> <i class="fas fa-images"></i> <?php echo $product['image_count']; ?> </span> <?php endif; ?> </div> <small class="text-muted"> <?php echo strlen($product['description']) > 100 ? substr($product['description'], 0, 100) . '...' : $product['description']; ?> </small> </td> <td> <?php if ($product['brand']): ?> <span class="badge bg-secondary"><?php echo htmlspecialchars($product['brand']); ?></span> <?php else: ?> <span class="text-muted">-</span> <?php endif; ?> </td> <td> <div> <strong class="text-primary">$<?php echo $product['price']; ?></strong> <?php if ($product['old_price'] && $product['old_price'] > $product['price']): ?> <div> <small class="text-muted text-decoration-line-through">$<?php echo $product['old_price']; ?></small> <span class="badge bg-danger ms-1"> خصم <?php echo round((($product['old_price'] - $product['price']) / $product['old_price']) * 100); ?>% </span> </div> <?php endif; ?> </div> </td> <td> <?php if ($product['stock_quantity'] > 10): ?> <span class="badge bg-success"> <i class="fas fa-check"></i> <?php echo $product['stock_quantity']; ?> </span> <?php elseif ($product['stock_quantity'] > 0): ?> <span class="badge bg-warning"> <i class="fas fa-exclamation"></i> <?php echo $product['stock_quantity']; ?> </span> <?php else: ?> <span class="badge bg-danger"> <i class="fas fa-times"></i> غير متوفر </span> <?php endif; ?> </td> <td> <?php if ($product['review_count'] > 0): ?> <div class="d-flex align-items-center"> <div class="stars me-2"> <?php $rating = $product['avg_rating']; $full_stars = floor($rating); $half_star = ($rating - $full_stars) >= 0.5; $empty_stars = 5 - $full_stars - ($half_star ? 1 : 0); echo str_repeat('<i class="fas fa-star text-warning" style="font-size: 12px;"></i>', $full_stars); if ($half_star) { echo '<i class="fas fa-star-half-alt text-warning" style="font-size: 12px;"></i>'; } echo str_repeat('<i class="far fa-star text-warning" style="font-size: 12px;"></i>', $empty_stars); ?> </div> <small class="text-muted">(<?php echo $product['review_count']; ?>)</small> </div> <?php else: ?> <span class="text-muted">لا توجد تقييمات</span> <?php endif; ?> </td> <td> <?php if ($product['stock_quantity'] > 0): ?> <span class="badge bg-success">نشط</span> <?php else: ?> <span class="badge bg-secondary">غير نشط</span> <?php endif; ?> </td> <td> <small class="text-muted"> <?php echo date('Y-m-d', strtotime($product['created_at'])); ?> </small> </td> <td> <div class="table-actions"> <a href="../product.php?id=<?php echo $product['id']; ?>" class="btn btn-sm btn-outline-primary" target="_blank" title="عرض في المتجر"> <i class="fas fa-eye"></i> </a> <a href="edit_product.php?id=<?php echo $product['id']; ?>" class="btn btn-sm btn-outline-warning" title="تعديل المنتج"> <i class="fas fa-edit"></i> </a> <a href="products.php?delete_id=<?php echo $product['id']; ?>" class="btn btn-sm btn-outline-danger" title="حذف المنتج" onclick="return confirm('هل أنت متأكد من حذف هذا المنتج؟ هذا الإجراء لا يمكن التراجع عنه.');"> <i class="fas fa-trash"></i> </a> </div> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> <?php endif; ?> </div> </div> </div> </div> </div> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script> <script> // تأكيد الحذف function confirmDelete(productName) { return confirm(`هل أنت متأكد من حذف المنتج "${productName}"؟ هذا الإجراء لا يمكن التراجع عنه.`); } // البحث الفوري document.addEventListener('DOMContentLoaded', function() { const searchInput = document.querySelector('input[name="search"]'); const brandSelect = document.querySelector('select[name="brand"]'); // البحث التلقائي بعد كتابة 3 أحرف let searchTimeout; searchInput.addEventListener('input', function() { clearTimeout(searchTimeout); if (this.value.length >= 3 || this.value.length === 0) { searchTimeout = setTimeout(() => { this.form.submit(); }, 500); } }); // التصفية التلقائية عند تغيير الماركة brandSelect.addEventListener('change', function() { this.form.submit(); }); }); </script> </body> </html>