<?php
  session_start();

  $lgid=$_GET['league_id'];
  if (!is_numeric($lgid)) {$lgid=$_SESSION['lgid'];}
  $yr=$_GET['year'];
  if (!is_numeric($yr))
  {
	  $allYrs=1;
	  $yr=$_SESSION['lgyear'];
  }
  else {$allYrs=0;}
  
  
  include($_SESSION['adminpath']."/dbopen.php");
  
  // Get Trade Counts
  $teamList="";
  $query="SELECT team1,t1abbr,team2,t2.abbr as t2abbr,cnt FROM (
	SELECT team1,t.abbr as t1abbr,team2,COUNT(*) as cnt FROM (
		SELECT year(th.date) as yr,IF(team_id_0<team_id_1,team_id_0,team_id_1) as team1,IF(team_id_0>team_id_1,team_id_0,team_id_1) as team2,th.*
		FROM `trade_history` as th
		WHERE year(th.date)=$yr
		 AND (
			team_id_0 IN (SELECT team_id FROM teams WHERE league_id=$lgid)
			OR
			team_id_1 IN (SELECT team_id FROM teams WHERE league_id=$lgid)
		 )
		ORDER BY team_id_0,team_id_1,th.date)
		as trades,teams as t
	WHERE trades.team1=t.team_id
	GROUP BY team1,team2
	) as tx,teams as t2
    WHERE tx.team2=t2.team_id
	ORDER BY team1,team2";
  if ($_GET['showq']==1) {echo "$query<br/>\n";}
  $result=mysqli_query($db,$query);
  while ($row=mysqli_fetch_array($result))
  {
	  $tid1=$row['team1'];
	  $tid2=$row['team2'];
	  $trades=$row['cnt'];

	  if (!isset($teams[$tid1])) {$teamList.=",$tid1";}
	  if (!isset($teams[$tid2])) {$teamList.=",$tid2";}
	  
	  $teams[$tid1]['trades'][$tid2]=$trades;
	  $teams[$tid2]['trades'][$tid1]=$trades;
	  
  }
  $teamList=trim($teamList, ",");
  
  // Get Team Info
  if ($allYrs==1) {$query="SELECT team_id,name,abbr,text_color_id,background_color_id FROM teams WHERE (league_id=$lgid AND allstar_team=0) OR team_id IN ($teamlist);";}
  else {$query="SELECT t.team_id,th.abbr,text_color_id,background_color_id FROM teams as t,`team_history` as th WHERE t.team_id=th.team_id AND (th.league_id=$lgid  OR t.team_id IN ($teamList)) AND th.year=$yr";}
  if ($_GET['showq']==1) {echo "$query<br/>\n";}
  $result=mysqli_query($db,$query);
  while ($row=mysqli_fetch_array($result))
  {
	  $tid=$row['team_id'];
	  $teams[$tid]['name']=$row['name'];
	  $teams[$tid]['abbr']=$row['abbr'];
	  $teams[$tid]['text_color_id']=$row['text_color_id'];
	  $teams[$tid]['background_color_id']=$row['background_color_id'];
	  
	  if (($row['text_color_id']=="#FFFFFF")||(($row['text_color_id']=="#000000") && ($row['background_color_id']!="#FFFFFF"))) {$pref_color=$row['background_color_id'];}
	  else {$pref_color=$row['text_color_id'];}
	  $teams[$tid]['preferred_color_id']=$pref_color;
  }
  
  ksort($teams);
  
  mysqli_close($db);
  
  // Create arrays
  $results=Array();
  $colorFill=Array();
  $colorStroke=Array();
  $i=0;
  $j=0;
  foreach ($teams as $tid1 => $val)
  {
  	  $results[$i]['abbr']=$teams[$tid1]['abbr'];
	  $results[$i]['text_color_id']=$teams[$tid1]['text_color_id'];
	  $results[$i]['background_color_id']=$teams[$tid1]['background_color_id'];
	  $results[$i]['preferred_color_id']=$teams[$tid1]['preferred_color_id'];
	  $colorFill[$i]=$teams[$tid1]['background_color_id'];
	  $colorStroke[$i]=$teams[$tid1]['text_color_id'];
	  $j=0;
	  foreach ($teams as $tid2 => $val2)
	  {
		  $trades=(isset($teams[$tid1]['trades'][$tid2])?$teams[$tid1]['trades'][$tid2]:0);
		  $results[$i]['trades'][$j]=0+$trades;
		  $j++;
	  }
	  $i++;
  }
  
  echo json_encode($results);
  
?>

