<?php

namespace App\Http\Controllers\Admin;

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

/**
 *
 */
class SportsbkController extends Controller {
	//篮球结果列表
	public function outcome(Req $req) {
		$request['home_team'] = isset($req->home_team) ? trim($req->home_team) : null;
		$request['status'] = isset($req->status) ? trim($req->status) : '-1';
		$request['source'] = isset($req->source) ? $req->source : null;
		$request['sureblurs'] = isset($req->sureblurs) ? $req->sureblurs : 'on';
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/Sportsbk/info');
		$dt->setLang('sportsbk');
		$dt->addColsFields('lg_id', array('templet' => '#liansai', 'sort' => true, 'width' => 200));
		$dt->addColsFields('update_time', array('templet' => '#userdetail', 'sort' => true, 'width' => 160));
		$dt->addColsFields('home_team', array('templet' => '#userdetail', 'sort' => false, 'width' => 120));
		$dt->addColsFields('guest_team', array('templet' => '#userdetail', 'sort' => false, 'width' => 130));
		$dt->addColsFields('dsnum', array('templet' => '#dsnum', 'sort' => false, 'width' => 70));
		$dt->addColsFields('csnum', array('templet' => '#csnum', 'sort' => false, 'width' => 70));
		$dt->addColsFields('sectionone_two', array('templet' => '#sectionone_two', 'sort' => false, 'width' => 200));
		$dt->addColsFields('sectionthere_four', array('templet' => '#sectionthere_four', 'sort' => false, 'width' => 200));
		$dt->addColsFields('status', array('templet' => '#matchstatus', 'sort' => false, 'width' => 100));
		if (checkRriv('/admin/Sportsbk/edit')) {
			$arr[] = 'edit';
		}
		
		$dt->setToolBar($arr, array('width' => 200));
		$dt->enableCheckBox();
		return view('admin/sportsbk/outcome', $dt->render($request));
	}
	
	//查询赛事结果
	function Matchresult(Req $req){
		$match_id = $req->match_id;
		$newapp = \App\Models\Stlqresult::where('match_id', $match_id)->first();

		$array =array(
			'corner_ball' => json_decode($newapp['corner_ball'],true),
			'penalty_card' => json_decode($newapp['penalty_card'],true),
			'newapp' => $newapp,
		);
		return $array;
	}
	
	//结算
	function Settlementlq(Req $req){
		//查询赛事下所有订单
		$ssid = $req->match_id;//赛事id
		$type = $req->type;//是否再结算1,首次结算2,再次结算
		$newapp = new \App\Models\MoneyBuyMatch();
		$simplex_ids = array_column($newapp->allsimplexorder($ssid,'lq'),'order_id');
		$str_ids = array_column($newapp->allstrorder($ssid),'order_id');

		$update = $this->addcomendnotice($ssid);
		if($update['status']==1){
			$SettlementOrder = new SettlementOrder();
	        $SettlementOrder->reSettlement($str_ids, 2,$type,'lq',$ssid);//串式
	        $data = $SettlementOrder->reSettlement($simplex_ids, 1,$type,'zq',$ssid);//单式注单
	        return responseToJson($data);
	    }else{
	    	return responseToJson($update);
	    }
	}
	
