<?php

namespace Modules\Bank\Controllers\Admin;

/** 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\Bank\Requests\Admin\BankRequest;
use Modules\Bank\Services\BankService;
use Modules\Currency\Services\CurrencyService;

class BankController extends Controller
{

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

    public function __construct(
        private BankService $bankService,
    )
    {}

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

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

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

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

    /**
     * Store a newly created Bank in storage.
     * @param BankRequest $request
     * @return JsonResponse
     */
    public function store(BankRequest $request) :JsonResponse
    {
        $this->bankService->create($request->validated());

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

    /**
     * Show the form for editing the specified Bank.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $bank = $this->bankService->getItem($id);
        if (!$bank)
        {
            abort(404);
        }
        $action= route('banks.update', $bank->id);
        $head= __('banks.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified Bank in storage.
     * @param BankRequest $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(BankRequest $request, int $id) :JsonResponse
    {
        $bank = $this->bankService->checkItem($id);
        if (!$bank)
        {
            abort(404);
        }
        $this->bankService->update($request->validated(), $id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('banks.index'),
        ];
        return ResponseService::response($response);
    }

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

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

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

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