<?php

namespace App\Livewire;

use App\Models\SiteBackground;
use Illuminate\Support\Carbon;
use Livewire\Attributes\Url;
use Livewire\Component;
use Livewire\WithPagination;

class BackgroundImages extends Component
{
    use WithPagination;

    #[Url(history: true)]
    public $filter = 'all';

    public function render()
    {
        $now = Carbon::now();

        $images = SiteBackground::when($this->filter === 'active', fn ($query) => $query->whereDate('date_start', '<=', $now)->whereDate('date_end', '>=', $now)
        )
            ->when($this->filter === 'inactive', fn ($query) => $query->whereDate('date_start', '>', $now)->orWhereDate('date_end', '<', $now)
            )
            ->paginate(10);

        return view('livewire.background-images', [
            'images' => $images,
        ]);
    }
}
