<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use \App\Http\Models;
use \Exception;

/**
 * 代理管理类
 */
class AgentController extends Controller
{


    /**
     * 代理登陆、注册
     *
     * @access public
     * @param mixed $req 数据传输
     * @return array JsonString
     */
    public function login(Request $req)
    {
        $code = -2;
        $msg = '操作失败';
        try {
            $agentModel = new Models\Agent;
            $sportsAgentModel = new Models\SportsAgent;
            // 获取Token url及参数.
            $gmenuId = $req->input('id');
            $backData = $sportsAgentModel->getSportsToken($gmenuId);
            if ($backData !== 1) {
                $msg = '链接失败';
                return toJson($code, $msg, []);
            }

            // 获取用户信息.
            $getMemberInfo = $agentModel->getMember();
            // 登陆.
            $backData = $sportsAgentModel->loginSports($gmenuId, $getMemberInfo);
            if ($backData !== 1) {
                $msg = '远程登陆失败';
                return toJson($code, $msg, []);
            }

            // 獲取余额.
            $getMembersDetailed = $agentModel->getMembersDetailed();
            // 如果有余额则转入
            if ($getMembersDetailed->membersDetailed_cash) {
                // 转账.
                $backData = $sportsAgentModel->transferSports($gmenuId, $getMembersDetailed->membersDetailed_cash, 0, $getMemberInfo, 'IN');
                if ($backData !== 1) {
                    $msg = '链接失败';
                    return toJson($code, $msg, []);
                }
            }

            $msg = '成功';
            $code = 1;

            return toJson($code, $msg, []);
        } catch (Exception $e) {
            return toJson($code, $msg, []);
        }

    }//end login()


}