	function info() {
		$page = Request::has('page') ? Request::get('page') : '';
		$list = Request::has('limit') ? Request::get('limit') : 1;
		$home_team = Request::has('home_team') ? Request::get('home_team') : '';
		$sureblurs = Request::has('sureblurs') ? Request::get('sureblurs') : 'off';
		$status = Request::has('status') ? Request::get('status') : '';
		$source = Request::has('source') ? Request::get('source') : '';
		$where = array();
		$orwhere = array();
		if (!empty($home_team)) {
			if (empty($sureblurs) || $sureblurs == 'off') {
				$where[] = array('st_lq_result.home_team', 'like', '%' . $home_team . '%');
				$orwhere[] = array('st_lq_result.guest_team', 'like', '%' . $home_team . '%');
			} else {
				$where[] = array('st_lq_result.home_team', '=', $home_team);
				$orwhere[] = array('st_lq_result.guest_team', '=', $home_team);
			}
		}
		if ($status != -1) {
			$where[] = array('st_lq_result.status', '=', $status);
		}
		if (!empty($source)) {
			$where[] = array('st_lq_result.source', '=', $source);
		}//var_dump($where);die;
		$newapp = new \App\Models\Stlqresult();
		$data = $newapp->resultbklist($list, $page, $where, $orwhere);
		
		for ($i=0; $i < count($data['data']); $i++) { 
			$match_id = $data['data'][$i]['match_id'];
			$score = $newapp->allscore($match_id,5);

			if(!empty($score[0])){
				$data['data'][$i]['okteamscore'] = $score[0]['guest_score']?$score[0]['guest_score']:0;//第一节客队比分
				$data['data'][$i]['ozteamscore'] = $score[0]['home_score']?$score[0]['home_score']:0;//第一节主队比分
			}else{
				$data['data'][$i]['okteamscore'] = 0;//第一节客队比分
				$data['data'][$i]['ozteamscore'] = 0;//第一节主队比分
			}
			if(!empty($score[1])){
				$data['data'][$i]['tkteamscore'] = $score[1]['guest_score']?$score[1]['guest_score']:0;//第二节客队比分
				$data['data'][$i]['tzteamscore'] = $score[1]['home_score']?$score[1]['home_score']:0;//第二节主队比分
			}else{
				$data['data'][$i]['tkteamscore'] = 0;//第二节客队比分
				$data['data'][$i]['tzteamscore'] = 0;//第二节主队比分
			}
			if(!empty($score[2])){
				$data['data'][$i]['skteamscore'] = $score[2]['guest_score']?$score[2]['guest_score']:0;//第三节客队比分
				$data['data'][$i]['szteamscore'] = $score[2]['home_score']?$score[2]['home_score']:0;//第三节主队比分
			}else{
				$data['data'][$i]['skteamscore'] = 0;//第三节客队比分
				$data['data'][$i]['szteamscore'] = 0;//第三节主队比分
			}
			if(!empty($score[3])){
				$data['data'][$i]['akteamscore'] = $score[3]['guest_score']?$score[3]['guest_score']:0;//第四节客队比分
				$data['data'][$i]['azteamscore'] = $score[3]['home_score']?$score[3]['home_score']:0;//第四节主队比分
			}else{
				$data['data'][$i]['akteamscore'] = 0;//第四节客队比分
				$data['data'][$i]['azteamscore'] = 0;//第四节主队比分
			}
		    // unset($data['data'][$i]['match_process'],$data['data'][$i]['id'],$data['data'][$i]['home_score'],$data['data'][$i]['guest_score'],$data['data'][$i]['update_time']); //去重
		}

		// $data['data'] = array_unique($data['data'], SORT_REGULAR); //去重
		return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
	}

	//修改添加赛事比分
	function updatascore(Req $req){
		$only = $req->only;//节数唯一标识
		$num = $req->num;//比分
		$match_id = $req->match_id;//节数唯一标识
		$okteamscore = $req->okteamscore;//第一节客队比分
		$ozteamscore = $req->ozteamscore;//第一节主队比分
		$tkteamscore = $req->tkteamscore;//第二节客队比分
		$tzteamscore = $req->tzteamscore;//第二节主队比分
		$skteamscore = $req->skteamscore;//第三节客队比分
		$szteamscore = $req->szteamscore;//第三节主队比分
		$akteamscore = $req->akteamscore;//第四节客队比分
		$azteamscore = $req->azteamscore;//第四节主队比分

		$newapp = new \App\Models\Stlqresult();
		$alldata = $newapp->onlyselect($match_id);
		
		if($only==1 || $only==2){
			$ret = \App\Models\Stlqresult::updateOrCreate(['match_id'=>$match_id,'match_process'=>1],array_merge($alldata,['home_score'=>intval($ozteamscore),'guest_score'=>intval($okteamscore),'update_time'=>date('Y-m-d H:i:s'),'match_process'=>1]));
		}
		if($only==3 || $only==4){
			$ret = \App\Models\Stlqresult::updateOrCreate(['match_id'=>$match_id,'match_process'=>2],array_merge($alldata,['home_score'=>intval($tzteamscore),'guest_score'=>intval($tkteamscore),'update_time'=>date('Y-m-d H:i:s'),'match_process'=>2]));
		}
		if($only==5 || $only==6){
			$ret = \App\Models\Stlqresult::updateOrCreate(['match_id'=>$match_id,'match_process'=>3],array_merge($alldata,['home_score'=>intval($szteamscore),'guest_score'=>intval($skteamscore),'update_time'=>date('Y-m-d H:i:s'),'match_process'=>3]));
		}
		if($only==7 || $only==8){
			$ret = \App\Models\Stlqresult::updateOrCreate(['match_id'=>$match_id,'match_process'=>4],array_merge($alldata,['home_score'=>intval($azteamscore),'guest_score'=>intval($akteamscore),'update_time'=>date('Y-m-d H:i:s'),'match_process'=>4]));
		}
		return responseToJson(1); 
	}

