<?php

namespace App\Livewire;

use Livewire\Component;
use Livewire\WithPagination;
use App\Models\CampBooking;
use Illuminate\Support\Facades\Auth;

class CampApplications extends Component
{
    use WithPagination;

    public $status;
    public $bookingId;

    protected $listeners = ['updateStatus'];

    public function mount()
    {
        $this->status = '';
        $this->bookingId = null;
    }

    public function updateStatus($bookingId, $newStatus)
    {
        if (Auth::user()->is_admin) {
            $booking = CampBooking::findOrFail($bookingId);
            $booking->update(['status' => $newStatus]);
            $this->resetPage(); // Refresh the pagination to reflect changes
        }
    }

    public function render()
    {
        $user = Auth::user();
        $applicationsQuery = CampBooking::with('camp_package')->latest();

        if (!$user->is_admin) {
            $applicationsQuery->where('parent_email', $user->email);
        }

        $applications = $applicationsQuery->paginate(10);

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