<?php

namespace Modules\Partner\Controllers\Admin;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use Modules\Partner\Models\Partner;
use Modules\Partner\Requests\Admin\PartnerRequest;
use Modules\Partner\Services\PartnerService;

class PartnerController extends Controller
{

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

    public function __construct(PartnerService $partnerService)
    {
        $this->partnerService = $partnerService;
    }

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

    /**
     * return data of the partners.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) //:View
    {
        $request->merge(['paginate'=> true]);
        $partners= $this->partnerService->get($request);

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

    /**
     * Show the form for creating a new Partner.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('partners.store');
        $head= __('partners.new');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Store a newly created Partner in storage.
     * @param PartnerRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(PartnerRequest $request) :JsonResponse
    {
        $partner= $this->partnerService->save($request, null);

        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('partners.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Show the form for editing the specified Partner.
     *
     * @param  Partner  $partner
     * @return View
     */
    public function edit(Partner $partner) :View
    {
        // dd($partner->id);
        $action= route('partners.update', $partner->id);
        $data= $this->partnerService->getItem($partner);
        $head= __('partners.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified Partner in storage.
     * @param PartnerRequest $request
     * @param Partner $partner
     * @return RedirectResponse|Redirector
     */
    public function update(PartnerRequest $request, Partner $partner) :JsonResponse
    {
        $partner= $this->partnerService->save($request, $partner);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('partners.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove the specified Partner from storage.
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $return= $this->partnerService->destroy($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Remove several partners by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $return= $this->partnerService->destroyAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore the specified Partner from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function restore(Request $request, int $id) :JsonResponse
    {
        $return= $this->partnerService->restore($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore several partners by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $return= $this->partnerService->restoreAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }
}
