<?php
namespace frontend\controllers;

use backend\helpers\PaginationHelper;
use backend\helpers\ValidatorHelper;
use frontend\models\Slide;
use Yii;

class SlideController extends BaseController
{
    public function actionIndex()
    {
        $data = Yii::$app->getRequest()->get();
        $data['page'] = isset($data['page']) ? max(intval($data['page']), 1) : 1;
        $data['pageSize'] = isset($data['pageSize']) ? intval($data['pageSize']) : 20;
        $data['orderBy'] = isset($data['orderBy']) ? trim($data['orderBy']) : 'id desc';
        $data = ValidatorHelper::validateData($data, [
            ['page', 'integer', 'min' => 1],
            ['pageSize', 'integer', 'min' => 1],
            ['orderBy', 'string'],
        ], $errors);
        if ($data == false) {
            return $this->outJson(0, '', $errors);
        }
        $query = Slide::find()->orderBy($data['orderBy']);
        $result = PaginationHelper::queryPage($query, $data['page'], $data['pageSize']);
        return $this->outJson(1, $result);
    }
}