delete('products', 'product_id = :id', ['id' => $_POST['product_id']]); setFlash('success', 'Product deleted successfully'); header('Location: /admin/products.php'); exit; } if ($action === 'bulk_delete' && !empty($_POST['product_ids'])) { $ids = $_POST['product_ids']; $placeholders = implode(',', array_fill(0, count($ids), '?')); db()->query("DELETE FROM products WHERE product_id IN ($placeholders)", $ids); setFlash('success', count($ids) . ' products deleted'); header('Location: /admin/products.php'); exit; } if ($action === 'toggle_status' && !empty($_POST['product_id'])) { $product = db()->fetch("SELECT is_active FROM products WHERE product_id = :id", ['id' => $_POST['product_id']]); if ($product) { db()->update('products', ['is_active' => !$product['is_active']], 'product_id = :id', ['id' => $_POST['product_id']]); setFlash('success', 'Product status updated'); } header('Location: /admin/products.php'); exit; } } // Filters $search = $_GET['search'] ?? ''; $category = $_GET['category'] ?? ''; $status = $_GET['status'] ?? ''; $page = max(1, intval($_GET['page'] ?? 1)); // Build query $where = ['1=1']; $params = []; if ($search) { $where[] = '(name LIKE :search OR sku LIKE :search)'; $params['search'] = '%' . $search . '%'; } if ($category) { $where[] = 'category = :category'; $params['category'] = $category; } if ($status === 'active') { $where[] = 'is_active = 1'; } elseif ($status === 'inactive') { $where[] = 'is_active = 0'; } elseif ($status === 'low_stock') { $where[] = 'stock <= low_stock_threshold'; } $whereClause = implode(' AND ', $where); // Get total and paginate $totalProducts = db()->count('products', $whereClause, $params); $pagination = paginate($totalProducts, $page, ADMIN_ITEMS_PER_PAGE); // Get products $products = db()->fetchAll( "SELECT * FROM products WHERE {$whereClause} ORDER BY created_at DESC LIMIT :limit OFFSET :offset", array_merge($params, ['limit' => $pagination['per_page'], 'offset' => $pagination['offset']]) ); // Get categories $categories = db()->fetchAll( "SELECT DISTINCT category FROM products WHERE category IS NOT NULL AND category != '' ORDER BY category" ); ?>