<?php
namespace backend\models\searches;

use backend\models\MemberApi;
use common\helpers\ExcelHelper;
use common\helpers\UserAgentHelper;
use Yii;
use yii\data\ArrayDataProvider;
use yii\data\Pagination;
use yii\helpers\Url;

class MemberSearch extends DataTable
{
    /**
     * @param array $params
     * @param int $type
     * @return ArrayDataProvider
     */
    public function search($params, $type)
    {
        $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['type'] = $type;

        if ($this->pageSize > 5000) {
            if (Yii::$app->getRequest()->getIsAjax()) {
                Yii::$app->getResponse()->redirect(Url::current(), UserAgentHelper::isIE() ? 200 : 302);
            } else {
                $this->exportXls($data);
            }
            Yii::$app->end();
        }

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

    /**
     * @param array $data
     */
    private function exportXls($data)
    {
        ExcelHelper::init();
        $attachmentName = "用户管理_" . date('Y-m-d') . '.xlsx';
        $header = ['账户', '邮箱', '姓名', '手机', '状态', '注册时间'];
        $source = [];
        $api = new MemberApi();
        $data['pageSize'] = 5000;
        $data['page'] = 1;
        while (true) {
            $result = $api->getList($data);
            if ($result['code'] == 1 && !empty($result['data']['dataList'])) {
                foreach ((array)$result['data']['dataList'] as $key => $row) {
                    $arr = [];
                    $arr[] = $row['logins'];
                    $arr[] = $row['username'];
                    $arr[] = $row['name'];
                    $arr[] = $row['mobile'];
                    if ($row['is_enable'] == 1) {
                        $arr[] = "启用";
                    } else {
                        $arr[] = "禁用";
                    }
                    $arr[] = date('Y-m-d H:i:s', intval($row['in_time'] / 1000));
                    $source[] = $arr;
                }
            } else {
                break;
            }
            if ($data['page'] * $data['pageSize'] >= $result['data']['totalCount']) {
                break;
            }
            $data['page']++;
        }
        ExcelHelper::output($source, $header, $attachmentName);
    }
}