<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Models\AccountsInfo;
use App\Models\Subordinate;
use App\Models\SystemLog;
use Illuminate\Support\Carbon;
class SubordinateController extends Controller
{
    //代理列表
    public function subordinateList(Request $request)
    {
        $nickname = $request->post('nickname');
        $game_id = $request->post('game_id');
        $start_time  = $request->post('start_time');
        $end_time    = $request->post('end_time');
        $list_where = [];
        !empty($start_time) ? $list_where[] = ['s.CreateAt','>=',$start_time] : '';
        !empty($end_time) ? $list_where[]    =   ['s.CreateAt','<=',date('Y-m-d',strtotime("$end_time+1day"))] : '';
        !empty($nickname) ? $list_where[] = ['ai.NickName','like','%'.$nickname.'%'] : '';
        !empty($game_id) ? $list_where[] = ['ai.GameID','=',(int)$game_id] : '';
    	$subordinate_list_obj = DB::table('QPPlatformDB.dbo.Subordinate as s')
    		->leftJoin('QPAccountsDB.dbo.AccountsInfo as ai','s.UserID','=','ai.UserID')
            ->leftJoin('QPAccountsDB.dbo.AccountsInfo as aii','s.SpreaderID','=','aii.UserID')
    		->select('s.UserID','ai.GameID','aii.GameID as SpGameId','s.SpreaderID','ai.NickName','s.Commission',DB::RAW('CONVERT(varchar(100), s.CreateAt, 20) as CreateAt'),'s.Level')
            ->groupBy('s.UserID','ai.GameID','aii.GameID','s.SpreaderID','ai.NickName','s.Commission','s.CreateAt','s.Level')
            ->where($list_where)
            ->orderBy('s.Level','asc')
    		->paginate(10);
    	return view('admin.subordinate.list',['list'=>$subordinate_list_obj,'nickname'=>$nickname,'game_id'=>$game_id,'start_time'=>$start_time,'end_time'=>$end_time]);
    }

    //点位设置
    public function setCommissionView(Request $request,$user_id)
    {   
        $data = Subordinate::find($user_id);
    	return view('admin.subordinate.set_commission_view',['data'=>$data]);
    }

    //点位设置
    public function setCommission(Request $request,$user_id)
    {
    	$params['commission'] = $request->input('num');
    	$params['user_id'] = $user_id;
        $params['spreader_id'] = $request->input('spreader_id');
        $result = Subordinate::setCommission($params);
        if($result['status'] == true){
        	return $this->json(200, "处理成功");
        }else{
        	return $this->json(500, $result['msg']);
        }
    }

    //添加代理页面
    public function addSubView(Request $request)
    {
        // $data = Subordinate::select('Commission','Level')->where('UserID',$user_id)->first();
        return view('admin.subordinate.add');
    }

    //添加代理
    public function addSub(Request $request)
    {
        $game_id = $request->input('game_id');
        $params['Commission'] = $request->input('commission');
        $params['UserID'] = AccountsInfo::where('GameID',$game_id)->value('UserID');
        $params['SpreaderID'] = 0;
        $params['CreateAt'] = Carbon::now();
        $params['Level'] = 1;
        if(Subordinate::find($params['UserID'])){
            return $this->json(500, '该用户已是分销商');
        }
        $result = Subordinate::insert($params);
        if($result){
            return $this->json(200, "处理成功");
        }else{
            return $this->json(500, $result['msg']);
        }
    }
    
    //直属玩家列表
    public function playersList(Request $request,$user_id)
    {
        $subs = DB::table('QPPlatformDB.dbo.Subordinate')
            ->where('SpreaderID',$user_id)
            ->pluck('UserID');
        $list = AccountsInfo::where('SpreaderID',$user_id)
            ->whereNotIn('UserID',$subs)
            ->get();
        return view('admin.subordinate.players',['list'=>$list]);
    }

    //直属代理列表
    public function subsList(Request $request,$user_id)
    {
        $subordinate_list1 = DB::table('QPPlatformDB.dbo.Subordinate as s')
            ->leftJoin('QPAccountsDB.dbo.AccountsInfo as ai','s.UserID','=','ai.UserID')
            ->leftJoin('QPRecordDB.dbo.GoldRebateLog as ry','s.UserID','=','ry.UserID')
            ->select('s.UserID','ai.GameID','s.Auth','s.SpreaderID','ai.NickName','s.Commission',DB::Raw('IsNull(SUM(ry.Tribute),0) as Tribute,IsNull(SUM(ry.Achievement),0) as Achievement'))
            ->groupBy('s.UserID','ai.GameID','s.SpreaderID','ai.NickName','s.Commission','s.Auth')
            ->where('ai.SpreaderID',$user_id)
            ->get();
        $add = 0;
        $subordinate_list = json_decode(json_encode($subordinate_list1),true);

        $user_ids = array_column($subordinate_list,'UserID');

        $subordinate_num = DB::table('QPPlatformDB.dbo.Subordinate')
            ->selectRaw('COUNT(UserID) as subordinate_num,SpreaderID')
            ->whereIn('SpreaderID',$user_ids)
            ->groupBy('SpreaderID')
            ->get();//直属代理人数
            
        $players_num = DB::table('QPAccountsDB.dbo.AccountsInfo')
            ->selectRaw('COUNT(UserID) as players_num,SpreaderID')
            ->whereIn('SpreaderID',$user_ids)
            ->groupBy('SpreaderID')
            ->get();//直属下级人数
        foreach ($subordinate_list1 as $key => &$value) {
            $users = AccountsInfo::getUsers($value->UserID);//团队人员
            $value->spread_num = AccountsInfo::getSpreadlNum($users);//团队总人数
            $value->subordinate_num = 0;
            $value->players_num = 0;
            $value->Revenue = 0;
            foreach ($subordinate_num as $k => $v) {
                if($value->UserID == $v->SpreaderID){
                    $value->subordinate_num = $v->subordinate_num;
                }
            }
            foreach ($players_num as $k => $v) {
                if($value->UserID == $v->SpreaderID){
                    $value->players_num = $v->players_num;
                }
            }
            // $value->Tribute = $value->Tribute/100;
            $value->Achievement = $value->Achievement/100;
            $value->players_num = $value->players_num-$value->subordinate_num;//直属玩家人数
        }
        unset($value);
        return view('admin.subordinate.subs',['list'=>$subordinate_list1]);
    }


}
