<?php

namespace Modules\ChipOrder\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\Order\Requests\Admin\OrderRequest;
use Modules\ChipOrder\Services\ChipOrderService;
use Modules\City\Services\CityService;
use Modules\ChipOrder\Enums\StatusEnum;

class ChipOrderController extends Controller
{

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

    public function __construct(
        private ChipOrderService $chipOrderService,
        private CityService $cityService,
    )
    {}

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

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

        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->chipOrderService->getItem($id);
        if (!$order)
        {
            abort(404);
        }
        $head= __('orders.edit');
        $action = route('chip_orders.update',$id);
        $statuses = StatusEnum::values();
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    public function update(Request $request,int $id) 
    {
        $this->chipOrderService->update($request->all(),$id);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('chip_orders.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove the specified Order from storage.
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(int $id) :JsonResponse
    {
        $this->chipOrderService->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->chipOrderService->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->chipOrderService->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->chipOrderService->destroyAll([
            'ids' => $request->ids,
            'status' => 0
        ]);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('orders.index'),
        ];
        return ResponseService::response($response);
    }
}
