<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\MealRequest;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\View\View;

use App\User;
use App\Notification;
use App\NotificationForward;
use App\SystemFood;
use App\SystemFoodDetail;
use App\TaskFile;
use App\Task;

class NotificationController extends Controller
{
    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/notifications';
    /** View folder */
    private $viewDirectory= 'admin.notifications.';

    /**
     * Display a listing of the resource.
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('notifications.head');
        $request= $request->toArray();
        return view($this->viewDirectory.'index',compact('title'));
    }

    /**
     * return data of the notifications.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $notifis = NotificationForward::where('forward_id',\Auth::user()->id)->pluck('notification_id');
        $query = Notification::whereIn('id',$notifis)->orderBy('id', 'DESC');
        
        
        if ($request->title) {
            $query->where('title',$request->title);
        }


        $notifications= $query->paginate(100);
        
        return view($this->viewDirectory.'grid',compact('notifications'));
    }
 public function edit(Notification $notification): View
    {
       $forward = NotificationForward::where('notification_id',$notification->id)->where('forward_id',\Auth::user()->id)->first();
       $forward->update(['seen'=>'1']);
       $forward->save();
        $head =  __('notifications.seen');
        if($notification->type == '1'){
            $task = Task::find($notification->description);
            $task_photos = TaskFile::where('task_id',$task->id)->where('type','0')->get();
            $task_files = TaskFile::where('task_id',$task->id)->where('type','1')->get();
        return view( $this->viewDirectory.'form', compact('notification', 'head','task','task_photos','task_files') );
        
        }

        return view( $this->viewDirectory.'form', compact('notification', 'head') );
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return View
     */
    
}
