<?php
namespace backend\controllers;

use backend\helpers\PaginationHelper;
use backend\helpers\ValidatorHelper;
use backend\models\Commission;
use backend\models\Member;
use backend\models\UserMember;
use common\helpers\Utils;
use Yii;
use backend\models\Mt4Users;

class Mt4UserController extends BaseController
{
    public function actionInfo()
    {
        $login = (int) Yii::$app->request->get('login');
        $from = Yii::$app->getRequest()->get('from');
        if (!$login) {
            return $this->outJson(0, [], '参数错误');
        }

        $model = new Mt4Users();
        $data = $model->findByLogin($login, true);
        if ($data) {
            if ($from == 'admin') {
                $ib = null;
                $parents = null;
                $isCommissionRun = null;
                $userMember = UserMember::find()->where(['login' => $login])->asArray()->limit(1)->one();
                if ($userMember != null) {
                    $isCommissionRun = $userMember['is_commission_run'];
                    $ib = Member::find()->where(['id' => $userMember['member_id']])->limit(1)->one();
                    if ($ib != null) {
                        $parents = Member::findParents($ib['id']);
                        foreach ($parents as $key => $parent) {
                            $parents[$key]['comm'] = Commission::find()->where(['login' => $login, 'member_id' => $parent['id']])->asArray()->limit(1)->one();
                        }
                    }
                }
                $data['isCommissionRun'] = $isCommissionRun;
                $data['ib'] = $ib;
                $data['parents'] = $parents == null ? null : array_reverse($parents);
            }
            return $this->outJson(1, $data);
        } else {
            return $this->outJson(0, [], '没有数据');
        }
    }

    /**
     * 名下用户
     */
    public function actionList()
    {
        $model = new Mt4Users();
        $result = $model->getList(Yii::$app->request->post());
        if ($result['code']) {
            return $this->outJson(1, $result['data']);
        } else {
            return $this->outJson(0, [], $result['message']);
        }
    }

    /**
     * MT4账户列表
     * @return \yii\web\Response
     */
    public function actionPagelist()
    {
        $data = Yii::$app->getRequest()->get();
        $data['search'] = isset($data['search']) ? trim($data['search']) : '';
        $data['page'] = isset($data['page']) ? max(intval($data['page']), 1) : 1;
        $data['pageSize'] = isset($data['pageSize']) ? intval($data['pageSize']) : 20;
        $data['orderBy'] = isset($data['orderBy']) ? trim($data['orderBy']) : 'LOGIN desc';
        $data = ValidatorHelper::validateData($data, [
            ['search', 'string'],
            ['page', 'integer', 'min' => 1],
            ['pageSize', 'integer', 'min' => 1],
            ['orderBy', 'string'],
        ], $errors);
        if ($data == false) {
            return $this->outJson(0, '', $errors);
        }
        $query = Mt4Users::find()->with(['ibMember'])->orderBy($data['orderBy']);
        $query->andWhere(['!=', 'GROUP', 'demoforex-usd']);
        $query->andFilterWhere([
            'or',
            ['like', 'LOGIN', $data['search']],
            ['like', 'NAME', $data['search']],
        ]);
        $result = PaginationHelper::queryPage($query, $data['page'], $data['pageSize']);
		foreach ($result['dataList'] as $key => $row) {
            $result['dataList'][$key]['MARGIN'] = Utils::formatFloatOrInt($result['dataList'][$key]['MARGIN']);
            $result['dataList'][$key]['MARGIN_FREE'] = Utils::formatFloatOrInt($result['dataList'][$key]['MARGIN_FREE']);
            $result['dataList'][$key]['MARGIN_LEVEL'] = Utils::formatFloatOrInt($result['dataList'][$key]['MARGIN_LEVEL']);
            $result['dataList'][$key]['EQUITY'] = Utils::formatFloatOrInt($result['dataList'][$key]['EQUITY']);
            if (isset($result['dataList'][$key]['ibMember']['name'])) {
                $result['dataList'][$key]['IBNAME'] = $result['dataList'][$key]['ibMember']['name'];
            } else {
                $result['dataList'][$key]['IBNAME'] = '';
            }
            unset($result['dataList'][$key]['ibMember']);
		}
        return $this->outJson(1, $result);
    }
}
