<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\NotificationAbsenceRequest;
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\NotificationAbsence;
use App\SystemFood;
use App\SystemFoodDetail;

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

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

    /**
     * return data of the notificationabsences.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $query = NotificationAbsence::orderBy('id', 'DESC');

        if ($request->date_from) {
            $query->whereDate('created_at', '>=', $request->date_from);
        }

        if ($request->date_to) {
            $query->whereDate('created_at', '<=', $request->date_to);
        }

        if ($request->name) {
            $query->where('name', 'LIKE', '%'.$request->name.'%');
        }


        if ( !is_null($request->status) && $request->status == 0) {
            $query->onlyTrashed();
        }

        $notificationabsences= $query->paginate(100);

        return view($this->viewDirectory.'grid',compact('notificationabsences'));
    }

   
   
    public function edit(NotificationAbsence $notificationabsence): View
    {
        $action= route('notificationabsences.update', $notificationabsence->id);
        $head = metaFields('notificationabsences', 'edit', getCurrentLocale()) ?? __('notificationabsences.edit');
        
        


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

    /**
     * Update the specified resource in storage.
     *
     * @param  NotificationAbsenceRequest  $request
     * @param  NotificationAbsence  $systemfood
     * @return RedirectResponse|Redirector
     */
        public function update(NotificationAbsenceRequest $request, NotificationAbsence $notificationabsence) :RedirectResponse
    {
      
        $notificationabsence->update($request->all());

        return redirect($this->redirectSuccessPath)->with('message', __('dashboard.saveDone'));
    }

}