<?php
/**
 * Created by PhpStorm.
 * User: Jonlin
 * Date: 2019/3/26
 * Time: 9:16
 */
namespace App\Sports\Controller;

use BaseController\Controller;
use \System\Model;

class IndexNav extends Controller{
    public function navlist(){
        if(!empty($_REQUEST['type_code'])){
            $type_code = $_REQUEST['type_code'];
        }else{
            $type_code = 'StRollBall';
        }
        if(!empty($_REQUEST['gametype'])){
            $gametype = $_REQUEST['gametype'];
        }else{
            $gametype = 'zq';
        }
        //滚球
        if($type_code == 'StRollBall'){
            $leagues = lm('st_'.$gametype.'_competition','Sports')
                ->join('st_'.$gametype.'_league','st_'.$gametype.'_competition.lg_id','=','st_'.$gametype.'_league.id')
                ->select('st_'.$gametype.'_competition.lg_id','st_'.$gametype.'_league.name_chinese','st_'.$gametype.'_league.name_english')
                ->where('st_'.$gametype.'_competition.status','1')
                ->where('st_'.$gametype.'_competition.match_date',date("Y-m-d"))
                //->where('st_'.$gametype.'_competition.match_time','>',date("h:i:s"))
                ->distinct('st_'.$gametype.'_competition.lg_id')
                ->get();
            $match->name = $gametype;
            for($i=0;$i<count($leagues);$i++){
                $match->leaguedata[$i]->league_id = $leagues[$i]->lg_id;
                $match->leaguedata[$i]->league_name_chinese = $leagues[$i]->name_chinese;
                $match->leaguedata[$i]->league_name_english = $leagues[$i]->name_english;
                $num = lm('St_'.$gametype.'_competition','Sports')
                    ->select('match_id')
                    ->where('status','1')
                    ->where('match_date',date("Y-m-d"))
                    //->where('match_time','>',date("h:i:s"))
                    ->where('lg_id',$leagues[$i]->lg_id)
                    ->get();
                $match->leaguedata[$i]->num = count($num);
                $gamematch = lm('st_'.$gametype.'_competition','Sports')
                    ->select('match_id','home_team','guest_team','match_date','match_time','status','tag')
                    ->where('status','1')
                    ->where('match_date',date("Y-m-d"))
                    ->where('match_time','>',date("h:i:s"))
                    ->where('lg_id',$leagues[$i]->lg_id)
                    ->get();
                for($j=0;$j<count($gamematch);$j++){
                    $match->leaguedata[$i]->matchdata[$j]->match_id = $gamematch[$j]->match_id;
                    $match->leaguedata[$i]->matchdata[$j]->home_team = $gamematch[$j]->home_team;
                    $match->leaguedata[$i]->matchdata[$j]->guest_team = $gamematch[$j]->guest_team;
                    $match->leaguedata[$i]->matchdata[$j]->match_date = $gamematch[$j]->match_date;
                    $match->leaguedata[$i]->matchdata[$j]->match_time = $gamematch[$j]->match_time;
                    $match->leaguedata[$i]->matchdata[$j]->status = $gamematch[$j]->status;
                    $match->leaguedata[$i]->matchdata[$j]->tag = $gamematch[$j]->tag;
                    $odds = lm('st_'.$gametype.'_odds','Sports')
                        ->select('id','odds_code','odds','condition','sort')
                        ->where(function ($query) {
                            $query->where('odds_code', 'concede_home')
                                ->orWhere('odds_code', 'concede_guest')
                                ->orWhere('odds_code', 'size_home')
                                ->orWhere('odds_code', 'size_guest');
                        })
                        ->where('match_id',$gamematch[$j]->match_id)
                        ->get()
                        ->toArray();
                    //根据 排序 获取 最新让球/大小玩法赔率
                    $sort = array_column($odds,'sort');
                    array_multisort($sort,SORT_DESC,$odds);
                    $odds = array_slice($odds,0,4);
                    for($a=0;$a<count($odds);$a++){
                        $match->leaguedata[$i]->matchdata[$j]->oddsdata[$a] = $odds[$a];
                    }
                }
            }
            Render($match, '1', lang('Tips','Sports')->get('success'));
        }
        // 即将
        else if($type_code == 'StSoon'){
            $leagues = lm('st_'.$gametype.'_competition','Sports')
                ->join('st_'.$gametype.'_league','st_'.$gametype.'_competition.lg_id','=','st_'.$gametype.'_league.id')
                ->select('st_'.$gametype.'_competition.lg_id','st_'.$gametype.'_league.name_chinese','st_'.$gametype.'_league.name_english')
                ->where('st_'.$gametype.'_competition.status','0')
                ->where('st_'.$gametype.'_competition.match_date',date("Y-m-d"))
                ->where('st_'.$gametype.'_competition.match_time','>',date("h:i:s"))
                ->distinct('st_'.$gametype.'_competition.lg_id')
                ->get();
            $match->name = $gametype;
            for($i=0;$i<count($leagues);$i++){
                $match->leaguedata[$i]->league_id = $leagues[$i]->lg_id;
                $match->leaguedata[$i]->league_name_chinese = $leagues[$i]->name_chinese;
                $match->leaguedata[$i]->league_name_english = $leagues[$i]->name_english;
                $num = lm('St_'.$gametype.'_competition','Sports')
                    ->select('match_id')
                    ->where('status','0')
                    ->where('match_date',date("Y-m-d"))
                    ->where('match_time','>',date("h:i:s"))
                    ->where('lg_id',$leagues[$i]->lg_id)
                    ->get();
                $match->leaguedata[$i]->num = count($num);
                $gamematch = lm('st_'.$gametype.'_competition','Sports')
                    ->select('match_id','home_team','guest_team','match_date','match_time','status','tag')
                    ->where('status','0')
                    ->where('match_date',date("Y-m-d"))
                    ->where('match_time','>',date("h:i:s"))
                    ->where('lg_id',$leagues[$i]->lg_id)
                    ->get();
                for($j=0;$j<count($gamematch);$j++){
                    $match->leaguedata[$i]->matchdata[$j]->match_id = $gamematch[$j]->match_id;
                    $match->leaguedata[$i]->matchdata[$j]->home_team = $gamematch[$j]->home_team;
                    $match->leaguedata[$i]->matchdata[$j]->guest_team = $gamematch[$j]->guest_team;
                    $match->leaguedata[$i]->matchdata[$j]->match_date = $gamematch[$j]->match_date;
                    $match->leaguedata[$i]->matchdata[$j]->match_time = $gamematch[$j]->match_time;
                    $match->leaguedata[$i]->matchdata[$j]->status = $gamematch[$j]->status;
                    $match->leaguedata[$i]->matchdata[$j]->tag = $gamematch[$j]->tag;
                    $odds = lm('st_'.$gametype.'_odds','Sports')
                        ->select('id','odds_code','odds','condition','sort')
                        ->where(function ($query) {
                            $query->where('odds_code', 'concede_home')
                                ->orWhere('odds_code', 'concede_guest')
                                ->orWhere('odds_code', 'size_home')
                                ->orWhere('odds_code', 'size_guest');
                        })
                        ->where('match_id',$gamematch[$j]->match_id)
                        ->get()
                        ->toArray();
                    //根据 排序 获取 最新让球/大小玩法赔率
                    $sort = array_column($odds,'sort');
                    array_multisort($sort,SORT_DESC,$odds);
                    $odds = array_slice($odds,0,4);
                    for($a=0;$a<count($odds);$a++){
                        $match->leaguedata[$i]->matchdata[$j]->oddsdata[$a] = $odds[$a];
                    }
                }
            }
            Render($match, '1', lang('Tips','Sports')->get('success'));
        }
        //今日
        else if($type_code = 'StToday'){
            $leagues = lm('st_'.$gametype.'_competition','Sports')
                ->join('st_'.$gametype.'_league','st_'.$gametype.'_competition.lg_id','=','st_'.$gametype.'_league.id')
                ->select('st_'.$gametype.'_competition.lg_id','st_'.$gametype.'_league.name_chinese','st_'.$gametype.'_league.name_english')
                ->where('st_'.$gametype.'_competition.status','!=','2')
                ->where('st_'.$gametype.'_competition.match_date',date("Y-m-d"))
                ->where('st_'.$gametype.'_competition.match_time','>',date("h:i:s"))
                ->distinct('st_'.$gametype.'_competition.lg_id')
                ->get();
            $match->name = $gametype;
            for($i=0;$i<count($leagues);$i++){
                $match->leaguedata[$i]->league_id = $leagues[$i]->lg_id;
                $match->leaguedata[$i]->league_name_chinese = $leagues[$i]->name_chinese;
                $match->leaguedata[$i]->league_name_english = $leagues[$i]->name_english;
                $num = lm('St_'.$gametype.'_competition','Sports')
                    ->select('match_id')
                    ->where('status','!=','2')
                    ->where('match_date',date("Y-m-d"))
                    ->where('match_time','>',date("h:i:s"))
                    ->where('lg_id',$leagues[$i]->lg_id)
                    ->get();
                $match->leaguedata[$i]->num = count($num);
                $gamematch = lm('st_'.$gametype.'_competition','Sports')
                    ->select('match_id','home_team','guest_team','match_date','match_time','status','tag')
                    ->where('status','!=','2')
                    ->where('match_date',date("Y-m-d"))
                    ->where('match_time','>',date("h:i:s"))
                    ->where('lg_id',$leagues[$i]->lg_id)
                    ->get();
                for($j=0;$j<count($gamematch);$j++){
                    $match->leaguedata[$i]->matchdata[$j]->match_id = $gamematch[$j]->match_id;
                    $match->leaguedata[$i]->matchdata[$j]->home_team = $gamematch[$j]->home_team;
                    $match->leaguedata[$i]->matchdata[$j]->guest_team = $gamematch[$j]->guest_team;
                    $match->leaguedata[$i]->matchdata[$j]->match_date = $gamematch[$j]->match_date;
                    $match->leaguedata[$i]->matchdata[$j]->match_time = $gamematch[$j]->match_time;
                    $match->leaguedata[$i]->matchdata[$j]->status = $gamematch[$j]->status;
                    $match->leaguedata[$i]->matchdata[$j]->tag = $gamematch[$j]->tag;
                    $odds = lm('st_'.$gametype.'_odds','Sports')
                        ->select('id','odds_code','odds','condition','sort')
                        ->where(function ($query) {
                            $query->where('odds_code', 'concede_home')
                                ->orWhere('odds_code', 'concede_guest')
                                ->orWhere('odds_code', 'size_home')
                                ->orWhere('odds_code', 'size_guest');
                        })
                        ->where('match_id',$gamematch[$j]->match_id)
                        ->get()
                        ->toArray();
                    //根据 排序 获取 最新让球/大小玩法赔率
                    $sort = array_column($odds,'sort');
                    array_multisort($sort,SORT_DESC,$odds);
                    $odds = array_slice($odds,0,4);
                    for($a=0;$a<count($odds);$a++){
                        $match->leaguedata[$i]->matchdata[$j]->oddsdata[$a] = $odds[$a];
                    }
                }
            }
            Render($match, '1', lang('Tips','Sports')->get('success'));
        }
        //早盘
        else if($type_code = 'StMorningPlate'){
            $leagues = lm('st_'.$gametype.'_competition','Sports')
                ->join('st_'.$gametype.'_league','st_'.$gametype.'_competition.lg_id','=','st_'.$gametype.'_league.id')
                ->select('st_'.$gametype.'_competition.lg_id','st_'.$gametype.'_league.name_chinese','st_'.$gametype.'_league.name_english')
                ->where('st_'.$gametype.'_competition.status','!=','2')
                ->where('st_'.$gametype.'_competition.match_date',date("Y-m-d"))
                ->where('st_'.$gametype.'_competition.match_time','>',date("h:i:s"))
                ->distinct('st_'.$gametype.'_competition.lg_id')
                ->get();
            $match->name = $gametype;
            for($i=0;$i<count($leagues);$i++){
                $match->leaguedata[$i]->league_id = $leagues[$i]->lg_id;
                $match->leaguedata[$i]->league_name_chinese = $leagues[$i]->name_chinese;
                $match->leaguedata[$i]->league_name_english = $leagues[$i]->name_english;
                $num = lm('St_'.$gametype.'_competition','Sports')
                    ->select('match_id')
                    ->where('status','!=','2')
                    ->where('match_date',date("Y-m-d"))
                    ->where('match_time','>',date("h:i:s"))
                    ->where('lg_id',$leagues[$i]->lg_id)
                    ->get();
                $match->leaguedata[$i]->num = count($num);
                $gamematch = lm('st_'.$gametype.'_competition','Sports')
                    ->select('match_id','home_team','guest_team','match_date','match_time','status','tag')
                    ->where('status','!=','2')
                    ->where('match_date',date("Y-m-d"))
                    ->where('match_time','>',date("h:i:s"))
                    ->where('lg_id',$leagues[$i]->lg_id)
                    ->get();
                for($j=0;$j<count($gamematch);$j++){
                    $match->leaguedata[$i]->matchdata[$j]->match_id = $gamematch[$j]->match_id;
                    $match->leaguedata[$i]->matchdata[$j]->home_team = $gamematch[$j]->home_team;
                    $match->leaguedata[$i]->matchdata[$j]->guest_team = $gamematch[$j]->guest_team;
                    $match->leaguedata[$i]->matchdata[$j]->match_date = $gamematch[$j]->match_date;
                    $match->leaguedata[$i]->matchdata[$j]->match_time = $gamematch[$j]->match_time;
                    $match->leaguedata[$i]->matchdata[$j]->status = $gamematch[$j]->status;
                    $match->leaguedata[$i]->matchdata[$j]->tag = $gamematch[$j]->tag;
                    $odds = lm('st_'.$gametype.'_odds','Sports')
                        ->select('id','odds_code','odds','condition','sort')
                        ->where(function ($query) {
                            $query->where('odds_code', 'concede_home')
                                ->orWhere('odds_code', 'concede_guest')
                                ->orWhere('odds_code', 'size_home')
                                ->orWhere('odds_code', 'size_guest');
                        })
                        ->where('match_id',$gamematch[$j]->match_id)
                        ->get()
                        ->toArray();
                    //根据 排序 获取 最新让球/大小玩法赔率
                    $sort = array_column($odds,'sort');
                    array_multisort($sort,SORT_DESC,$odds);
                    $odds = array_slice($odds,0,4);
                    for($a=0;$a<count($odds);$a++){
                        $match->leaguedata[$i]->matchdata[$j]->oddsdata[$a] = $odds[$a];
                    }
                }
            }
            Render($match, '1', lang('Tips','Sports')->get('success'));

        }
        //串场
        else if($type_code = 'StStringScene'){
            $leagues = lm('st_'.$gametype.'_competition','Sports')
                ->join('st_'.$gametype.'_league','st_'.$gametype.'_competition.lg_id','=','st_'.$gametype.'_league.id')
                ->select('st_'.$gametype.'_competition.lg_id','st_'.$gametype.'_league.name_chinese','st_'.$gametype.'_league.name_english')
                ->where('st_'.$gametype.'_competition.status','!=','2')
                ->where('st_'.$gametype.'_competition.match_date',date("Y-m-d"))
                ->where('st_'.$gametype.'_competition.match_time','>',date("h:i:s"))
                ->distinct('st_'.$gametype.'_competition.lg_id')
                ->get();
            $match->name = $gametype;
            for($i=0;$i<count($leagues);$i++){
                $match->leaguedata[$i]->league_id = $leagues[$i]->lg_id;
                $match->leaguedata[$i]->league_name_chinese = $leagues[$i]->name_chinese;
                $match->leaguedata[$i]->league_name_english = $leagues[$i]->name_english;
                $num = lm('St_'.$gametype.'_competition','Sports')
                    ->select('match_id')
                    ->where('status','!=','2')
                    ->where('match_date',date("Y-m-d"))
                    ->where('match_time','>',date("h:i:s"))
                    ->where('lg_id',$leagues[$i]->lg_id)
                    ->get();
                $match->leaguedata[$i]->num = count($num);
                $gamematch = lm('st_'.$gametype.'_competition','Sports')
                    ->select('match_id','home_team','guest_team','match_date','match_time','status','tag')
                    ->where('status','!=','2')
                    ->where('match_date',date("Y-m-d"))
                    ->where('match_time','>',date("h:i:s"))
                    ->where('lg_id',$leagues[$i]->lg_id)
                    ->get();
                for($j=0;$j<count($gamematch);$j++){
                    $match->leaguedata[$i]->matchdata[$j]->match_id = $gamematch[$j]->match_id;
                    $match->leaguedata[$i]->matchdata[$j]->home_team = $gamematch[$j]->home_team;
                    $match->leaguedata[$i]->matchdata[$j]->guest_team = $gamematch[$j]->guest_team;
                    $match->leaguedata[$i]->matchdata[$j]->match_date = $gamematch[$j]->match_date;
                    $match->leaguedata[$i]->matchdata[$j]->match_time = $gamematch[$j]->match_time;
                    $match->leaguedata[$i]->matchdata[$j]->status = $gamematch[$j]->status;
                    $match->leaguedata[$i]->matchdata[$j]->tag = $gamematch[$j]->tag;
                    $odds = lm('st_'.$gametype.'_odds','Sports')
                        ->select('id','odds_code','odds','condition','sort')
                        ->where(function ($query) {
                            $query->where('odds_code', 'concede_home')
                                ->orWhere('odds_code', 'concede_guest')
                                ->orWhere('odds_code', 'size_home')
                                ->orWhere('odds_code', 'size_guest');
                        })
                        ->where('match_id',$gamematch[$j]->match_id)
                        ->get()
                        ->toArray();
                    //根据 排序 获取 最新让球/大小玩法赔率
                    $sort = array_column($odds,'sort');
                    array_multisort($sort,SORT_DESC,$odds);
                    $odds = array_slice($odds,0,4);
                    for($a=0;$a<count($odds);$a++){
                        $match->leaguedata[$i]->matchdata[$j]->oddsdata[$a] = $odds[$a];
                    }
                }
            }
            Render($match, '1', lang('Tips','Sports')->get('success'));

        }
        //冠军
        else if($type_code = 'StChampion'){
            $leagues = lm('st_'.$gametype.'_competition','Sports')
                ->join('st_'.$gametype.'_league','st_'.$gametype.'_competition.lg_id','=','st_'.$gametype.'_league.id')
                ->select('st_'.$gametype.'_competition.lg_id','st_'.$gametype.'_league.name_chinese','st_'.$gametype.'_league.name_english')
                ->where('st_'.$gametype.'_competition.status','!=','2')
                ->where('st_'.$gametype.'_competition.match_date',date("Y-m-d"))
                ->where('st_'.$gametype.'_competition.match_time','>',date("h:i:s"))
                ->distinct('st_'.$gametype.'_competition.lg_id')
                ->get();
            $match->name = $gametype;
            for($i=0;$i<count($leagues);$i++){
                $match->leaguedata[$i]->league_id = $leagues[$i]->lg_id;
                $match->leaguedata[$i]->league_name_chinese = $leagues[$i]->name_chinese;
                $match->leaguedata[$i]->league_name_english = $leagues[$i]->name_english;
                $num = lm('St_'.$gametype.'_competition','Sports')
                    ->select('match_id')
                    ->where('status','!=','2')
                    ->where('match_date',date("Y-m-d"))
                    ->where('match_time','>',date("h:i:s"))
                    ->where('lg_id',$leagues[$i]->lg_id)
                    ->get();
                $match->leaguedata[$i]->num = count($num);
                $gamematch = lm('st_'.$gametype.'_competition','Sports')
                    ->select('match_id','home_team','guest_team','match_date','match_time','status','tag')
                    ->where('status','!=','2')
                    ->where('match_date',date("Y-m-d"))
                    ->where('match_time','>',date("h:i:s"))
                    ->where('lg_id',$leagues[$i]->lg_id)
                    ->get();
                for($j=0;$j<count($gamematch);$j++){
                    $match->leaguedata[$i]->matchdata[$j]->match_id = $gamematch[$j]->match_id;
                    $match->leaguedata[$i]->matchdata[$j]->home_team = $gamematch[$j]->home_team;
                    $match->leaguedata[$i]->matchdata[$j]->guest_team = $gamematch[$j]->guest_team;
                    $match->leaguedata[$i]->matchdata[$j]->match_date = $gamematch[$j]->match_date;
                    $match->leaguedata[$i]->matchdata[$j]->match_time = $gamematch[$j]->match_time;
                    $match->leaguedata[$i]->matchdata[$j]->status = $gamematch[$j]->status;
                    $match->leaguedata[$i]->matchdata[$j]->tag = $gamematch[$j]->tag;
                    $odds = lm('st_'.$gametype.'_odds','Sports')
                        ->select('id','odds_code','odds','condition','sort')
                        ->where(function ($query) {
                            $query->where('odds_code', 'concede_home')
                                ->orWhere('odds_code', 'concede_guest')
                                ->orWhere('odds_code', 'size_home')
                                ->orWhere('odds_code', 'size_guest');
                        })
                        ->where('match_id',$gamematch[$j]->match_id)
                        ->get()
                        ->toArray();
                    //根据 排序 获取 最新让球/大小玩法赔率
                    $sort = array_column($odds,'sort');
                    array_multisort($sort,SORT_DESC,$odds);
                    $odds = array_slice($odds,0,4);
                    for($a=0;$a<count($odds);$a++){
                        $match->leaguedata[$i]->matchdata[$j]->oddsdata[$a] = $odds[$a];
                    }
                }
            }
            Render($match, '1', lang('Tips','Sports')->get('success'));

        }
    }

}