<?php

namespace App\Http\Controllers\Application;
use Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class NotificationController extends Controller
{
    public function __construct()
    {
        $this->middleware('admin');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function index()
    {
        $user = Auth::guard('admin')->user(); 
        return view('application.notifications.notifications');
    }

    public function partial()
    {
        $user = Auth::guard('admin')->user();
        $notifications = $user->notifications()->paginate(30);  

        return view('application.notifications.notification_partial',compact('notifications'));
    }

    public function markAsRead(Request $request)
    {
        $user = Auth::user();
        $notification = $user->unreadNotifications()->where('id',$request->id)->first();
        if ($notification){
            $notification->markAsRead();
        }
        echo 'success';
    }

    public function markAllAsRead()
    {
        $user = Auth::user();
        $notifications = $user->unreadNotifications()->get();
        foreach ($notifications as $key => $notification) {
            $notification->markAsRead();
        }

        return redirect()->back();
    }
}
