<?php

namespace App\Http\Livewire;

use Livewire\Component;

class NotificationsDeliveryController extends Component
{
    public $notifications, $count, $count_all;

    public function mount()
    {
        $this->notification();
    }

    protected $listeners = [
        'notification'
    ];
    //el evento notification se dispara cuando llega una notificación nueva, usando el servicio pusher
    public function notification()
    {
        $this->notifications = auth()->user()->notifications->take(5);
        $this->count = auth()->user()->unreadNotifications->count();
        $this->count_all = auth()->user()->notifications->count();
    }

    public function render()
    {
        return view('livewire.notifications.delivery.component');
    }

    public function markAllNotificationsAsRead()
    {
        auth()->user()->notification = 0;
        auth()->user()->save();
    }

    public function markNotificationAsRead($id)
    {
        $notification = auth()->user()->notifications()->findOrFail($id);
        $notification->markAsRead();
        $this->count = auth()->user()->unreadNotifications->count();
    }



}
