<?php

namespace App\Http\Controllers\Admin;

/** Ajax Request */

use App\Exports\MemberExport;
use App\Http\Requests\Admin\DeliveryRequest;
use App\Models\User;
use App\Services\Interfaces\DeliveryRepositoryInterface;
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 Maatwebsite\Excel\Facades\Excel;

class DeliveryController extends Controller
{

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

    public function __construct(DeliveryRepositoryInterface $deliveryRepository)
    {
        $this->deliveryRepository = $deliveryRepository;
    }

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

    /**
     * return data of the deliveries.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) //:View
    {
        $request->merge(['all_info'=> true]);
        if (isset($request->export)) {
            $deliveries= $this->deliveryRepository->get($request);
            return Excel::download(new MemberExport($deliveries, 'deliveries'), 'Members.xlsx');
        }

        $request->merge(['paginate'=> true]);
        $deliveries= $this->deliveryRepository->get($request);

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

    public function fetchDeliveries(Request $request)
    {
        $members= $this->deliveryRepository->get($request);
        return $members;
    }

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

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

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

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

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

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

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

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

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