<?php

namespace App\Livewire\Admin\Services;

use App\Models\Service;
use Livewire\Component;
use Livewire\WithPagination;

class CategoryView extends Component
{
    use WithPagination;

    protected $paginationTheme = 'tailwind';

    public string $search = '';

    // 🔁 VERY IMPORTANT
    public function updatingSearch()
    {
        $this->resetPage();
    }

    public function render()
{
    return view('livewire.admin.services.category-view', [
        'services' => Service::with('category')
            ->where(function ($q) {
                $q->where('title', 'like', '%'.$this->search.'%')
                  ->orWhere('description', 'like', '%'.$this->search.'%')
                  ->orWhereHas('category', function ($cat) {
                      $cat->where('name', 'like', '%'.$this->search.'%');
                  });
            })
            ->orderBy('created_at', 'asc') // ✅ old first
            ->paginate(10),
    ])->layout('layouts.admin');
}

}
