<?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 GjmatchController extends Controller {

	//
	public function index(Req $req){
		$request=array();
		$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';
		$request['home_team'] = isset($req->home_team) ? trim($req->home_team) : null;
		$dt = \App\Lib\DataTable\DataTable::init();
        $dt->setDataSource('/admin/gjmatch/info');
        $dt->setLang('gjmatch');
        $dt->addColsFields('id', array('templet' => '#home_team', 'sort' => false, 'width' => 60));
        $dt->addColsFields('name_chinese', array('templet' => '#guest_team', 'sort' => false, 'width' => 200));
        $dt->addColsFields('game_name', array('templet' => '#home_rate', 'sort' => false, 'width' => 130));
        $dt->addColsFields('ctime', array('templet' => '#home_rate', 'sort' => false, 'width' => 180));
        $dt->addColsFields('utime', array('templet' => '#home_rate', 'sort' => false, 'width' => 180));
        $dt->addColsFields('status', array('templet' => '#status', 'sort' => false, 'width' => 80));

        if (checkRriv('/admin/gjmatch/edit')) {
			$arr[] = 'edit';
		}
		$dt->setToolBar($arr, array('width' => 200));
        $dt->enableCheckBox();

		return view('admin/gjmatch/index',$dt->render($request));
	}

	//添加
	public function add(Req $req) {
		if (!$req->isMethod('post')) {
            $lange = trans('menu');
			$newapp = new \App\Models\SoccerLeague();
			$league_data = $newapp->allleague();//联赛id

            return view('admin.gjmatch/add',['data'=>$league_data]);
        } else {
            $model = new \App\Models\Stzqleagueresult();
			$model->lg_id = trim($req->input('lg_id'));//联赛id
			$model->game_name = trim($req->input('game_name'));
			$model->result = json_encode(explode(',', trim($req->input('result'))),JSON_UNESCAPED_UNICODE);
			$model->ctime = date("Y-m-d H:i:s");//创建时间
			$model->status = trim($req->input('status'));
			$model->save();
			return responseToJson(1);
        }
	}

	//编辑
	public function edit(Req $req) {
		$id = $req->id;
		if (intval($id) < 1) {
			return -1;
		}
		if (!$req->isMethod('post')) {

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

			$newapp = new \App\Models\SoccerLeague();
			$league_data = $newapp->allleague();//所有联赛

			//已选择的联赛相信
			$only = $newapp->onlyleague($data['lg_id']);

			return view('admin.gjmatch/edit', ['data'=>$data,'ldata'=>$league_data,'only'=>$only]);
		} else {
			$model = \App\Models\Stzqleagueresult::where('id', $id)->first();
			$model->game_name = $req->input('game_name');
			$model->utime = date("Y-m-d H:i:s");//更新时间
			$model->lg_id = $req->input('lg_id');//赛事id
			$model->status = $req->input('status');

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

	}

	public function info(){
		$page = Request::has('page') ? Request::get('page') : '';
		$list = Request::has('limit') ? Request::get('limit') : 10;
		$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') : '';
        $where = array();
        if (!empty($home_team)) {
			if (empty($sureblurs) || $sureblurs == 'off') {
				$where[] = array('st_zq_league_result.game_name', 'like', '%' . $home_team . '%');
			} else {
				$where[] = array('st_zq_league_result.game_name', '=', $home_team);
			}
		}
        if ($status != -1) {
			$where[] = array('st_zq_league_result.status', '=', $status);
		}
		if (!empty($source)) {
			$where[] = array('st_zq_league_result.source', '=', $source);
		}
		$newapp = new \App\Models\Stzqleagueresult();
        $data = $newapp->resultlist($list, $page, $where);
        
        return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
	}

	//删除
	public function dele(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\Stzqleagueresult::whereIn('id', $ids)->delete();
		if (!$rows) {
			return responseToJson(-2003); 
		}
		return responseToJson(1);
	}

}