<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request as Req;
use Illuminate\Support\Facades\DB;
use Request;

/**
 *赛事场次管理
 */
class CompetitionController extends Controller {
	public function Index(Req $req){
        $request=array();
        $request['match_id']=isset($req->match_id)?trim($req->match_id):null;//赛事id

        $request['game']=isset($req->game)?trim($req->game):null;//游戏名
        //开始时间
        $request['start_time_a']=isset($req->start_time_a)?trim($req->start_time_a):null;
        $request['start_time_b'] = isset($req->start_time_b) ? trim($req->start_time_b) : null;
        //结束时间
        $request['end_time_a'] = isset($req->end_time_a) ? trim($req->end_time_a) : null;
        $request['end_time_b'] = isset($req->end_time_b) ? trim($req->end_time_b) : null;
        //赛事名称
        $request['info_name']=isset($req->info_name )?$req->info_name :null;
        $request['sub_title']=isset($req->sub_title )?$req->sub_title :null;
        $request['info_no']=isset($req->info_no )?$req->info_no :null;
        //状态
        $request['status']=isset($req->status)?$req->status:null;
        $dt = \App\Lib\DataTable\DataTable::init();
        $dt->setDataSource('/admin/Competition/dataSource?match_id='.$request['match_id']);
        $dt->setLang('competition');
        $dt->addColsFields('game_name', array('sort' => false,'width'=>150));
        $dt->addColsFields('info_name', array('sort' => false,'width'=>150));
        $dt->addColsFields('sub_title', array('sort' => false,'width'=>150));
        $dt->addColsFields('info_no', array('sort' => false,'width'=>187));
        $dt->addColsFields('start_time', array('sort' => false,'width'=>200));
        $dt->addColsFields('end_time', array('sort' => false,'width'=>200));
        $dt->addColsFields('status', array('templet'=>'#status'));
        $dt->addColsFields('tools',array('templet' => '#tools'));
        return view('admin.competition/index', $dt->render($request));
	}
	//数据源
	function dataSource(Req $req){

        $match_id=isset($req->match_id)?intval($req->match_id):'';//赛事id

        $game=isset($req->game)?trim($req->game):null;//游戏名
        //开始时间
        $start_time_a=isset($req->start_time_a)?trim($req->start_time_a):null;
        $start_time_b = isset($req->start_time_b) ? trim($req->start_time_b) : null;
        //结束时间
        $end_time_a = isset($req->end_time_a) ? trim($req->end_time_a) : null;
        $end_time_b = isset($req->end_time_b) ? trim($req->end_time_b) : null;
        //赛事名称
        $info_name=isset($req->info_name)?$req->info_name:null;
        //场次
        $sub_title=isset($req->sub_title)?$req->sub_title:null;
        $info_no=isset($req->info_no)?trim($req->info_no):null;
        //状态
        $status =isset($req->status)?$req->status:null;
        //条数
        $list=isset($req->limit)?intval($req->limit):10;
        $where=array();//条件数组
        if(!empty($match_id)){
            $where[]=array('match_id','=',$match_id);
        }
        if(!empty($game)){
            $where[]=array('game_name','=',$game);
        }
        if(!empty($start_time_a)){
            $start_time_a=$start_time_a.' 00:00:00';
            $where[]=array('start_time','>=',$start_time_a);
        }
        if(!empty($start_time_b)){
            $start_time_b=$start_time_a.' 23:59:59';
            $where[]=array('start_time','>=',$start_time_b);
        }
        if(!empty($end_time_a)){
            $end_time_a=$end_time_a.' 00:00:00';
            $where[]=array('end_time','>=',$end_time_a);
        }
        if(!empty($end_time_b)){
            $end_time_b=$end_time_b.' 23:59:59';
            $where[]=array('start_time','>=',$end_time_b);
        }
        if(!empty($info_name)){
            $where[]=array('info_name','like','%'.$info_name.'%');
        }
        if(!empty($sub_title)){
            $where[]=array('sub_title','like','%'.$sub_title.'%');
        }
        if(!empty($status)){
            $where[]=array('status','=',$status);
        }
        if(!empty($info_no)){
            $where[]=array('info_no','=',$info_no);
        }
      $data=\App\Models\Egame::getInfoList($where,$list);

      return \App\Lib\DataTable\DataTable::init()->toJson($data['data'],$data['total']);
    }

