<?php

namespace App\Http\Controllers\Admin;

use App\Reservation;
use Exception;
use App\Language;
use App\Reason;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Requests\OfflineRequest;
use Illuminate\Routing\Redirector;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class OfflineController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/offlines';
    /** View folder */
    private $viewDirectory= 'admin.offlines.';

    /**
     * Display a listing of the resource.
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $action= route('offlines.store');
        $head= __('offlines.new');
         $reasons = Reason::cursor();
        return view($this->viewDirectory.'index',compact('head','action','reasons'));
    }

    /**
     * return data of the offlines.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $query = Reservation::where('type','free')->latest();

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

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

        if ($request->date) {
            $query->where('date',$request->date);
        }
        if ($request->time) {
            $query->where('time',$request->time);
        }

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

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

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

    /**
     * Show the form for creating a new resource.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('offlines.store');
        $head= __('offlines.new');
        $reasons = Reason::cursor();
        return view( $this->viewDirectory.'form', compact('action', 'head','reasons') );
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param OfflineRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(OfflineRequest $request) :RedirectResponse
    {
        $request->merge(['type'=>'reserved']);
         Reservation::create($request->all());
        // Insert Description

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


    /**
     * Show the form for editing the specified resource.
     *
     * @param Reservation $offline
     * @return View
     */
    public function edit(Reservation $offline) :View
    {
        $action= route('offlines.update', $offline->id);
        $head= __('offlines.edit');
        $reasons = Reason::cursor();
       

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

    /**
     * Update the specified resource in storage.
     *
     * @param OfflineRequest $request
     * @param Reservation $offline
     * @return RedirectResponse|Redirector
     */
    public function update(OfflineRequest $request, Reservation $offline) :RedirectResponse
    {
        // Update image if request has it
        $requst->merge(['type'=>'reserved']);
        $offline->update($request->all());


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

    

    /**
     * Remove the specified resource from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $offline = Reservation::withTrashed()->find($id);
        if ($offline) {
            if ($offline->deleted_at) {
                $offline->forceDelete();
            } else {
                $offline->delete();
            }
            return response()->json(['message'=> __('dashboard.deletedDone')]);
        } else{
            return response()->json(['message'=> __('dashboard.noResult')], 400);
        }
    }

    /**
     * Remove several offlines by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $ids= $request->ids;
        if ($request->force) {
            Reservation::onlyTrashed()->whereIn('id', $ids)->forceDelete();
        } else {
            Reservation::whereIn('id', $ids)->delete();
        }

        return response()->json(['message'=> __('dashboard.deletedDone')]);
    }

    /**
     * Restore the specified category from storage
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function restore(Request $request, int $id) :JsonResponse
    {
        $offline = Reservation::withTrashed()->find($id);

        if ($offline){
            $offline->restore();
            return response()->json(['message'=> __('dashboard.saveDone')]);
        }
        return response()->json(['message'=> __('dashboard.noResult')], 400);
    }

    /**
     * Restore several offlines by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $offlines = Reservation::whereIn('id', $request->ids)->onlyTrashed();
        if ($offlines){
            $offlines->restore();
            return response()->json(['message'=> __('dashboard.saveDone')]);
        }
    }
}