<?php
    $division = Session::get('division');

    $all_leagues = DB::table('leagues')->where('is_retired', '=', 0)->orderBy('id', 'asc')->get();

    $leagues = array();

    foreach ($all_leagues as $league)
    {
    	if (in_array($division, explode(',', $league->divisions_list)) or $division == str_replace(',', '/', $league->divisions_list))
    	{
    		$leagues[] = $league;
    	}    		    	
    }
   
    /*
	// Sort leagues for display order by division then league number
	uksort ($leagues, function ($b, $a) {
	      
	    $cls_a = substr ($a, 0, 1);
	    $cls_b = substr ($b, 0, 1);

		$sd_a = (stristr($a, 'SD') !== false);
		$sd_b = (stristr($b, 'SD') !== false);

		$comb_a = (stristr($a, '/') !== false);
		$comb_b = (stristr($b, '/') !== false);

		$num_a = substr ($a, strpos($a, '-') + 1, 1);
		$num_b = substr ($b, strpos($b, '-') + 1, 1);

		if ($cls_a > $cls_b) return -1;
		if ($cls_a < $cls_b) return 1;

		if ($sd_a and !$sd_b)
		{
		    return 1;
		}
		if (!$sd_a and $sd_b)
		{
			return -1;
		}

		if ($comb_a and $comb_b)
		{
		  if ($num_a < $num_b) return -1;
		  if ($num_a > $num_b) return 1;
		}
		else if ($comb_a and !$comb_b)
		{
		  return 1;
		}
		else if (!$comb_a and $comb_b)
		{
		  return -1;
		}          

		if ($num_a < $num_b) return -1;
		if ($num_a > $num_b) return 1;          

		return 0;
    });*/


	//echo "<div class=\"columns\"><div class=\"half\">";

	
	$i = 0;
	foreach ($leagues as $league)
	{	
		// Get all schools in this league
        $programs = DB::table('activity_programs')->where('league', '=', $league->slug)->where('activity', '=', $activity_slug)->get();

        if (count ($programs) > 0)
        {
			
			if ($i > count ($leagues) / 2)
			{
				echo "</div><div class=\"half\">";
				$i = 0;
			}

			echo "<h2>" . $league->slug . " " . $league->name . "</h2>";

	        foreach ($programs as $program)
	        {	        	
	        	$mascot_id = (is_null($program->mascot)) ? school::find($program->host_school)->mascot : $program->mascot;

	        	$mascot = mascot::find($mascot_id)->name;

	        	echo "<a href=\"#\" style=\"font-weight:bold;\">" . $program->name . "</a> <i>" . $mascot ."</i><br />";
	        }	
        }        
	}




	$programs = DB::table('activity_programs')->where('league', '=', null)->where('activity', '=', $activity_slug)->get();

	$independents = array();
	foreach ($programs as $program)
	{
	    if ($division == $program->division) $independents[] = $program;
	}

	if (count($independents) > 0)
	{
		echo "<h2>Independent / No League Assigned</h2>";

        foreach ($independents as $program)
        {
        	echo "<a href=\"#\">" . $program->name . "</a><br />";
        }	

	}

	


	//echo "</div>";
	//echo "</div>";

	echo "&nbsp;";

	


/*
      
      // Go through each league
      foreach ($slugs as $slug => $name)
      {
          echo "<b>$slug $name</b><br />";
      
          // Get all schools in this league
          $schools = DB::table('league_schools')->remember(10)->join('schools', 'schools.id', '=', 'league_schools.school')->where('league', '=', $slug)->orderBy('schools.short_name')->get();

          // Go through each school in this league
          foreach ($schools as $school)
          {                            
              // Get the school's demographics
              $demographics = DB::table('school_demographics')->remember(10)->where('school', '=', $school->id)->first();
              
              $is_irregular = DB::table('classification_schools')->remember(10)->where('school', '=', $school->id)->pluck('is_irregular');

              echo '<div style="width:150px; float:left; overflow:hidden; white-space:nowrap; margin-right:10px;">';
              echo ($is_irregular) ? '*' : '';
              echo $school->short_name . '</div><div style="float:left;">';
              echo ($demographics->enrollment > 0) ? $demographics->enrollment : 'NEW';
              echo '</div><br />';             
           

          }

          echo '<br />';
      }
      
*/

?>

