<?php

namespace App\Http\Controllers\Admin;

/** Ajax Request */

use App\Exports\OrderExport;
use App\Http\Requests\Admin\OrderRequest;
use App\Models\OrderPayment;
use App\Models\OrderProduct;
use App\Models\Room;
use App\Models\Cobon;
use App\Models\Order;
use App\Models\TableNumber;
use App\Models\User;
use App\Services\Interfaces\OrderRepositoryInterface;
use App\Services\Repositories\RoomRepository;
use App\Services\Repositories\CityRepository;
use App\Services\Repositories\DeliveryRepository;
use App\Services\Repositories\MemberRepository;
use App\Services\Repositories\TableNumberRepository;
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 OrderController extends Controller
{

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

    public function __construct(OrderRepositoryInterface $orderRepository)
    {
        $this->orderRepository = $orderRepository;
    }

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

    /**
     * return data of the orders.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) //:View
    {
        if (isset($request->export)) {
            $orders= $this->orderRepository->get($request);
            return Excel::download(new OrderExport($orders, 'orders'), 'Orders.xlsx');
        }

        $request->merge(['paginate'=> true]);
        $orders= $this->orderRepository->get($request);

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

    /**
     * Show the form for creating a new Order.
     *
     * @return View
     */
    public function create(Request $request)
    {
        //
    }

    /**
     * Store a newly created Order in storage.
     * @param OrderRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(OrderRequest $request) :JsonResponse
    {

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

    /**
     * Show the form for editing the specified Order.
     *
     * @param  Order  $order
     * @return View
     */
    public function edit(Request $request,Order $order) :View
    {
        $action= route('orders.update', $order->id);
        $products = OrderProduct::where('order_id',$order->id)->cursor();
        $payment = OrderPayment::where('order_id',$order->id)->first();
        $head= __('orders.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

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

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