<?php

namespace App\Http\Controllers\Admin;

use App\Consultation;
use App\Consulting;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;

class ConsultationController extends Controller
{

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

    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= __('consultations.head');
        $request= $request->toArray();
        return view($this->viewDirectory.'index',compact('title'));
    }

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

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

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

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

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

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

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


    /**
     * Show the form for editing the specified resource.
     *
     * @param Consultation $consultation
     * @return View
     */
    public function edit(Consultation $consultation) :View
    {
        $head = metaFields('consultations', 'edit', getCurrentLocale()) ?? __('consultations.edit');
        $local = getCurrentLocale();
        $consultationType = Consulting::withDescription($consultation->consulting_id);
        return view( $this->viewDirectory.'form', compact('consultation', 'consultationType', 'local', 'head') );
    }

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

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

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

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