<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/27/027
 * Time: 18:07
 */

namespace backend\models\searches;


use backend\models\AdminOpenApi;
use backend\models\OpenApi;
use yii\data\ArrayDataProvider;
use yii\data\Pagination;

class OpenSearch extends DataTable
{
    /**
     * @param array $params
     * @return ArrayDataProvider
     */
    public function search($params)
    {
        $this->setAttributes($params);
        // 初始化DataTable查询参数
        $this->initSearchParams();

        $pagination = new Pagination();
        $pagination->setPageSize($this->pageSize);
        $data['pageSize'] = $pagination->getPageSize();
        $data['page'] = $this->page;
        if ($this->orderBy) {
            $data['orderBy'] = $this->orderBy;
        }
        if (!empty($this->search['value']) && trim($this->search['value']) !== '') {
            $data['search'] = trim($this->search['value']);
        }
        $data['member_id'] = \Yii::$app->getUser()->getId();

        $api = new OpenApi();
        $result = $api->getOpenList($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;
    }
}