<?php

namespace Modules\Order\Controllers\Warehouse;

/** 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\Order\Enums\OrderStatusEnum;
use Modules\Order\Models\Order;
use Modules\Order\Requests\Warehouse\OrderRequest;
use Modules\Order\Services\OrderService;

class OrderController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/warehouse/orders';
    /** View folder */
    private $viewDirectory= 'warehouse.orders.';
    //
    private OrderService $orderService;

    public function __construct(OrderService $orderService)
    {
        $this->orderService = $orderService;
    }

    /**
     * Display a listing of the orders.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('orders.head');
        $statuses = OrderStatusEnum::values();

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

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

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

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



    /**
     * Show the form for editing the specified Order.
     *
     * @param  Order  $order
     * @return View
     */
    public function edit(Order $order) :View
    {
        // dd($order->id);
        $action= route($this->viewDirectory.'update', $order->id);
        $data= $this->orderService->getItem($order->id);
        $head= __('orders.edit');
        $statuses = OrderStatusEnum::warehouse_values();
        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->orderService->update($request->all(), $order->id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route($this->viewDirectory.'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->orderService->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->orderService->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->orderService->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->orderService->restoreAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }
}
