<?php
/**
 * Created by PhpStorm.
 * User: chenkuan
 * Date: 2017/12/15
 * Time: 下午3:22
 */

namespace frontend\models\searches;

use frontend\models\IndexActivityApi;
use yii\data\ArrayDataProvider;
use yii\data\Pagination;

class IndexActivitySearch extends DataTable
{
    public function search($params, $methodName = '')
    {
        $this->setAttributes($params);
        $this->initSearchParams();

        $pagination = new Pagination();
        $pagination->setPageSize($this->pageSize);
        $data['page'] = $this->page;
        $data['pageSize'] = $pagination->getPageSize();

        if ($this->orderBy) {
            $data['orderBy'] = $this->orderBy;
        }
        if (!empty($this->search['value']) && trim($this->search['value']) !== '') {
            $data['search'] = trim($this->search['value']);
        }

        $api = new IndexActivityApi();
        $result = $api->$methodName($data);
        $models = [];
        if ($result['code'] == 1) {
            $models = (array)$result['data']['dataList'];
            $pagination->totalCount = (int)$result['data']['totalCount'];
        }
        $dataProvider = new ArrayDataProvider([
            'models' => $models,
            'pagination' => $pagination,
            'totalCount' => $pagination->totalCount,
        ]);
        return $dataProvider;
    }
}
