<?php
namespace backend\models\searches;

use backend\models\WithdrawApi;
use Yii;
use yii\data\ArrayDataProvider;
use yii\data\Pagination;

class WithdrawSearch extends DataTable
{
    public $sTime;
    public $eTime;
    public $memberId;
    public $type;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        $rules = parent::rules();
        $rules[] = [['sTime', 'eTime', 'memberId', 'type'], 'safe'];
        return $rules;
    }

    /**
     * @param array $params
     * @param null|string $from
     * @return ArrayDataProvider
     */
    public function search($params, $from = null)
    {
        $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']);
        }
        if ($from == 'admin') {
            $data['from'] = $from;
            if ($this->sTime && ($inTimeStart = strtotime($this->sTime))) {
                $data['inTimeStart'] = $inTimeStart * 1000;
            }
            if ($this->eTime && ($inTimeEnd = strtotime($this->eTime))) {
                $data['inTimeEnd'] = $inTimeEnd * 1000;
            }
            $this->memberId && $data['memberId'] = $this->memberId;
            is_numeric($this->type) && $data['type'] = $this->type;
        } else {
            $data['memberId'] = Yii::$app->getUser()->getId();
        }
        $api = new WithdrawApi();
        $result = $api->getWithdrawList($data);
        $models = [];
        if ($result['code'] == 1) {
            $models = (array)$result['data']['dataList'];
            $pagination->totalCount = $result['data']['totalCount'];
        }
        $dataProvider = new ArrayDataProvider([
            'models' => $models,
            'pagination' => $pagination,
            'totalCount' => $pagination->totalCount,
        ]);
        return $dataProvider;
    }
}