<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/1/001
 * Time: 15:23
 */

namespace backend\models\searches;


use backend\models\MailApi;
use yii\data\ArrayDataProvider;
use yii\data\Pagination;

class MailSearch extends DataTable
{
    public function search($params)
    {
        $this->setAttributes($params);
        $this->initSearchParams();
        
        $pagination = new Pagination();
        $pagination->pageSize = $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']);
        }
        
        $mailApi = new MailApi();
        $result = $mailApi->getMailList($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;
    }
}
