<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Pagination\LengthAwarePaginator;

class UserController extends Controller
{
    public function index(Request $request)
    {
        $data = User::query();
        $search = $request->input('search');
        if ($search) {
            $data->where('name', 'like', "%$search%");
        }
        $sortField = $request->input('sort_field');
        $sortOrder = $request->input('sort_order', 'asc');
        if ($sortField) {
            $data->orderBy($sortField, $sortOrder);
        }
        $paginatedData = $data->paginate($request->input('per_page', 10));
        return view('users', compact('paginatedData'));
    }
}