<?php

namespace App\Http\Controllers\Admin;

/** Ajax Request */

use App\Exports\tableNumberExport;
use App\Http\Requests\Admin\TableNumberRequest;
use App\Models\TableNumber;
use App\Services\Interfaces\TableNumberRepositoryInterface;
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 TableNumberController extends Controller
{

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

    public function __construct(TableNumberRepositoryInterface $tableNumberRepository)
    {
        $this->tableNumberRepository = $tableNumberRepository;
    }

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

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

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

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

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

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

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

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

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

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

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

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