<?php
/**
 * Created by PhpStorm.
 * User: Jun.peng
 * Date: 2019/6/22
 * Time: 10:35
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request as Req;
use Request;

/**
 * Class LqChampionsLeagueController
 * @package App\Http\Controllers\Admin
 * 篮球 冠军联赛 管理
 */
class LqChampionsLeagueController extends Controller
{
    public function index(Req $req) {
        //$request['name_chinese'] = isset($req->name_chinese) ? trim($req->name_chinese) : '-1';
        $request['name_chinese'] = isset($req->name_chinese) ? trim($req->name_chinese) : '';
        $request['area_id'] = isset($req->area_id) ? trim($req->area_id) : '';
        $request['country_id'] = isset($req->country_id) ? trim($req->country_id) : '';
        $request['id'] = isset($req->id) ? trim($req->id) : '';
        $request['status'] = isset($req->status) ? trim($req->status) : '-1';
        $request['sureblurs'] = isset($req->sureblurs) ? $req->sureblurs : 'on';
        $data = \App\Models\BasketLeague::select('id','lg_id','name_chinese')->get();
        $area = \App\Models\StAreaCountry::where('pid',0)->get();
        $country = \App\Models\StAreaCountry::where('pid','!=',0)->get();
        $request['league'] = $data;
        $request['area'] = $area;
        $request['country'] = $country;
        $dt = \App\Lib\DataTable\DataTable::init();
        $dt->setDataSource('/admin/LqChampionsLeague/info');
        $dt->setLang('sportsbase');
        $dt->addColsFields('lg_id', array('templet' => '#userdetail', 'sort' => true, 'width' => 120));
        $dt->addColsFields('name_chinese', array('templet' => '#userdetail', 'sort' => false, 'width' => 200));
        $dt->addColsFields('area', array('sort' => false, 'width' => 180));
        $dt->addColsFields('country', array('sort' => true, 'width' => 180));
        $dt->addColsFields('league_status', array('templet' => '#userdetail', 'sort' => false, 'width' => 90));
        $dt->addColsFields('utime', array('templet' => '#userdetail', 'sort' => false, 'width' => 160));
        $dt->addColsFields('isresults', array('templet' => '#isresults', 'sort' => false, 'width' => 160));
        // if (checkRriv('/admin/LqChampionsLeague/edit')) {
        //     $arr[] = 'edit';
        // }
        // $dt->setToolBar($arr, array('width' => 140));
        $dt->enableCheckBox();
        return view('sports/basket_league', $dt->render($request));
    }

    function info(Req $req) {
        $page = Request::has('page') ? Request::get('page') : '';
        $list = Request::has('limit') ? Request::get('limit') : 10;
        $name_chinese = Request::has('name_chinese') ? Request::get('name_chinese') : '';
        $area_id = Request::has('area_id') ? Request::get('area_id') : '';
        $country_id = Request::has('country_id') ? Request::get('country_id') : '';
        $status = Request::has('status') ? Request::get('status') : '';
        $sureblur = Request::has('sureblurs') ? Request::get('sureblurs') : 'off';

        $where = array();
        $where[] = array('st_lq_odds.type', 1);
        if (!empty($name_chinese)) {
            if (empty($sureblur) || $sureblur == 'off') {
                $where[] = array('st_lq_league.name_chinese', 'like', '%' . $name_chinese . '%');
            }else{
                $where[] = array('st_lq_league.name_chinese', '=', $name_chinese);
            }
        }
//        if ($name_chinese != -1) {
//            $where[] = array('st_zq_league.name_chinese', '=', $name_chinese);
//        }
        if(!empty($area_id)){
            $where[] = array('st_lq_league.area_id', '=', $area_id);
        }
        if(!empty($country_id)){
            $where[] = array('st_lq_league.country_id', '=', $country_id);
        }
        if($status != -1){
            $where[] = array('st_lq_league.status', '=', $status);
        }

        $newapp = new \App\Models\BasketLeague();
        $data = $newapp->get_league_gj_info($list, $page, $where);
        $allcount = $newapp->join('st_lq_odds','st_lq_odds.lg_id', '=', 'st_lq_league.id')->where($where)->distinct('st_lq_league.name_chinese')->count('st_lq_league.name_chinese');
        return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $allcount);
    }

    /**
     *获取地区
     */
    function getarea() {
        $area = \App\Models\StAreaCountry::where('pid',0)->get();
        return $area;
    }

    /**
     *修改冠军联赛信息
     */
    function edit(Req $req) {
        $id = $req->id;
        if (intval($id) < 1) {
            return -1;
        }

        $areas = $this->getarea();
        if (!$areas) {
            return -2;
        }
        $data = \App\Models\BasketLeague::where('id',$id)->first();
        $areaname = \App\Models\StAreaCountry::where('id',$data->area_id)->first();
        $countryname = \App\Models\StAreaCountry::where('id',$data->country_id)->first();
        if(!empty($areaname)){
            $data->area = $areaname->name;
        }
        if(!empty($countryname)){
            $data->country = $countryname->name;
        }

        $odds_code = \App\Models\BasketOdds::where('lg_id',$id)->where('type',1)->where('odds_code','!=','')->select('odds_code')->distinct()->get()->toArray();//赔率表
        
        for ($i=0; $i < count($odds_code); $i++) { 
            $allteam[] = \App\Models\BasketOdds::where('odds_code',$odds_code[$i]['odds_code'])->where('lg_id',$id)->where('type',1)->select('team')->distinct()->get()->toArray();
        }

        $data = $data->toArray();
        $isdisplay = \App\Models\Stlqleagueresult::where(['lg_id'=>$id,'status'=>1])->select("game_name")->distinct()->get()->toArray();
        $idisplay = array_column($isdisplay,'game_name');
        $displays = implode(",",$idisplay);//转换成字符串

        //新增纪录
        foreach($odds_code as $k => $v) {
            foreach($allteam[$k] as $kk => $vv) {
                $name = $v['odds_code'];
                $isteam = $allteam[$k][$kk]['team'];
                $res = \App\Models\Stlqleagueresult::where(['lg_id'=>$id,'game_name'=>$name,'result'=>$isteam])->first();

                $addresult = [
                    'lg_id' => $id,
                    'game_name' => $name,
                    'result' => $isteam,
                    'ctime' => date('Y-m-d H:i:s',time()),
                    'status' => 0,
                ];

                if(empty($res)){
                    \App\Models\Stlqleagueresult::insert($addresult);
                }

            }
        }

        for ($i=0; $i < count($odds_code); $i++) { 
            $oddscode = $odds_code[$i]['odds_code'];
            $stdisplay[] = \App\Models\Stlqleagueresult::where(['lg_id'=>$id,'game_name'=>$oddscode])->select("id","status")->orderBy('id','desc')->distinct()->get()->toArray();
        }
        $data['allteam'] = $allteam;
        $data['odds_code'] = $odds_code;
        $data['displays'] = $displays;
        $data['isdisplay'] = $isdisplay;
        $data['stdisplay'] = $stdisplay;

        return view('sports.basket_champions_league_form', $data);
    }

    //修改状态
    function ajaxstatus(Req $req){
        $isstatus = $req->isstatus;
        $id = $req->id;

        \App\Models\Stlqleagueresult::where(['id'=>$id])->update(['status'=>$isstatus]);
        
        return responseToJson(1);
    }

}
