<?php

namespace App\Http\Controllers\Admin;


use App\CompanyRequest;
use App\CompanyRequestTopic;
use App\Http\Requests\UpdateCaseFilesRequest;
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 CompanyRequestController extends Controller
{

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

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


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

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

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

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

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

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

        if ($request->phone) {
            $query->where('company_requests.phone', 'LIKE', '%'.$request->phone.'%');
        }
        if ($request->request_no) {
            $query->where('company_requests.id', 'LIKE', '%'.$request->request_no.'%');
        }

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

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

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


    /**
     * Show the form for editing the specified service.
     *
     * @param CompanyRequest $companyRequest
     * @return Factory|RedirectResponse|View
     */
    public function edit(CompanyRequest $companyRequest)
    {
//        $action = route('companyRequests.update', $companyRequest->id);
        $head = metaFields('companyRequests', 'edit', getCurrentLocale()) ?? __('companyRequests.edit');
        $title = $head;
        $requestTopic = CompanyRequestTopic::withDescription($companyRequest->company_request_topic_id);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified resource in storage.
     *
     * @param UpdateCaseFilesRequest $request
     * @param CompanyRequest $companyRequest
     * @return JsonResponse
     */
//    public function update(UpdateCaseFilesRequest $request, CompanyRequest $companyRequest)
//    {
//        CaseUpdate::where('company_request_id', $companyRequest->id)->delete();
//
//        foreach ($request['files'] as $key => $file) {
//            $companyUpdate = CaseUpdate::create([
//                'file' => $file,
//                'user_id' => $companyRequest->user_id,
//                'company_request_id' => $companyRequest->id,
//            ]);
//
//            foreach (languages() as $lang) {
//                $data=[
//                    'file_name'=> $request['file_name_'.$lang->local][$key],
//                    'company_update_id'=> $companyUpdate->id,
//                    'language_id'=> $lang->id,
//                ];
//                CaseUpdateDescription::create($data);
//            }
//
//        }
//
//        session()->flash('message', __('dashboard.saveDone'));
//        return response()->json(['url' => route('companyRequests.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= CompanyRequest::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 companyRequests by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $ids= $request->ids;

        if ($request->force) {
            CompanyRequest::onlyTrashed()->whereIn('id', $ids)->forceDelete();
        } else {
            CompanyRequest::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= CompanyRequest::withTrashed()->find($id);
        if ($consulting) {
            $consulting->restore();
            return response()->json(['message'=> __('dashboard.saveDone')]);
        }
        return response()->json(['message'=> __('dashboard.noResult')], 400);

    }

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