117 lines
4.1 KiB
PHP
117 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Employer;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
use App\Models\User;
|
|
use App\Models\Worker;
|
|
use App\Models\WorkerCategory;
|
|
use App\Models\Shortlist;
|
|
|
|
class WorkerController extends Controller
|
|
{
|
|
private function resolveCurrentUser()
|
|
{
|
|
$sess = session('user');
|
|
$sessId = is_array($sess) ? ($sess['id'] ?? null) : ($sess->id ?? null);
|
|
|
|
if (!$sessId) {
|
|
$user = User::where('role', 'employer')->first();
|
|
if ($user) {
|
|
session(['user' => (object)[
|
|
'id' => $user->id,
|
|
'name' => $user->name,
|
|
'email' => $user->email,
|
|
'role' => 'employer',
|
|
'subscription_status' => $user->subscription_status ?? 'active',
|
|
]]);
|
|
return $user;
|
|
}
|
|
} else {
|
|
return User::find($sessId);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$user = $this->resolveCurrentUser();
|
|
$employerId = $user ? $user->id : 2;
|
|
|
|
// Fetch workers from DB
|
|
$dbWorkers = Worker::with(['category', 'skills'])
|
|
->where('status', 'active')
|
|
->get();
|
|
|
|
$workers = $dbWorkers->map(function ($w) {
|
|
return [
|
|
'id' => $w->id,
|
|
'name' => $w->name,
|
|
'nationality' => $w->nationality,
|
|
'category' => $w->category ? $w->category->name : 'General Helper',
|
|
'skills' => $w->skills->pluck('name')->toArray(),
|
|
'availability' => $w->availability,
|
|
'experience' => $w->experience,
|
|
'salary' => (int)$w->salary,
|
|
'religion' => $w->religion,
|
|
'languages' => ['English', 'Arabic'], // Custom language field or mock
|
|
'age' => $w->age,
|
|
'verified' => (bool)$w->verified,
|
|
'bio' => $w->bio,
|
|
];
|
|
})->toArray();
|
|
|
|
// Get saved shortlist for current employer
|
|
$shortlistedIds = Shortlist::where('employer_id', $employerId)->pluck('worker_id')->toArray();
|
|
|
|
// Dynamically build filter metadata from DB values
|
|
$dbCategories = WorkerCategory::pluck('name')->toArray();
|
|
$dbNationalities = Worker::distinct()->where('status', 'active')->pluck('nationality')->toArray();
|
|
|
|
$filtersMetadata = [
|
|
'categories' => array_merge(['All Categories'], $dbCategories),
|
|
'nationalities' => array_merge(['All Nationalities'], $dbNationalities),
|
|
'availabilities' => ['All Availabilities', 'Immediate', '1 Week', '2 Weeks', '1 Month'],
|
|
'experienceLevels' => ['All Experience', '1-2 Years', '3-5 Years', '5+ Years'],
|
|
'religions' => ['All Religions', 'Christian', 'Muslim', 'Hindu', 'Buddhist'],
|
|
];
|
|
|
|
return Inertia::render('Employer/Workers/Index', [
|
|
'initialWorkers' => $workers,
|
|
'initialShortlistedIds' => $shortlistedIds,
|
|
'filtersMetadata' => $filtersMetadata,
|
|
]);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$w = Worker::with(['category', 'skills', 'documents'])->findOrFail($id);
|
|
|
|
$worker = [
|
|
'id' => $w->id,
|
|
'name' => $w->name,
|
|
'nationality' => $w->nationality,
|
|
'category' => $w->category ? $w->category->name : 'General Helper',
|
|
'skills' => $w->skills->pluck('name')->toArray(),
|
|
'availability' => $w->availability,
|
|
'experience' => $w->experience,
|
|
'experience_years' => 5, // Can parse or default
|
|
'salary' => (int)$w->salary,
|
|
'religion' => $w->religion,
|
|
'languages' => ['English', 'Arabic'],
|
|
'age' => $w->age,
|
|
'verified' => (bool)$w->verified,
|
|
'bio' => $w->bio,
|
|
'passport_status' => 'OCR Verified',
|
|
'visa_status' => 'Transferable',
|
|
];
|
|
|
|
return Inertia::render('Employer/Workers/Show', [
|
|
'worker' => $worker,
|
|
]);
|
|
}
|
|
}
|