<?php
namespace App\Http\Controllers;
use App\Models\Item;
use App\Models\Category;
use Illuminate\Http\Request;
class ItemController extends Controller
{
// عرض جميع المنتجات
public function index()
{
$items = Item::with('category')->get();
return view('items.index', compact('items'));
}
// عرض نموذج إضافة منتج جديد
public function create()
{
$categories = Category::all();
return view('items.create', compact('categories'));
}
// تخزين منتج جديد
public function store(Request $request)
{
// التحقق من صحة البيانات
$validated = $request->validate([
'item_name' => 'required|string|max:255',
'description' => 'required|string',
'old_price' => 'nullable|numeric',
'new_price' => 'required|numeric',
'category_id' => 'required|exists:categories,id',
'main_img' => 'required|image|mimes:jpeg,png,jpg,gif,webp',
'img_1' => 'nullable|image|mimes:jpeg,png,jpg,gif,webp',
'img_2' => 'nullable|image|mimes:jpeg,png,jpg,gif,webp',
'img_3' => 'nullable|image|mimes:jpeg,png,jpg,gif,webp',
'trend' => 'nullable|in:1', // الحقل trend يمكن أن يكون 1 فقط
'new' => 'nullable|in:1', // الحقل new يمكن أن يكون 1 فقط
'exist' => 'nullable|in:1', // الحقل new يمكن أن يكون 1 فقط
]);
// تعديل القيم الافتراضية للحقول trend وnew
$validated['trend'] = $request->has('trend') ? 1 : 0; // إذا تم تحديد العنصر، القيمة تكون 1، وإلا 0
$validated['new'] = $request->has('new') ? 1 : 0; // إذا تم تحديد العنصر، القيمة تكون 1، وإلا 0
$validated['exist'] = $request->has('exist') ? 1 : 0; // إذا تم تحديد العنصر، القيمة تكون 1، وإلا 0
// dd($validated);
// تحديد المسار الذي سيتم حفظ الصور فيه
$path = 'images/items'; // مجلد items داخل public
// رفع الصورة الرئيسية (main_img)
if ($request->hasFile('main_img')) {
$mainFilename = time() . '_main_' . $request->file('main_img')->getClientOriginalName();
$request->file('main_img')->move($path, $mainFilename);
$validated['main_img'] = 'images/items/' . $mainFilename;
}
// رفع الصورة الإضافية الأولى (img_1)
if ($request->hasFile('img_1')) {
$img1Filename = time() . '_img1_' . $request->file('img_1')->getClientOriginalName();
$request->file('img_1')->move($path, $img1Filename);
$validated['img_1'] = 'images/items/' . $img1Filename;
}
// رفع الصورة الإضافية الثانية (img_2)
if ($request->hasFile('img_2')) {
$img2Filename = time() . '_img2_' . $request->file('img_2')->getClientOriginalName();
$request->file('img_2')->move($path, $img2Filename);
$validated['img_2'] = 'images/items/' . $img2Filename;
}
// رفع الصورة الإضافية الثالثة (img_3)
if ($request->hasFile('img_3')) {
$img3Filename = time() . '_img3_' . $request->file('img_3')->getClientOriginalName();
$request->file('img_3')->move($path, $img3Filename);
$validated['img_3'] = 'images/items/' . $img3Filename;
}
// إنشاء المنتج باستخدام create
Item::create($validated);
return redirect()->route('items.index')->with('success', 'تم إضافة المنتج بنجاح!');
}
// عرض تفاصيل منتج معين
public function show(Item $item)
{
return view('items.show', compact('item'));
}
// عرض نموذج تعديل منتج
public function edit(Item $item)
{
$categories = Category::all();
return view('items.edit', compact('item', 'categories'));
}
// تحديث منتج
public function update(Request $request, Item $item)
{
// التحقق من صحة البيانات
$validated = $request->validate([
'item_name' => 'required|string|max:255',
'old_price' => 'nullable|numeric',
'new_price' => 'required|numeric',
'category_id' => 'required|exists:categories,id',
'main_img' => 'nullable|image|mimes:jpeg,png,jpg,gif,webp',
'img_1' => 'nullable|image|mimes:jpeg,png,jpg,gif,webp',
'img_2' => 'nullable|image|mimes:jpeg,png,jpg,gif,webp',
'img_3' => 'nullable|image|mimes:jpeg,png,jpg,gif,webp',
'trend' => 'nullable|in:1', // الحقل trend يمكن أن يكون 1 فقط
'new' => 'nullable|in:1', // الحقل new يمكن أن يكون 1 فقط
'exist' => 'nullable|in:1', // الحقل new يمكن أن يكون 1 فقط
]);
// تعديل القيم الافتراضية للحقول trend وnew
$validated['trend'] = $request->has('trend') ? 1 : 0;
$validated['new'] = $request->has('new') ? 1 : 0;
$validated['exist'] = $request->has('exist') ? 1 : 0;
// تحديد المسار الذي سيتم حفظ الصور فيه
$path = 'images/items'; // مجلد items داخل public
// حذف الصور القديمة (إذا تم تحديد عناصر checkbox للحذف)
if ($request->has('delete_img_1') && $item->img_1) {
$oldImg1Path = public_path($item->img_1);
if (file_exists($oldImg1Path)) {
unlink($oldImg1Path); // حذف الصورة القديمة
}
$validated['img_1'] = null; // تعيين الحقل كـ NULL
}
if ($request->has('delete_img_2') && $item->img_2) {
$oldImg2Path = public_path($item->img_2);
if (file_exists($oldImg2Path)) {
unlink($oldImg2Path); // حذف الصورة القديمة
}
$validated['img_2'] = null; // تعيين الحقل كـ NULL
}
if ($request->has('delete_img_3') && $item->img_3) {
$oldImg3Path = public_path($item->img_3);
if (file_exists($oldImg3Path)) {
unlink($oldImg3Path); // حذف الصورة القديمة
}
$validated['img_3'] = null; // تعيين الحقل كـ NULL
}
// رفع الصورة الرئيسية (main_img)
if ($request->hasFile('main_img')) {
if ($item->main_img) {
$oldMainImagePath = public_path($item->main_img);
if (file_exists($oldMainImagePath)) {
unlink($oldMainImagePath); // حذف الصورة القديمة
}
}
$mainFilename = time() . '_main_' . $request->file('main_img')->getClientOriginalName();
$request->file('main_img')->move($path, $mainFilename);
$validated['main_img'] = 'images/items/' . $mainFilename;
}
// رفع الصور الإضافية
foreach (['img_1', 'img_2', 'img_3'] as $imgField) {
if ($request->hasFile($imgField)) {
if ($item->$imgField) {
$oldImagePath = public_path($item->$imgField);
if (file_exists($oldImagePath)) {
unlink($oldImagePath); // حذف الصورة القديمة
}
}
$filename = time() . '_' . $imgField . '_' . $request->file($imgField)->getClientOriginalName();
$request->file($imgField)->move($path, $filename);
$validated[$imgField] = 'images/items/' . $filename;
}
}
// تحديث المنتج باستخدام update
$item->update($validated);
return redirect()->route('items.index')->with('success', 'تم تحديث المنتج بنجاح!');
}
// حذف منتج
public function destroy(Item $item)
{
// تحديد المسار الذي تم حفظ الصور فيه
$path = 'images/items'; // مجلد items داخل public
// حذف الصورة الرئيسية (main_img)
if ($item->main_img && file_exists(public_path($item->main_img))) {
unlink(public_path($item->main_img)); // حذف الصورة من النظام
}
// حذف الصور الإضافية (img_1, img_2, img_3)
foreach (['img_1', 'img_2', 'img_3'] as $imgField) {
if ($item->$imgField && file_exists(public_path($item->$imgField))) {
unlink(public_path($item->$imgField)); // حذف الصورة من النظام
}
}
// حذف المنتج من قاعدة البيانات
$item->delete();
return redirect()->route('items.index')->with('success', 'تم حذف المنتج بنجاح!');
}
public function search(Request $request)
{
$query = $request->input('query');
if (!empty($query)) {
$items = Item::where('item_name', 'like', '%' . $query . '%')
->orWhereHas('category', function ($q) use ($query) {
$q->where('category_name', 'like', '%' . $query . '%');
})
->with('category')
->paginate(10);
// إذا كان هناك بحث ولكن لا يوجد نتائج
if ($items->isEmpty()) {
session()->flash('message', 'لم يتم العثور على منتجات مطابقة. إليك أكثر المنتجات رواجاً:');
$items = Item::where('trend', 1)->with('category')->take(10)->get();
}
} else {
// إذا لم يكن هناك أي بحث
session()->flash('message', 'الاكثر مطابقة:');
$items = Item::where('trend', 1)->with('category')->take(10)->get();
}
return view('search', compact('items', 'query'));
}
}