131 lines
4.8 KiB
PHP
131 lines
4.8 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\Shortlist;
|
|
use App\Models\Conversation;
|
|
use App\Models\Message;
|
|
use App\Models\Announcement;
|
|
use Carbon\Carbon;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
// Resolve current employer user
|
|
$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',
|
|
]]);
|
|
}
|
|
} else {
|
|
$user = User::find($sessId);
|
|
}
|
|
|
|
// Fallback user if DB has no users (precaution)
|
|
if (!$user) {
|
|
$user = new User([
|
|
'id' => 2,
|
|
'name' => 'John Doe',
|
|
'subscription_status' => 'active',
|
|
'subscription_expires_at' => now()->addDays(24),
|
|
]);
|
|
}
|
|
|
|
// Get subscription expires at date
|
|
$sub = $user->subscription()->where('status', 'active')->latest()->first();
|
|
$expiresAt = $sub ? $sub->expires_at : now()->addDays(24);
|
|
$daysRemaining = $expiresAt ? max(0, now()->diffInDays($expiresAt, false)) : 30;
|
|
|
|
// 1. Stats
|
|
$shortlistedCount = Shortlist::where('employer_id', $user->id)->count();
|
|
$messagesSent = Message::where('sender_id', $user->id)->where('sender_type', 'employer')->count();
|
|
|
|
$stats = [
|
|
'shortlisted_count' => $shortlistedCount,
|
|
'messages_sent' => $messagesSent,
|
|
'profile_views_given' => 45, // Static/mock analytic metric for page visits
|
|
'days_remaining' => (int)$daysRemaining,
|
|
];
|
|
|
|
// 2. Shortlisted workers
|
|
$shortlists = Shortlist::where('employer_id', $user->id)
|
|
->with(['worker.category', 'worker.skills'])
|
|
->get();
|
|
|
|
$shortlistedWorkers = $shortlists->map(function ($s) {
|
|
$w = $s->worker;
|
|
if (!$w) return null;
|
|
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,
|
|
'photo_url' => null,
|
|
'verified' => (bool)$w->verified,
|
|
];
|
|
})->filter()->values()->toArray();
|
|
|
|
// 3. Recent messages
|
|
$dbConversations = Conversation::where('employer_id', $user->id)
|
|
->with(['worker', 'messages' => function ($q) {
|
|
$q->latest();
|
|
}])
|
|
->get();
|
|
|
|
$recentMessages = $dbConversations->map(function ($conv) {
|
|
$lastMsg = $conv->messages->first();
|
|
$worker = $conv->worker;
|
|
if (!$worker || !$lastMsg) return null;
|
|
|
|
return [
|
|
'id' => $conv->id,
|
|
'worker_name' => $worker->name,
|
|
'last_message' => $lastMsg->text,
|
|
'unread' => $lastMsg->sender_type === 'worker' && is_null($lastMsg->read_at),
|
|
'sent_at' => $lastMsg->created_at->diffForHumans(),
|
|
'timestamp' => $lastMsg->created_at,
|
|
];
|
|
})->filter()->sortByDesc('timestamp')->values()->toArray();
|
|
|
|
// 4. Announcements
|
|
$dbAnnouncements = Announcement::latest()->limit(5)->get();
|
|
$announcements = $dbAnnouncements->map(function ($ann) {
|
|
return [
|
|
'id' => $ann->id,
|
|
'title' => $ann->title,
|
|
'body' => $ann->body,
|
|
'created_at' => $ann->created_at->format('M d, Y'),
|
|
];
|
|
})->toArray();
|
|
|
|
return Inertia::render('Employer/Dashboard', [
|
|
'employer' => [
|
|
'name' => $user->name,
|
|
'subscription_status' => $sub ? $sub->status : 'active',
|
|
'subscription_expires_at' => $expiresAt ? $expiresAt->format('Y-m-d') : '2026-12-31',
|
|
'plan_name' => $sub ? (ucfirst($sub->plan_id) . ' Pass') : 'Premium Employer Pass',
|
|
],
|
|
'stats' => $stats,
|
|
'shortlisted_workers' => $shortlistedWorkers,
|
|
'recent_messages' => array_slice($recentMessages, 0, 5),
|
|
'announcements' => $announcements,
|
|
]);
|
|
}
|
|
}
|