<?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 __construct() {

    }


    /**
     * 代理登陆、注册
     *
     * @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;
            $gmenuId = $req->input('id');

            // 获取用户信息.
            $getMemberInfo = $agentModel->getMember();
            // 验证用户.
            $backData = $sportsAgentModel->checkAccountSports($gmenuId, $getMemberInfo);
            if ($backData !== 1) {
                $msg = '链接失败';
                return toJson($code, $msg, []);
            }//end if

            // 登陆.
            $loginBackData = $sportsAgentModel->loginSports($gmenuId, $getMemberInfo);
            if ($loginBackData === 2) {
                $msg = '远程登陆失败';
                return toJson($code, $msg, []);
            }

            // 獲取余额.
            $getMembersDetailed = $agentModel->getMembersDetailed();
            // 如果有余额则转入
            if ($getMembersDetailed->membersDetailed_cash > 0) {
                // 转账.
                $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, $loginBackData);
        } catch (Exception $e) {
            return toJson($code, $msg, []);
        }

    }//end login()


    /**
     * 代理转账
     *
     * @access public
     * @param mixed $req 数据传输
     * @return array JsonString
     */
    public function turnOutMoney(Request $req)
    {
        $code = -2;
        $msg = '操作失败';
        try {
            $agentModel = new Models\Agent;
            $sportsAgentModel = new Models\SportsAgent;
            $gmenuId = $req->input('id');
            $money = $req->input('money');
            $type = $req->input('type');
            // 获取用户信息.
            $getMemberInfo = $agentModel->getMember();
            // 獲取余额.
            $getMembersDetailed = $agentModel->getMembersDetailed();
            if ($type === 'OUT') {
                $cash = ($getMembersDetailed->membersDetailed_cash + $money);
            } elseif ($type === 'IN') {
                $cash = ($getMembersDetailed->membersDetailed_cash - $money);
                if ($cash < 0) {
                    $msg = '余额不足';
                    return toJson($code, $msg, []);
                }
            }

            $backData = $sportsAgentModel->transferSports($gmenuId, $money, $cash, $getMemberInfo, $type);
            if ($backData !== 1) {
                $msg = '链接失败';
                return toJson($code, $msg, []);
            }



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

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

    }//end turnOutMoney()


    /**
     * 体育获取用户盈利
     *
     * @access public
     * @param mixed $req 数据传输
     * @return array JsonString
     */
    public function userProfit()
    {
        $sportsAgentModel = new Models\SportsAgent;
        // 查询盈利.
        $backData = $sportsAgentModel->sportsProfit();
        if ($backData === 2) {
            $code = -2;
            $msg = '链接失败';
            return toJson($code, $msg, []);
        }

        return $backData;

    }//end userProfit()


}
