<?php

namespace App\Http\Controllers\API\v1;

use App\Http\Controllers\Controller;
use App\Models\Audit;
use App\Models\Employee;
use Illuminate\Http\Request;

class AuditController extends Controller
{
    public function index(Request $request)
    {
        $keyword = $request->input('keyword', '');
        $perPage = $request->input('per_page',PHP_INT_MAX);
        $sortBy = $request->input('sortBy', '');
        $sortType = $request->input('sortType', '');
        
        $data = Audit::with('employee')->where(function ($query) use ($keyword) {
            $keyword = str_replace(" ", "%", $keyword);
            // $query->where('user_id', 'like', '%' . $keyword . '%');
            // $query->where('employees.firstname', 'like', '%' . $keyword . '%');
            // $query->where('employees.lastname', 'like', '%' . $keyword . '%');
        });

        if (!empty($sortBy) && !empty($sortType)) {
            $data = $data->orderBy($sortBy, $sortType);
        }

        $data = $data->paginate($perPage);
        return response()->json($data);

        // $audit = Audit::all();
    }
}
