<?php

namespace Modules\Order\Controllers\Admin;

/** Ajax Request */

use App\Exports\OrderExport;
use Maatwebsite\Excel\Facades\Excel;
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\Canteen\Services\CanteenService;
use Modules\Order\Requests\Admin\OrderRequest;
use Modules\Order\Services\OrderService;

class OrderController extends Controller
{

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

    public function __construct(
        private OrderService $orderService,
        private CanteenService $canteenService,
    )
    {}

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

    /**
     * return data of the orders.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request)
    {
        if (!isset($request->export)) 
        {
            $request->merge(['paginate'=>true]);
        }
        
        $orders= $this->orderService->get($request->all());

        if (isset($request->export)) 
        {
            return Excel::download(new OrderExport($orders, 'orders'), 'Orders.xlsx');
        }
        return view($this->viewDirectory.'grid',get_defined_vars());
    }


    /**
     * Show the form for editing the specified Order.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $order = $this->orderService->getItem($id);
        if (!$order)
        {
            abort(404);
        }
        $head= __('orders.edit');
        $canteens = $this->canteenService->get();

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


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

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

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

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