conn = $conn; } public function getProducts($filters = [], $page = 1, $limit = 12) { $offset = ($page - 1) * $limit; $where = []; $params = []; $types = ""; if (!empty($filters['category'])) { $where[] = "category_id = ?"; $params[] = $filters['category']; $types .= "i"; } if (!empty($filters['search'])) { $search = "%" . $filters['search'] . "%"; $where[] = "(name LIKE ? OR name_ar LIKE ? OR description LIKE ? OR description_ar LIKE ?)"; $params = array_merge($params, [$search, $search, $search, $search]); $types .= "ssss"; } if (!empty($filters['min_price'])) { $where[] = "price >= ?"; $params[] = $filters['min_price']; $types .= "d"; } if (!empty($filters['max_price'])) { $where[] = "price <= ?"; $params[] = $filters['max_price']; $types .= "d"; } $whereClause = !empty($where) ? "WHERE " . implode(" AND ", $where) : ""; // Get total count $countQuery = "SELECT COUNT(*) as total FROM products $whereClause"; $stmt = $this->conn->prepare($countQuery); if (!empty($params)) { $stmt->bind_param($types, ...$params); } $stmt->execute(); $total = $stmt->get_result()->fetch_assoc()['total']; // Get products $query = "SELECT p.*, c.name as category_name, c.name_ar as category_name_ar, (SELECT image_url FROM product_images WHERE product_id = p.id AND is_primary = 1 LIMIT 1) as primary_image, (SELECT AVG(rating) FROM reviews WHERE product_id = p.id) as average_rating, (SELECT COUNT(*) FROM reviews WHERE product_id = p.id) as review_count FROM products p LEFT JOIN categories c ON p.category_id = c.id $whereClause ORDER BY p.created_at DESC LIMIT ? OFFSET ?"; $stmt = $this->conn->prepare($query); $params[] = $limit; $params[] = $offset; $types .= "ii"; $stmt->bind_param($types, ...$params); $stmt->execute(); $result = $stmt->get_result(); $products = []; while ($row = $result->fetch_assoc()) { $products[] = $row; } return [ 'products' => $products, 'total' => $total, 'pages' => ceil($total / $limit), 'current_page' => $page ]; } public function getProduct($id) { $query = "SELECT p.*, c.name as category_name, c.name_ar as category_name_ar, (SELECT AVG(rating) FROM reviews WHERE product_id = p.id) as average_rating, (SELECT COUNT(*) FROM reviews WHERE product_id = p.id) as review_count FROM products p LEFT JOIN categories c ON p.category_id = c.id WHERE p.id = ?"; $stmt = $this->conn->prepare($query); $stmt->bind_param("i", $id); $stmt->execute(); $product = $stmt->get_result()->fetch_assoc(); if ($product) { // Get images $stmt = $this->conn->prepare("SELECT * FROM product_images WHERE product_id = ? ORDER BY is_primary DESC, sort_order ASC"); $stmt->bind_param("i", $id); $stmt->execute(); $result = $stmt->get_result(); $product['images'] = []; while ($row = $result->fetch_assoc()) { $product['images'][] = $row; } // Get reviews $stmt = $this->conn->prepare(" SELECT r.*, u.username, u.full_name FROM reviews r LEFT JOIN users u ON r.user_id = u.id WHERE r.product_id = ? ORDER BY r.created_at DESC "); $stmt->bind_param("i", $id); $stmt->execute(); $result = $stmt->get_result(); $product['reviews'] = []; while ($row = $result->fetch_assoc()) { $product['reviews'][] = $row; } // Get related products $stmt = $this->conn->prepare(" SELECT p.*, (SELECT image_url FROM product_images WHERE product_id = p.id AND is_primary = 1 LIMIT 1) as primary_image FROM products p WHERE p.category_id = ? AND p.id != ? LIMIT 4 "); $stmt->bind_param("ii", $product['category_id'], $id); $stmt->execute(); $result = $stmt->get_result(); $product['related_products'] = []; while ($row = $result->fetch_assoc()) { $product['related_products'][] = $row; } } return $product; } public function addReview($data) { if (!is_logged_in()) { return ['success' => false, 'message' => 'يجب تسجيل الدخول لإضافة تقييم']; } $stmt = $this->conn->prepare("INSERT INTO reviews (product_id, user_id, rating, comment) VALUES (?, ?, ?, ?)"); $stmt->bind_param("iiis", $data['product_id'], $_SESSION['user_id'], $data['rating'], $data['comment']); if ($stmt->execute()) { return ['success' => true, 'message' => 'تم إضافة التقييم بنجاح']; } return ['success' => false, 'message' => 'حدث خطأ أثناء إضافة التقييم']; } public function getCategories() { $query = "SELECT * FROM categories ORDER BY parent_id ASC, name ASC"; $result = $this->conn->query($query); $categories = []; while ($row = $result->fetch_assoc()) { $categories[] = $row; } return $categories; } }