<?php

namespace App\Http\Controllers\Admin;

/** Ajax Request */

use App\City;
use App\DocumentationRequest;
use App\DocumentationType;
use App\Http\Requests\DocumentationRequests;
use Exception;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;

class DocumentationRequestController extends Controller
{

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

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

    /**
     * return data of the documentationRequests.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $query = DocumentationRequest::orderByDesc('id');

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

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

        if ($request->type) {
            $query->where('documentation_requests.type', $request->type);
        }

        if ($request->documentation_status) {
            $query->where('documentation_requests.status', $request->documentation_status);
        }

        if ($request->consulting_date) {
            $query->whereDate('documentation_requests.consulting_date', '=', $request->consulting_date);
        }

        if ($request->consulting_time) {
            $query->where('documentation_requests.consulting_time', '=', $request->consulting_time);
        }

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

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

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


    /**
     * Show the form for editing the specified service.
     *
     * @param DocumentationRequest $documentationRequest
     * @return Factory|RedirectResponse|View
     */
    public function edit(DocumentationRequest $documentationRequest)
    {
//        if ($documentationRequest->status != 'active'){
//            return redirect()->route('documentationRequests.index');
//        }
        $action= route('documentationRequests.update', $documentationRequest->id);
        $head = metaFields('documentationRequests', 'edit', getCurrentLocale()) ?? __('documentationRequests.edit');
        $local = getCurrentLocale();
        $documentationType = DocumentationType::withDescription($documentationRequest->documentation_type_id);
        $city = $documentationRequest->city_id ? City::withDescription($documentationRequest->city_id) : null;
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }


    public function activeDocumentation(DocumentationRequest $documentationRequest)
    {
        $documentationRequest->update(['status' => 'documented']);
        return response()->json(['message' => __('documentationRequests.documented_success')]);

    }

    /**
     * Update the specified service in storage.
     * @param DocumentationRequests $request
     * @param DocumentationRequest $documentationRequest
     * @return JsonResponse
     */
    public function update(DocumentationRequests $request, DocumentationRequest $documentationRequest) :JsonResponse
    {
        $data = $request->all();
        $documentationRequest->update($data);
        session()->flash('message', __('dashboard.saveDone'));
        return response()->json(['url' => route('documentationRequests.index')]);
//        return redirect($this->redirectSuccessPath)->with('message', __('dashboard.saveDone'));
    }




    /**
     * Remove the specified service from storage.
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $consulting= DocumentationRequest::withTrashed()->find($id);
        if ($consulting) {

            if ($consulting->deleted_at) {
                $consulting->forceDelete();
            } else {
                $consulting->delete();
            }
            return response()->json(['message'=> __('dashboard.deletedDone')]);
        } else{
            return response()->json(['message'=> __('dashboard.noResult')], 400);
        }
    }

    /**
     * Remove several documentationRequests by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $ids= $request->ids;

        if ($request->force) {
            DocumentationRequest::onlyTrashed()->whereIn('id', $ids)->forceDelete();
        } else {
            DocumentationRequest::whereIn('id', $ids)->delete();
        }

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

    /**
     * Restore the specified service from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function restore(Request $request, int $id) :JsonResponse
    {
        $consulting= DocumentationRequest::withTrashed()->find($id);
        if ($consulting) {
            $consulting->restore();
            return response()->json(['message'=> __('dashboard.saveDone')]);
        }
        return response()->json(['message'=> __('dashboard.noResult')], 400);

    }

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