<?php

namespace Modules\Store\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\Option\Enums\OptionStatusEnum;
use Modules\Option\Services\OptionService;
use Modules\Store\Models\EditStore;
use Modules\Store\Requests\Admin\EditStoreRequest;
use Modules\Store\Services\EditStoreService;

class EditStoreController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/edit_stores';
    /** View folder */
    private $viewDirectory= 'admin.edit_stores.';
    //
    private EditStoreService $editStoreService;
    private OptionService $optionService;

    public function __construct(EditStoreService $editStoreService,OptionService $optionService)
    {
        $this->editStoreService = $editStoreService;
        $this->optionService = $optionService;
    }

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

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

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


    /**
     * Show the form for editing the specified EditStore.
     *
     * @param  EditStore  $editStore
     * @return View
     */
    public function edit(Request $request,EditStore $editStore) :View
    {
        $head = __('edit_stores.edit');
        $action= route('edit_stores.update', $editStore->id);
        $edit_store= $this->editStoreService->getItem($editStore);
        $request->merge(['type'=>OptionStatusEnum::REASON_REFUSE_EDIT->value]);
        $reasons = $this->optionService->get($request);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified EditStore in storage.
     * @param EditStoreRequest $request
     * @param EditStore $editStore
     * @return RedirectResponse|Redirector
     */
    public function update(EditStoreRequest $request, EditStore $editStore) :JsonResponse
    {
        $data = $editStore->toArray();
        $data['status']= $request->status;
        $data['reason_id']= $request->reason_id;
        $data['message']= $request->message;
        unset($data['created_at']);
        unset($data['updated_at']);
        unset($data['deleted_at']);
        unset($data['store_id']);
        $this->editStoreService->save($data,$editStore);


        $this->editStoreService->destroy($request,$editStore->id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('edit_stores.index'),
        ];
        return ResponseService::response($response);
    }

}
