<?php

namespace App\Admin\Repositories;

use App\Models\SiteAlbum as Model;
use Dcat\Admin\Repositories\EloquentRepository;

class SiteAlbum extends EloquentRepository
{
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;


    public static function siteAlbumPaginate($filter = [],$sort = [],$perPage = 15,$page = 1)
    {
        $siteAlbum = new Model();
        $filterModel = isset($filter['model'])? $filter['model'] : '';
        if ($filterModel) {
            $siteAlbum = $siteAlbum->where('model', 'like', '%'. $filterModel. '%');
        }
        $folder_id = isset($filter['folder_id'])? $filter['folder_id'] : '';
        if ($folder_id) {
            $siteAlbum = $siteAlbum->where('folder_id', $folder_id);
        }
        $albumFolder = isset($filter['album_folder'])? $filter['album_folder'] : -1;
        if ($albumFolder != -1) {

            $siteAlbum = $siteAlbum->whereIn('folder_id', $albumFolder);
        }
        if (empty($sort)) {
            $siteAlbum = $siteAlbum->orderBy('id', 'desc');
        } else {
            $siteAlbum = $siteAlbum->orderBy($sort[0], $sort[1]);
        }

        $result = $siteAlbum->paginate(
            $perPage, $columns = ['*'], $pageName = 'page', $page
        );
        return $result;
    }

    public static function getByFolderId($id)
    {
        $siteAlbum = new Model();
        $result = $siteAlbum->where('folder_id', $id)->first();
        return $result;
    }

}
