<?php

namespace Modules\Quotation\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\Quotation\Models\Quotation;
use Modules\Quotation\Requests\Admin\QuotationRequest;
use Modules\Quotation\Services\QuotationService;
use Modules\Setting\Models\Setting;

class QuotationController extends Controller
{

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

    public function __construct(QuotationService $quotationService)
    {
        $this->quotationService = $quotationService;
    }

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

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

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

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

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

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

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

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

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

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

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

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