	function saislist(){
		$lgid = $_POST['lgid'];//赛事id
		$newstzqcompetition = new \App\Models\SportsBasket();
		$league_data = $newstzqcompetition->allcompetition($lgid);//赛事id

		return $league_data;
	}

	function addoutcome(Req $req) {
		if (!$req->isMethod('post')) {
            $lange = trans('menu');
			$newapp = new \App\Models\BasketLeague();
			$league_data = $newapp->allleague();//联赛id
			return view('admin.sportsbk/addoutcome',['data'=>$league_data]);
		}else{
			$model = new \App\Models\Stlqresult();
			$model->lg_id = trim($req->input('lg_id'));//联赛id
			$model->home_team = trim($req->input('home_team'));
			$model->guest_team = trim($req->input('guest_team'));
			$model->home_rate = trim($req->input('home_rate'));
			$model->guest_rate = trim($req->input('guest_rate'));
			$model->home_score = trim($req->input('home_score'));
			$model->guest_score = trim($req->input('guest_score'));
			$model->all_goal = trim($req->input('all_goal'));
			$model->match_score = trim($req->input('match_score'));
			$model->u_home_score = trim($req->input('u_home_score'));
			$model->u_guest_score = trim($req->input('u_guest_score'));
			$model->update_time = date('Y-m-d h:i:s', time());//更新时间
			$model->match_time = "11:22";//trim($req->input('match_time'));
			$model->tag = trim($req->input('tag'));
			$model->status = trim($req->input('status'));
			$model->match_process = trim($req->input('match_process'));
			$model->match_id = trim($req->input('match_id'));//赛事id

			$model->save();
			return responseToJson(1);

		}
	}

	//赛事结果修改
	function edit(Req $req) {
		$id = $req->id;
		if (intval($id) < 1) {
			return -1;
		}
		if (!$req->isMethod('post')) {

			$data = \App\Models\Stlqresult::where('id', $id)->first();
			if (!$data) {
				return -2;
			}
			$data = $data->toArray();

			return view('admin.sportsbk/edit', $data);
		} else {
			$model = \App\Models\Stlqresult::where('id', $id)->first();
			$model->home_team = $req->input('home_team');
			$model->guest_team = $req->input('guest_team');
			$model->home_rate = $req->input('home_rate');
			$model->guest_rate = $req->input('guest_rate');
			$model->status = $req->input('status');
			//$model->update_time = time();
			$model->save();
			return responseToJson(1);
		}
	}

	//删除赛事结果
	function deleteoutcome(Req $req) {
		$id = $req->input('id');
		if (empty($id)) {
			return responseToJson(-2001); //
		}
		$ids = explode(',', $id);
		if (!is_array($ids) && intval($ids) < 0) {
			return responseToJson(-2002); //
		}
		if (is_array($ids) && count($ids) > 0) {
			foreach ($ids as $k => $v) {
				if (intval($v) < 1) {
					unset($ids[$k]);
				}
			}
		}
		// echo '敬请期待';die;
		$rows = \App\Models\Stlqresult::whereIn('id', $ids)->delete();
		if (!$rows) {
			return responseToJson(-2003); 
		}
		return responseToJson(1); 
	}


}