<?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\ReservationRequest;
use Illuminate\Routing\Redirector;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class HoldController extends Controller
{

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

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

    /**
     * return data of the holds.
     *
     * @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->name) {
            $query->where('name', 'LIKE', '%'.$request->name.'%');
        }
        if ($request->email) {
            $query->where('email','LIKE', '%'.$request->email.'%');
        }
        if ($request->phone) {
            $query->where('phone',$request->phone);
        }
        if ($request->date) {
            $query->where('date',$request->date);
        }
        if ($request->time) {
            $query->where('time',$request->time);
        }
        if ($request->reason) {
            $query->where('reason_id',$request->reason);
        }
         if ($request->type) {
            $query->where('type',$request->type);
        }

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

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

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

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

    /**
     * Store a newly created resource in storage.
     *
     * @param ReservationRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(ReservationRequest $request) :RedirectResponse
    {
        $hold = Reservation::create($request->all());
        // Insert Description

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


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

       $reasons = Reason::cursor();

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

    /**
     * Update the specified resource in storage.
     *
     * @param ReservationRequest $request
     * @param Reservation $hold
     * @return RedirectResponse|Redirector
     */
    public function update(ReservationRequest $request, Reservation $hold) :RedirectResponse
    {
        // Update image if request has it
        $hold->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
    {
        $hold = Reservation::withTrashed()->find($id);
        if ($hold) {
            $hold->update(['type'=>'absent']);
            return response()->json(['message'=> __('dashboard.saveDone')]);
        } else{
            return response()->json(['message'=> __('dashboard.noResult')], 400);
        }
    }

    /**
     * Remove several holds 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
    {
        $hold = Reservation::withTrashed()->find($id);

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

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