<?php

namespace App\Classes\Modules\Orders\Services;


use App\Models\Order;
use Carbon\Carbon;
use Illuminate\Http\Request;

use App\Http\Resources\Order as OrderResource;

class SearchesOrder
{
    /** @var  Order */
    private $repository;

    /**
     * SearchesOrder constructor.
     * @param Order $repository
     */
    public function __construct(Order $repository)
    {
        $this->repository = $repository;
    }


    public function execute(Request $request,array $status)
    {

        $order = $this->repository;

        if($request->input('marking') !== null)
        {
            $order = $order->where('marking','LIKE','%'.$request->input('marking').'%');
        }

        if($request->input('startDate') !== null)
        {
            $startDate = Carbon::createFromFormat('d-m-Y', $request->input('startDate'))->format('Y-m-d');
            $order = $order->where('created_at', '>=', $startDate);
        }

        if($request->input('endDate') !== null)
        {
            $endDate = Carbon::createFromFormat('d-m-Y', $request->input('endDate'))->format('Y-m-d');
            $order = $order->where('created_at', '<=', $endDate);
        }

        if(count($status))
        {
            $order = $order->whereIn('current_step', $status);
        }

        $order = $order->paginate(10);
        return OrderResource::collection($order);

    }

}