    //详情查看
    function View(Req $req){
        $info_no=$req->info_no?trim($req->info_no):'';
        if(empty($info_no)){
            echo '没获取正确的区号';
            return -1;
        }
        $data=\App\Models\Egame::getDetail($info_no);
//        dump($data);
        return view('/admin/competition/view',['data'=>$data]);

    }

    //新增游戏期数
    function Add(Req $req){
        $match_id=$req->match_id?intval($req->match_id):'';
        $match_info=array();
        $game=$req->game?trim($req->game):'';
        if(empty($match_id)){
            $match_info=\App\Models\Egame::getNowMatch($game);
            if($match_info<0){
                echo '没有对应赛事';
                return -1;
            }
            $match_id=$match_info['match_id'];
        }
        if(!$req->isMethod('post')){

            //获取赛事信息
            $match_info=\App\Models\Egame_match::getInfoByID($match_id);
            if(!empty($game)){
                $match_info['game_name']=$game;
            }

            //获取赛事队伍
            $team_info=\App\Models\Egame_team::getMatchTeam($match_id);
            if($team_info<0||empty($team_info)){
                echo '请先添加赛事参赛队伍';
                return $team_info;
            }
            return view('admin.competition/add',['match'=>$match_info,'team'=>$team_info]);
        }else{
            $info_no=$req->info_no?trim($req->info_no):'';
            $info_name=$req->info_name?trim($req->info_name):'';
            $start_time=$req->start_time?$req->start_time:'';
            $end_time=$req->end_time?$req->end_time:'';
            $sub_title=$req->sub_title?trim($req->sub_title):'';
            $sealingplate=$req->sealingplate?trim($req->sealingplate):'';
            $game_name=$req->game_name?$req->game_name:'';
            $video_url=$req->video_url?$req->video_url:'';
            $team_a_identity=$req->team_a_identity?trim($req->team_a_identity):'';
            $team_b_identity=$req->team_b_identity?trim($req->team_b_identity):'';
            /****************************数据检测********************************/
            if(empty($info_no)){
                return responseToJson(-9040410422);
            }
            if(empty($info_name)){
                return responseToJson(-9040410522);
            }
            if(empty($start_time)){
                return responseToJson(-9040410622);
            }
            if(empty($end_time)){
                return responseToJson(-9040410722);
            }
            if(empty($sealingplate)){
                return responseToJson(-9040410822);
            }
            if(empty($sub_title)){
                return responseToJson(-9040410922);
            }
            if(empty($game_name)){
                return responseToJson(-9040411022);
            }
            if(empty($team_a_identity)||empty($team_b_identity)){
                return responseToJson(-9040411122);
            }
            if($team_a_identity==$team_b_identity){
                //选择了相同队伍
                return responseToJson(-9040411222);
            }
            //检测期号是否重复
            $check=\App\Models\Egame::checkNo($info_no);
            if($check<0){
                return responseToJson($check);
            }
            /***************************插入数据************************************/
            DB::beginTransaction();
            //egame
            $data=array(
                'info_no'=>$info_no,
                'info_name'=>$info_name,
                'start_time'=>$start_time,
                'end_time'=>$end_time,
                'sub_title'=>$sub_title,
                'sealingplate'=>$sealingplate,
                'game_name'=>$game_name,
                'match_id'=>$match_id,
            );
            $res=\App\Models\Egame::addNew($data);
            if($res<0){
                DB::rollback();
                return responseToJson($res);
            }
            //比赛队伍表
            //获取队伍信息
            $team_a_info=\App\Models\Egame_team::getInfoByID($team_a_identity);
            $team_b_info=\App\Models\Egame_team::getInfoByID($team_b_identity);
            $team_vs_data=array(
                'team_a_code'=>'first',
                'team_a_name'=>$team_a_info['team_name'],
                'team_a_logo'=>$team_a_info['team_icon'],
                'team_a_identity'=>$team_a_identity,
                'team_b_code'=>'second',
                'team_b_name'=>$team_b_info['team_name'],
                'team_b_logo'=>$team_b_info['team_icon'],
                'team_b_identity'=>$team_b_identity,
                'info_no'=>$info_no,
                'info_name'=>$info_name,
                'video_url'=>$video_url
            );
            $res=\App\Models\Egame_info::addNewMatch($team_vs_data);
            if($res<0){
                DB::rollback();
                return responseToJson($res);
            }
            DB::commit();
            return responseToJson($res);
        }
    }

}