<?php

namespace App\Admin\Repositories;

use App\Libraries\RpcClient;
use App\Models\NullModel as Model;
use Dcat\Admin\Form;
use Dcat\Admin\Repositories\EloquentRepository;
use Dcat\Admin\Show;
use JsonRPC\Client;

/*
 * RPC调用相册
 */
class RpcAlbum extends EloquentRepository
{
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;


    public function execute($method, $params = [])
    {
        return RpcClient::albumExecute($method, $params);
    }

    /*
     * 通过IDS获取相册详情
     */
    public function getByids($ids)
    {
        return $this->execute('siteAlbumGetByIds', [
            'ids' => $ids,
        ]);
    }

    /*
     * 获取相册列表
     */
    public function get(Grid\Model|\Dcat\Admin\Grid\Model $model)
    {
        // 获取当前页数
        $currentPage = $model->getCurrentPage();
        // 获取每页显示行数
        $perPage = $model->getPerPage();
        //排序
        $sort = $model->getSort();
        if (empty($sort) == true || $sort[0] == null) {
            $sort = [];
        }
        // 获取筛选参数
        $filterModel = $model->filter()->input('model', '');
        $folder_id = $model->filter()->input('folder_id', '');

        $filter = [
            'model' => $filterModel,
            'folder_id' => $folder_id,
        ];

        $result = $this->execute('siteAlbumPaginate', [
            'filter' => $filter,
            'sort' => $sort,
            'perPage'=>$perPage,
            'page' => $currentPage,
        ]);

        $data = $result['data'] ?? [];

        return $model->makePaginator(
          $data['total'] ?? 0,
           $data['data'] ?? [] // 传入数据二维数组
        );
    }


    /*
     * 获取相册详情
     */
    public function detail(Show $show): array
    {
        // 获取数据主键值
        $id = $show->getKey();
        $result = $this->execute('siteAlbumGet', [
            'id' => $id,
        ]);
        $data = $result['data'] ?? [];
        return $data;
    }


}
