<?php

namespace Modules\Rate\Controllers\Warehouse;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Modules\Rate\Requests\Admin\RateRequest;
use Modules\Rate\Services\RateService;
use Modules\Currency\Services\CurrencyService;

class RateController extends Controller
{

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

    public function __construct(
        private RateService $rateService,
    )
    {}

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

    /**
     * return data of the rates.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $request->merge(['paginate'=>true,'warehouse_id'=>auth()->user()->warehouse_id]);
        $rates= $this->rateService->get($request->all());

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


    /**
     * Show the form for editing the specified Rate.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $rate = $this->rateService->getItem($id);
        if (!$rate)
        {
            abort(404);
        }
        $head= __('rates.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }



    /**
     * Remove the specified Rate from storage.
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(int $id) :JsonResponse
    {
        $this->rateService->destroy($id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route($this->viewDirectory.'index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove several rates by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $this->rateService->destroyAll([
            'ids' => $request->ids,
            'status' => 1
        ]);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route($this->viewDirectory.'index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Restore the specified Rate from storage.
     *
     * @param int $id
     * @return JsonResponse
     */
    public function restore(int $id) :JsonResponse
    {
        $this->rateService->destroy($id);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route($this->viewDirectory.'index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Restore several rates by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $this->rateService->destroyAll([
            'ids' => $request->ids,
            'status' => 0
        ]);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route($this->viewDirectory.'index'),
        ];
        return ResponseService::response($response);
    }
}
