<?php

namespace App\Livewire;

use Livewire\Component;
use Livewire\WithPagination;
use Livewire\Attributes\Url;

use App\Models\SiteBackground;
use Illuminate\Support\Carbon;

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
        ]);
    }
}
