127 lines
3.7 KiB
PHP
127 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Models\ActivityLog;
|
|
use App\Models\Booking;
|
|
use App\Models\Comment;
|
|
use App\Models\Flight;
|
|
use App\Models\SiteVisit;
|
|
use App\Models\Tour;
|
|
use App\Models\User;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$bookingsCount = Booking::count();
|
|
$flightsCount = Flight::count();
|
|
$toursCount = Tour::count();
|
|
$commentsCount = Comment::count();
|
|
$usersCount = User::count();
|
|
$viewsCount = SiteVisit::count();
|
|
|
|
$days = collect(range(9, 0))->map(
|
|
fn ($i) => Carbon::today()->subDays($i)
|
|
);
|
|
|
|
$visitsRaw = SiteVisit::selectRaw('DATE(created_at) as visit_date, COUNT(*) as total')
|
|
->where('created_at', '>=', Carbon::today()->subDays(9)->startOfDay())
|
|
->groupBy('visit_date')
|
|
->pluck('total', 'visit_date');
|
|
|
|
$visitsChartLabels = $days
|
|
->map(fn ($d) => $d->locale('fa')->translatedFormat('d M'))
|
|
->toArray();
|
|
|
|
$visitsChartData = $days
|
|
->map(fn ($d) => (int) ($visitsRaw[$d->toDateString()] ?? 0))
|
|
->toArray();
|
|
|
|
$visitsChartOptions = [
|
|
'chart' => [
|
|
'type' => 'bar',
|
|
'height' => 320,
|
|
'toolbar' => ['show' => false],
|
|
'fontFamily' => 'inherit',
|
|
],
|
|
'series' => [
|
|
[
|
|
'name' => 'بازدید',
|
|
'data' => $visitsChartData,
|
|
],
|
|
],
|
|
'xaxis' => [
|
|
'categories' => $visitsChartLabels,
|
|
'labels' => [
|
|
'style' => ['colors' => '#8c9097'],
|
|
],
|
|
],
|
|
'yaxis' => [
|
|
'labels' => [
|
|
'style' => ['colors' => '#8c9097'],
|
|
],
|
|
],
|
|
'plotOptions' => [
|
|
'bar' => [
|
|
'borderRadius' => 6,
|
|
'columnWidth' => '45%',
|
|
'distributed' => false,
|
|
],
|
|
],
|
|
'fill' => [
|
|
'type' => 'gradient',
|
|
'gradient' => [
|
|
'shade' => 'light',
|
|
'type' => 'vertical',
|
|
'shadeIntensity' => 0.4,
|
|
'gradientToColors' => ['#8b5cf6'],
|
|
'opacityFrom' => 1,
|
|
'opacityTo' => 0.85,
|
|
'stops' => [0, 100],
|
|
],
|
|
],
|
|
'colors' => ['#5B93FF'],
|
|
'dataLabels' => ['enabled' => false],
|
|
'grid' => [
|
|
'strokeDashArray' => 4,
|
|
'borderColor' => '#e8e8e8',
|
|
],
|
|
'tooltip' => [
|
|
'theme' => 'light',
|
|
],
|
|
];
|
|
|
|
$recentLogs = ActivityLog::query()
|
|
->whereIn('model_type', [Tour::class, Flight::class])
|
|
->with('admin')
|
|
->orderByDesc('created_at')
|
|
->limit(10)
|
|
->get()
|
|
->map(function ($log) {
|
|
|
|
$modelClass = $log->model_type;
|
|
$relatedModel = $modelClass::find($log->model_id);
|
|
|
|
$log->model_title = $relatedModel->title ?? '(حذف شده)';
|
|
$log->model_label = $log->model_type === Tour::class ? 'تور' : 'پرواز';
|
|
|
|
return $log;
|
|
});
|
|
|
|
return view('admin.dashboard', compact(
|
|
'bookingsCount',
|
|
'flightsCount',
|
|
'toursCount',
|
|
'commentsCount',
|
|
'usersCount',
|
|
'viewsCount',
|
|
'visitsChartOptions',
|
|
'recentLogs',
|
|
));
|
|
}
|
|
}
|