<?php 
namespace backend\models\searches;

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

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();
        }

        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 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;
    }

    /**
     * @param array $data
     */
    private function exportXls($data)
    {
        ExcelHelper::init();
        $attachmentName = "出金申请_" . date('Y-m-d') . '.xlsx';
        $header = ['类型', '代理商', '账户', '状态', '美金', '人民币', '手续费', '实扣', '汇率', '姓名',
            '手机', '银行', '省份', '城市', '分行', '卡号', '申请时间', '出金时间', '备注', '操作人'];
        $source = [];
        $api = new WithdrawApi();
        $data['pageSize'] = 5000;
        $data['page'] = 1;
        while (true) {
            $result = $api->getWithdrawList($data);
            if ($result['code'] == 1 && !empty($result['data']['dataList'])) {
                foreach ((array)$result['data']['dataList'] as $key => $row) {
                    $arr = [];
                    if ($row['memberType'] == 1) {
                        $arr[] = "用户";
                    } elseif($row['memberType'] == 2) {
                        $arr[] = "代理";
                    } else {
                        $arr[] = "";
                    }
                    $arr[] = $row['IBNAME'];
                    $arr[] = $row['login'];
                    if ($row['type'] == 0) {
                        $arr[] = "等待审核";
                    } elseif($row['type'] == 1) {
                        $arr[] = "不通过";
                    } elseif($row['type'] == 2) {
                        $arr[] = "已出金";
                    } elseif($row['type'] == 3) {
                        $arr[] = "管理员撤销";
                    } elseif($row['type'] == 4) {
                        $arr[] = "未分享朋友圈";
                    } elseif($row['type'] == 5) {
                        $arr[] = "暂不处理";
                    } elseif($row['type'] == 6) {
                        $arr[] = "处理中";
                    } else {
                        $arr[] = "";
                    }
                    $arr[] = $row['amount'];
                    $arr[] = $row['rmb'];
                    $arr[] = $row['fee'];
                    $arr[] = $row['sk'];
                    $arr[] = $row['rate'];
                    $arr[] = $row['true_name'];
                    $arr[] = $row['mobile'];
                    $arr[] = $row['bank_name'];
                    $arr[] = $row['bank_province'];
                    $arr[] = $row['bank_city'];
                    $arr[] = $row['bank_sub_name'];
                    $bank_card_no = $row['bank_card_no'];
                    $arr[] = function ($cell) use ($bank_card_no) {
                        /** @var \PHPExcel_Cell $cell */
                        $cell->setValueExplicit($bank_card_no);
                        return true;
                    };
                    $arr[] = date('Y-m-d H:i:s', intval($row['in_time'] / 1000));
                    if ($row['out_time'] > 0) {
                        $arr[] = date('Y-m-d H:i:s', intval($row['out_time'] / 1000));
                    } else {
                        $arr[] = "";
                    }
                    $arr[] = $row['memo'];
                    $arr[] = $row['admin_name'];
                    $source[] = $arr;
                }
            } else {
                break;
            }
            if ($data['page'] * $data['pageSize'] >= $result['data']['totalCount']) {
                break;
            }
            $data['page']++;
        }
        ExcelHelper::output($source, $header, $attachmentName);
    }
}