<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request as Req;
use Illuminate\Support\Facades\DB;
Use App\Lib\Settlement\SettlementWinFail;
use App\Models;
use Request;

/**
 *
 */
class BaseLeagueController 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['lg_id'] = isset($req->lg_id) ? trim($req->lg_id) : '';
        $request['status'] = isset($req->status) ? trim($req->status) : '0';
        $request['sureblurs'] = isset($req->sureblurs) ? $req->sureblurs : 'on';
        $data = \App\Models\BaseLeague::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/BaseLeague/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));
        if (checkRriv('/admin/BaseLeague/edit')) {
            $arr[] = 'edit';
        }
        $dt->setToolBar($arr, array('width' => 140));
        $dt->enableCheckBox();
        return view('sports/base_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();
        if (!empty($name_chinese)) {
            if (empty($sureblur) || $sureblur == 'off') {
                $where[] = array('st_bq_league.name_chinese', 'like', '%' . $name_chinese . '%');
            }else{
                $where[] = array('st_bq_league.name_chinese', '=', $name_chinese);
            }
        }
//        if ($name_chinese != -1) {
//            $where[] = array('st_bq_league.name_chinese', '=', $name_chinese);
//        }
        if(!empty($area_id)){
            $where[] = array('st_bq_league.area_id', '=', $area_id);
        }
        if(!empty($country_id)){
            $where[] = array('st_bq_league.country_id', '=', $country_id);
        }
        if($status != 0){
            $where[] = array('st_bq_league.status', '=', $status);
        }

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

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

    /**
     *获取该地区的所有国家
     */
    function getcountry() {
        $pid = $_GET["id"];
        $country = \App\Models\StAreaCountry::where('pid',$pid)->get();
        return json_encode($country);
    }

    /**
     *添加联赛
     */
    function add(Req $req) {
        if (!$req->isMethod('post')) {
            $areas = $this->getarea();
            if (!$areas) {
                return -2;
            }
            $data['area'] = $areas;
            return view('sports.base_league_form', $data);
        } else {
            $model = new \App\Models\BaseLeague();
            $model->name_chinese = $req->input('name_chinese');
            $model->country_id = $req->input('country_id');
            $model->area_id = $req->input('area_id');
            $model->status = $req->input('status');
            $model->lg_id = time();
            $model->utime = date('Y-m-d H:i:s',time());
            $model->source = 'user-defined';

            $db = new \App\Models\Stbqlocalleague();
            $db->source = 'user-defined';
            $db->lg_id = $model->lg_id;

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

        }
    }

    function view(Req $req) {
        return $this->edit($req);
    }

    /**
     *修改联赛
     */
    function edit(Req $req) {
        $id = $req->id;
        if (intval($id) < 1) {
            return -1;
        }
        if (!$req->isMethod('post')) {
            $areas = $this->getarea();
            if (!$areas) {
                return -2;
            }
            $data = \App\Models\BaseLeague::where('lg_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;
            }

            $data = $data->toArray();
            $data['area'] = $areas;

            return view('sports.base_league_form', $data);
        } else {
            $model = new \App\Models\BaseLeague();
            $model->id = $id;
            $model = $model::find($model->id);
            if(!empty($req->input('country_id'))){
                $model->country_id = $req->input('country_id');
            }else{
                $model->country_id = 0;
            }
            if(!empty($req->input('area_id'))){
                $model->area_id = $req->input('area_id');
            }else{
                $model->area_id = 0;
            }
            //$model->status = $req->input('status');
            $model->utime = date('Y-m-d H:i:s',time());
            $model->save();
            return responseToJson(1);
        }
    }
    /**
     *删除联赛
     */
    public function delete(Req $req) {
        $id = $req->input('id');
        if (empty($id)) {
            return responseToJson(-2001); //id???????
        }
        $ids = explode(',', $id);
        if (!is_array($ids) && intval($ids) < 0) {
            return responseToJson(-2002); //id????
        }
        $id = array();
        $localleague = \App\Models\Stbqlocalleague::get();
        if (is_array($ids) && count($ids) > 0) {
            foreach ($ids as $k => $v) {
                if (intval($v) < 1) {
                    unset($ids[$k]);
                }
                $league = \App\Models\BaseLeague::where('id',$v)->first();
                for($i=0;$i<count($localleague);$i++){
                    if($localleague[$i]->lg_id == $league->lg_id){
                        $id[] = $localleague[$i]->id;
                    }
                }
            }
        }


        $rows = \App\Models\BaseLeague::whereIn('id', $ids)->delete();
        if (!$rows) {
            return responseToJson(-2003); //id????
        }
        if(count($id)>0) {
            $row = \App\Models\Stbqlocalleague::whereIn('id', $id)->delete();
            if (!$row) {
                return responseToJson(-2003); //id????
            }
        }
        return responseToJson(1, trans('menu.delete_success')); //id????

    }


}