<?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 TeammanageController extends Controller {
	/**
	 * @return 球队管理列表
	 */
	function index(Req $req) {
		$request=array();
		$request['team_name'] = isset($req->team_name) ? trim($req->team_name) : null;
		$request['sureblurs'] = isset($req->sureblurs) ? $req->sureblurs : 'on';
		$request['status'] = isset($req->status) ? trim($req->status) : '-1';

        $dt = \App\Lib\DataTable\DataTable::init();
        $dt->setDataSource('/admin/Teammanage/tlist');
        $dt->setLang('teammanage');
        $dt->addColsFields('team_name_cn', array('templet' => '#team_name_cn', 'sort' => false, 'width' => 200));
        $dt->addColsFields('game_type', array('templet' => '#game_type', 'sort' => false, 'width' => 100));
        $dt->addColsFields('update_time', array('templet' => '#update_time', 'sort' => false, 'width' => 160));
        $dt->addColsFields('status', array('templet' => '#status', 'sort' => false, 'width' => 130));
        $dt->addColsFields('resultjg', array('templet' => '#resultjg', 'sort' => false, 'width' => 300));
        $dt->addColsFields('operation', array('templet' => '#operation', 'sort' => false, 'width' => 300));
		
        $dt->enableCheckBox();
		return view('admin.teammanage/index', $dt->render($request));
	}

	function tlist(){
		$page = Request::has('page') ? Request::get('page') : '';
		$list = Request::has('limit') ? Request::get('limit') : 10;
		$team_name = Request::has('team_name') ? Request::get('team_name') : '';
		$sureblur = Request::has('sureblurs') ? Request::get('sureblurs') : 'off';
		$status = Request::has('status') ? Request::get('status') : '';

		$where = array();
		if (!empty($team_name)) {
			if (empty($sureblur) || $sureblur == 'off') {
				$where[] = array('st_team.team_name_cn', 'like', '%' . $team_name . '%');
			} else {
				$where[] = array('st_team.team_name_cn', '=', $team_name);
			}
		}
		if ($status != -1) {
			$where[] = array('st_team.status', '=', $status);
		}

        $newapp = new \App\Models\Stteam();
        $data = $newapp->teamlist($list, $page, $where);
        
        return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
	}

	//所有球队名
	function allname(Req $req){
		$id = $req->id;
		$resultjg = $req->resultjg;
		$stationname = $req->stationname;
		if (!$req->isMethod('post')) {
			$request['oname'] = \App\Models\Stteamlist::where('pid', $id)->first();
			return view('admin.teammanage/allname',$request);
		} else {
            $model = \App\Models\Stteamlist::where('pid', $id)->first();
			$model->resultjg = $resultjg;
            $model->stationname = $stationname;

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

	//添加编辑球队名
	function addteamonly(Req $req){
		$id = $req->id;
		$teamnamecn = $req->teamnamecn;//布伦特福德

		$model = \App\Models\Stteamlist::where('pid', $id)->first();
		$nap = \App\Models\Stteam::where('team_name_cn', $teamnamecn)->first();
		$aid = $nap->toArray()['id'];
		$del = \App\Models\Stteamlist::where('pid', $aid)->first();
		$new = \App\Models\Stteam::where('id', $id)->first();
		if(count($model)==0){
			\App\Models\Stteamlist::updateOrCreate(['resultjg'=>$teamnamecn,'pid'=>$id,'update_time'=>date('Y-m-d H:i:s')]);
		}else{
			$re = \App\Models\Stteamlist::where('pid', $id)->select("resultjg")->first()->toArray();
			$rename = $re['resultjg'] .",". $teamnamecn;
			\App\Models\Stteamlist::where ('pid', $id)->update(['resultjg' =>$rename,'update_time'=>date('Y-m-d H:i:s')]);
		}

		if(count($del)==0){
			$nid = $nap->toArray()['id'];
			$jieg = $new->toArray()['team_name_cn'];
			$mid = \App\Models\Stteamlist::updateOrCreate(['resultjg'=>$jieg,'pid'=>$nid,'update_time'=>date('Y-m-d H:i:s')]);
		}else{
			$nid = $nap->toArray()['id'];
			$nem = $nap->toArray()['team_name_cn'];
			$ra = \App\Models\Stteamlist::where('pid', $nid)->select("resultjg")->first()->toArray();
			$raname = $ra['resultjg'] .",". $nem;
			\App\Models\Stteamlist::where ('pid', $id)->update(['resultjg' =>$raname,'update_time'=>date('Y-m-d H:i:s')]);
		}

		return responseToJson(1);
	}

	//查询匹配的球队
	function teamresult(Req $req){
		$teamsousuo = $req->teamsousuo;
		$id = $req->id;
		$new = \App\Models\Stteam::where('id', $id)->select("game_type")->first()->toArray();
		$ttype = $new['game_type'];
		$where = array();
		$where[] = array('team_name_cn', 'like', '%' . $teamsousuo . '%');
		$where[] = array('game_type',$ttype);
		$where[] = array('id', '!=', $id);

		$model = \App\Models\Stteam::where($where)->select("team_name_cn")->get()->toArray();
		return $model;
	}


}

?>