<?php
namespace backend\modules\user\controllers;

use backend\models\Mt4tradeApi;
use Yii;

class WithdrawController extends BaseController
{
    public function actionIndex()
    {
        $params = Yii::$app->getRequest()->getQueryParams();
        return $this->render('index', [
            'profit' => $this->sumProfit($params),
        ]);
    }

    protected function sumProfit($params)
    {
        $api = new Mt4tradeApi();
        $data = [];
        $data['login'] = Yii::$app->getUser()->getIdentity(false)->getMain_login();
        $data['type'] = 'withdraw';
        if (isset($params['sTime']) && trim($params['sTime']) !== '') {
            $data['closeTimeStart'] = date('Y-m-d 00:00:00', strtotime($params['sTime']));
        }
        if (isset($params['eTime']) && trim($params['eTime']) !== '') {
            $data['closeTimeEnd'] = date('Y-m-d 23:59:59', strtotime($params['eTime']));
        }
        $result = $api->sumProfit($data);
        if ($result['code'] == 1) {
            return !empty($result['data']['profit']) ? round($result['data']['profit'], 5) : 0;
        } else {
            return 0;
        }
    }
}