<?php

namespace App\Livewire\Monitoring;

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

class AttendanceLogIndex extends Component
{
    use WithPagination;

    public $search = '';
    public $perPage = 15;

    public function updatingSearch()
    {
        $this->resetPage();
    }

    public function render()
    {
        $logs = AttendanceLog::with(['machine', 'user'])
            ->when($this->search, function ($query) {
                $query->where('raw_user_id', 'like', '%' . $this->search . '%')
                      ->orWhereHas('user', function ($q) {
                          $q->where('name', 'like', '%' . $this->search . '%');
                      });
            })
            ->orderBy('timestamp', 'desc')
            ->paginate($this->perPage);

        return view('livewire.monitoring.attendance-log-index', [
            'logs' => $logs
        ])->layout('components.layouts.app');
    }
}
