<?php

namespace App\Http\Controllers;

use App\Models\AttendanceUser;
use App\Models\AttendanceUserRequest;
use Illuminate\Http\Request;
use Illuminate\View\View;

class AttendanceUserController extends Controller
{
    public function index(Request $request): View
    {
        $search = $request->query('q');

        $usersQuery = AttendanceUser::query();
        if ($search) {
            $usersQuery->where('pin', 'like', '%' . $search . '%')
                ->orWhere('name', 'like', '%' . $search . '%');
        }

        $users = $usersQuery->orderBy('pin')
            ->paginate(10)
            ->withQueryString();

        $requests = AttendanceUserRequest::query()
            ->latest()
            ->paginate(10, ['*'], 'req_page')
            ->withQueryString();

        return view('attendance.users', compact('users', 'requests', 'search'));
    }
}
