<?php
  
namespace App\Http\Controllers;
  
use App\Team;
use App\Category;
use App\Group;
use App\Season;
use Illuminate\Http\Request;
use Session;
use Redirect;

class TeamController extends Controller
{
    
    protected $rules = [
        'name' => ['required']
    ];

    public function index()
    {
        
        
        $type = isset($_GET["type"]) ? $_GET["type"] : '';
        $categories = array();
        if ($type != '')
            $categories = Category::where('type', '=', $type)->orderBy('name')->pluck('name', 'id')->toArray();

        $category_id = isset($_GET["category_id"]) ? $_GET["category_id"] : '';
        $groups = array();
        if ($category_id != '')
            $groups = Group::where('category_id', '=', $category_id)->orderBy('name')->pluck('name', 'id')->toArray();

        $group_id = isset($_GET["group_id"]) ? $_GET["group_id"] : '';

        if (($category_id != '' && sizeof($groups) == 0) || $group_id != '')
        {
            $teams = Team::orderBy('name')->get(); //->paginate(50);
            $aIds = array();
            foreach($teams as $t)
            {
                $c = $t->getCalendar();
                if (@$c->category_id == $category_id)
                {
                    if ($group_id != '')
                    {
                        if (@$c->group_id == $group_id)
                            $aIds[] = $t->id;
                    }
                    else
                        $aIds[] = $t->id;
                }
                    
            }
            $teams = Team::whereIn('id', $aIds); //->paginate(50);
            
        }
        else
        {
            $teams = Team::where('id', '=', -1); //->paginate(50);
        }

        $season_id = isset($_GET["season_id"]) ? $_GET["season_id"] : '';
        if ($season_id != "")
            $teams->where('season_id', '=', $season_id);
        $teams = $teams->orderBy('name')->get();

        $seasons = Season::all()->pluck('name', 'id')->toArray();
        Session::put('type', $type);
        Session::put('category_id', $category_id);
        Session::put('group_id', $group_id);
        Session::put('season_id', $season_id);


        return view('teams.index',compact('teams', 'categories', 'groups', 'seasons'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
    }
   
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('teams.create');
    }
  
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate($this->rules);
  
        $input = $request->all();
        
        $input["excluded"] = isset($input["excluded"]) ? ($input["excluded"] == 'on' ? true : false) : false;

        Team::create($input);
   
        return redirect()->route('teams.index')
                        ->with('success','Team created successfully.');
    }
   
    /**
     * Display the specified resource.
     *
     * @param  \App\Team  $team
     * @return \Illuminate\Http\Response
     */
    public function show(Team $team)
    {
        return view('teams.show',compact('team'));
    }
   
    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Team  $team
     * @return \Illuminate\Http\Response
     */
    public function edit(Team $team)
    {
        return view('teams.edit',compact('team'));
    }
  
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Team  $team
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Team $team)
    {
        $request->validate($this->rules);
  
        $input = $request->all();

        $input["excluded"] = isset($input["excluded"]) ? ($input["excluded"] == 'on' ? true : false) : false;
        
        $team->update($input);

        return Redirect::to('/admin/teams/?type=' . Session::get('type') . "&category_id=" . Session::get('category_id') . "&group_id=" . Session::get('group_id'). "&season_id=" . Session::get('season_id'));
  
        // return redirect()->route('teams.index')->with('success','Team updated successfully');
    }
  
    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Team  $team
     * @return \Illuminate\Http\Response
     */
    public function destroy(Team $team)
    {
        $team->delete();

        return Redirect::to('/admin/teams/?type=' . Session::get('type') . "&category_id=" . Session::get('category_id') . "&group_id=" . Session::get('group_id'));
  
        //return redirect()->route('teams.index')->with('success','Team deleted successfully');
    }
}