<?php
/**
 * Created by PhpStorm.
 * User: chenkuan
 * Date: 2017/11/8
 * Time: 上午10:09
 */

namespace backend\models\searches;


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

class LeverSearch extends DataTable
{
    public $type;
    public $sTime;
    public $eTime;
    public $member_id;

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

    /**
     * @param array $params
     * @param $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 && ($inStartTime = strtotime($this->sTime))) {
                $data['inStartTime'] = $inStartTime * 1000;
            }
            if ($this->eTime && ($inEndTime = strtotime($this->eTime))) {
                $data['inEndTime'] = $inEndTime * 1000 + 86400000;
            }
            $this->member_id && $data['member_id'] = $this->member_id;
            is_numeric($this->type) && $data['type'] = $this->type;
        } else {
            $data['member_id'] = (int)$params['member_id'];
        }

        if ($this->pageSize > 5000) {
            if (\Yii::$app->getRequest()->isAjax) {
                \Yii::$app->getResponse()->redirect(Url::current(), UserAgentHelper::isIE() ? 200 : 302);
            } else {
                $this->exportXls($data);
            }
            \Yii::$app->end();
        }
        
        $api = new LeverApi();
        $result = $api->getLeverList($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;
    }

    /**
     * @param $data
     */
    private function exportXls($data)
    {
        ExcelHelper::init();
        $attachmentName = "修改杠杆_" . date('Y-m-d') . '.xlsx';
        $header = ['姓名', '状态', '会员ID', '账户', '杠杆', '申请时间', '备注'];
        $source = [];
        $api = new LeverApi();
        $data['pageSize'] = 5000;
        $data['page'] = 1;
        while (true) {
            $result = $api->getLeverList($data);
            if ($result['code'] == 1 && $result['data']['dataList']) {
                foreach ((array)$result['data']['dataList'] as $key => $row) {
                    $arr = [];
                    $arr[] = $row['name'];
                    if ((int)$row['type'] == 0) {
                        $arr[] = "等待审核";
                    } elseif ((int)$row['type'] == 1) {
                        $arr[] = "不通过";
                    } else {
                        $arr[] = "已修改";
                    }
                    $arr[] = $row['member_id'];
                    $arr[] = $row['login'];
                    $arr[] = $row['lever'];
                    $arr[] = date('Y-m-d H:i:s', intval($row['in_time'] / 1000));
                    $arr[] = $row['memo'];
                    $source[] = $arr;
                }
            } else {
                break;
            }
            if ($data['page'] * $data['pageSize'] >= $result['data']['totalCount']) {
                break;
            }
            $data['page']++;
        }
        ExcelHelper::output($source, $header, $attachmentName);
    }
}
