<?php

namespace App\Http\Controllers\Admin;

use App\Course;
use App\Evaluate;
use App\Hotel;
use App\Product;
use App\User;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;

class EvaluateController extends Controller
{

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

    /**
     * Display a listing of the resource.
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('evaluates.head');
        $request= $request->toArray();
//        $hotels = Hotel::whereHas('evaluates')
//            ->join('hotel_descriptions as hotel_desc', 'hotels.id', 'hotel_desc.hotel_id')
//            ->where('hotel_desc.language_id', currentLanguage()->id)
//            ->select(['hotels.id', 'hotel_desc.name'])->cursor();

        $users = User::whereHas('evaluates')->where('role', 'user')->cursor();

        return view($this->viewDirectory.'index',compact('title', 'users'));
    }

    /**
     * return data of the pages.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $languageId = currentLanguage()->id;
        $query = Evaluate::join('users', 'users.id', 'evaluates.user_id')
//        ->join('hotels', 'hotels.id', 'evaluates.hotel_id')
//        ->join('hotel_descriptions as hotel_desc', 'hotels.id', 'hotel_desc.hotel_id')
//        ->where('hotel_desc.language_id', $languageId)
//
//        ->join('umrah_trips', 'umrah_trips.id', 'evaluates.umrah_trip_id')
//        ->join('umrah_trip_descriptions as umrah_trip_desc', 'umrah_trips.id', 'umrah_trip_desc.umrah_trip_id')
//        ->where('umrah_trip_desc.language_id', $languageId)
        ->select(
            [
                'evaluates.*',
                'users.name as user_name',
                'users.id as user_id',
//                'hotels.id as hotel_id',
//                'hotel_desc.name as hotel_name',
//                'umrah_trip_desc.name as umrah_name',

            ]);


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

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

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

        if ($request->user_id) {
            $query->where('users.id', $request->user_id);
        }

        if ($request->email) {
            $query->where('users.email', $request->email);
        }

//        if ($request->hotel_id) {
//            $query->where('hotels.id', $request->hotel_id);
//        }


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

        $evaluates = $query->paginate(10);

        return view($this->viewDirectory.'grid',compact('evaluates'));
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param Evaluate $evaluate
     * @return View
     */
    public function edit(Evaluate $evaluate) :View
    {
        $head = __('evaluates.comment_details');
        $languageId = currentLanguage()->id;
        $myEvaluate = Evaluate::where('evaluates.id', $evaluate->id)
            ->join('users', 'users.id', 'evaluates.user_id');

        if ($evaluate->hotel_id){
            $myEvaluate->join('hotels', 'hotels.id', 'evaluates.hotel_id')
            ->join('hotel_descriptions as hotel_desc', 'hotels.id', 'hotel_desc.hotel_id')
            ->where('hotel_desc.language_id', $languageId)
                ->select(
                    [
                        'evaluates.*',
                        'users.name as user_name',
                        'users.email as user_email',
                        'users.phone as user_phone',
                        'users.id as user_id',
                        'hotels.id as hotel_id',
                        'hotel_desc.name as hotel_name',
                    ]);
        }
        if ($evaluate->umrah_trip_id){
            $myEvaluate->join('umrah_trips', 'umrah_trips.id', 'evaluates.umrah_trip_id')
            ->join('umrah_trip_descriptions as umrah_trip_desc', 'umrah_trips.id', 'umrah_trip_desc.umrah_trip_id')
            ->where('umrah_trip_desc.language_id', $languageId)->select(
                    [
                        'evaluates.*',
                        'users.name as user_name',
                        'users.email as user_email',
                        'users.phone as user_phone',
                        'users.id as user_id',
                        'umrah_trip_desc.name as umrah_name',
                    ]);
        }
        $evaluate = $myEvaluate->first();

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


    public function activeEvaluate(Evaluate $evaluate)
    {
        if ($evaluate->status){
            $evaluate->update(['status' => 0]);
            return response()->json(['message' => __('evaluates.de_active_success')]);
        } else {
            $evaluate->update(['status' => 1]);
            return response()->json(['message' => __('evaluates.active_success')]);
        }

    }


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

    /**
     * Remove several pages by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $ids= $request->ids;
        if ($request->force) {
            Evaluate::onlyTrashed()->whereIn('id', $ids)->forceDelete();
        } else {
            Evaluate::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
    {
        $evaluate = Evaluate::withTrashed()->find($id);

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

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