<?php

namespace App\Http\Controllers\Admin;

use App\ConsultingEvaluate;
use App\User;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;

class ConsultingEvaluateController extends Controller
{

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

    public function __construct()
    {
        $this->redirectSuccessPath = getCurrentLocale().$this->redirectSuccessPath;
    }

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

    /**
     * return data of the pages.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $query = ConsultingEvaluate::orderBy('consulting_evaluates.id', 'DESC')
        ->join('users', 'users.id', 'consulting_evaluates.user_id')
        ->select(['consulting_evaluates.*', 'users.name']);

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

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

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

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

        if ($request->eval_status == 'active'){
            $query->where('consulting_evaluates.status', 1);
        }

        if ($request->eval_status == 'non_active'){
            $query->where('consulting_evaluates.status', 0);
        }


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

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

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param ConsultingEvaluate $consultingEvaluate
     * @return View
     */
    public function edit(ConsultingEvaluate $consultingEvaluate) :View
    {
        $head = __('consultingEvaluates.edit');
        $title = $head;
        $languageId = currentLanguage()->id;
        $consultingEvaluate = ConsultingEvaluate::where('consulting_evaluates.id', $consultingEvaluate->id)
            ->join('consulting_requests', 'consulting_requests.id', 'consulting_evaluates.consulting_request_id')
        ->select([
            'consulting_evaluates.*',
            'consulting_requests.user_name',
            'consulting_requests.user_phone',
            'consulting_requests.user_email',
            'consulting_requests.consulting_type',
            'consulting_requests.consulting_way',
            'consulting_requests.consulting_date',
            'consulting_requests.consulting_time',
        ])->first();

        return view( $this->viewDirectory.'form', get_defined_vars());
    }


    public function activeEvaluate(ConsultingEvaluate $consultingEvaluate)
    {
        if ($consultingEvaluate->status){
            $consultingEvaluate->update(['status' => 0]);
            return response()->json(['message' => __('consultingEvaluates.de_active_success')]);
        } else {
            $consultingEvaluate->update(['status' => 1]);
            return response()->json(['message' => __('consultingEvaluates.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
    {
        $consultingEvaluate = ConsultingEvaluate::withTrashed()->find($id);
        if ($consultingEvaluate) {
            if ($consultingEvaluate->deleted_at) {
                $consultingEvaluate->forceDelete();
            } else {
                $consultingEvaluate->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) {
            ConsultingEvaluate::onlyTrashed()->whereIn('id', $ids)->forceDelete();
        } else {
            ConsultingEvaluate::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
    {
        $consultingEvaluate = ConsultingEvaluate::withTrashed()->find($id);

        if ($consultingEvaluate){
            $consultingEvaluate->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
    {
        $consultingEvaluates = ConsultingEvaluate::whereIn('id', $request->ids)->onlyTrashed();
        if ($consultingEvaluates){
            $consultingEvaluates->restore();
            return response()->json(['message'=> __('dashboard.saveDone')]);
        }
    }
}
