<?php

namespace Modules\Gallery\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Gallery\Interfaces\GalleryRepositoryInterface;
use Modules\Gallery\Models\Gallery;

class GalleryRepository implements GalleryRepositoryInterface
{
    public function get(Request $request)
    {
        $query = Gallery::orderBy('id', 'DESC');

        if ($request->date_from) {
            $query->whereDate('created_at', '>=', $request->date_from);
        }

        if ($request->date_to) {
            $query->whereDate('created_at', '<=', $request->date_to);
        }


        if ( !is_null($request->status) && $request->status == 0) {
            $query->onlyTrashed();
        }

        if ($request->paginate ?? null) {
            $countries= $query->paginate(50);
        }else{
            $countries=$query->cursor();
        }

        return $countries;
    }

    public function save(Request $request)
    {
        Gallery::query()->delete();
        if(isset($request->images))
        {
            foreach($request->images as $image)
            {
                Gallery::create(['image'=>$image]);
            }
        }
    }

}
