<?php

namespace App\Http\Controllers\Admin;

/** Ajax Request */

use App\Http\Requests\Admin\CobonRequest;
use App\Models\Room;
use App\Models\Cobon;
use App\Services\Interfaces\CobonRepositoryInterface;
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 CobonController extends Controller
{

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

    public function __construct(CobonRepositoryInterface $cobonRepository)
    {
        $this->cobonRepository = $cobonRepository;
    }

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

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

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

    public function fetchCities(Request $request)
    {
        $cobons= $this->cobonRepository->get($request);
        return $cobons;
    }

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

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

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

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

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

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

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

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

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