<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
//
// Copyright (c) 2022 RealTime Fantasy Sports, Inc.
//
require_once("autoload.php");
require_once("../conf/basketball.cfg");
require_once("basketball-functions.php");

list($rtuser, $theme, $league) = BasketballPresentation::Initialize(true);

$theme->ResponsiveHeader($rtuser);

?>
<style>
.Indent { padding-left: 10px; font-weight: bold; }
.Conference { text-decoration: underline; }
.EditButton {margin-top: -4px;}
</style>
<?

// prevent php warnings later if league has no archives
$years = array();
$year = 0;

$result = $globals->db_basketball->Query("select year as Years from League_Archives where league_id = " . $league->league_id . " union all " .
                                       "select year as Years from League_Archive_Files where league_id = " . $league->league_id . " order by years desc");
while ($row = $globals->db_basketball->FetchRowObject($result)) {
   $years[$row->Years] = $row->Years;
}
$globals->db_basketball->Free($result);

echo '<div class="row">';
if (count($years)) {
	foreach($years as $year) {
	   echo '<div class="col-md-4 col-tn-12 season">';

	   echo "<h4>" . $year . " Season Archive";
	   if (false && $league->is_commissioner) {
		   echo '<span class="pull-right">';
		   echo '<a data-original-title="Edit" class="btn btn-default btn-xs EditButton" href="' . BasketballPresentation::URL('archive-edit.php') . '&YEAR=' . $year . '"><span class="glyphicon glyphicon-edit"></span></a>';
		   echo '</span>';
	   }
	   echo "</h4>";
	   echo '<table class="table table-tight table-no-borders"><tbody>';

	   $champs = 0;
	   $conf_list = array();
		$custom_files = "";

	   $result = $globals->db_basketball->Query("select * from League_Archives where league_id = ". $league->league_id . " and year = " . (int)$year . " order by conference_id asc");
	   while ($arch = $globals->db_basketball->FetchRowObject($result)) {
		  if (!array_key_exists($arch->Conference_Id, $conf_list)) {
			 $conf_list[$arch->Conference_Id] = new stdClass();
			 $conf_list[$arch->Conference_Id]->name = "";
			 $conf_list[$arch->Conference_Id]->files = "";
		  }

//		  if (!$champs++ && strlen(trim($arch->Champion)))
//				echo '<tr><td>League Champion:<br><span class=Indent>' . trim($arch->Champion) . '</span></td></tr>';
		 
	   } 
	   $globals->db_basketball->Free($result);

//	   if ($champs == 0) echo "<tr><td>League Champion: ---</td></tr>";

	   $result = $globals->db_basketball->Query("select * from League_Archive_Awards where league_id = " . $league->league_id . " and year = " . $year);
	   while ($arch = $globals->db_basketball->FetchRowObject($result)) {
		  if (strlen(trim($arch->Award))) {
			 echo "<tr><td>" . $arch->Award . ":<br><span class=Indent>" . $arch->Winner . "</span></td></tr>";
		  }
	   }
	   $globals->db_basketball->Free($result);

	   $result = $globals->db_basketball->Query("select * from League_Archive_Files where league_id = " . $league->league_id . " and year = " . $year . " order by archive_type asc, conference_id asc");
	   while ($row = $globals->db_basketball->FetchRowObject($result)) {
		  if (!array_key_exists($row->Conference_Id, $conf_list)) {
			 $conf_list[$row->Conference_Id] = new stdClass();
			 $conf_list[$row->Conference_Id]->name = "";
			 $conf_list[$row->Conference_Id]->files = "";
		  }

		  if ( $row->Archive_Type < 100 ) {
			 switch ($row->Archive_Type) {
					case  0: $label = "Final Rosters";  break;
					case  1: $label = "Game Results";   break;
					case  2: $label = "Standings";      break;
					case  3: $label = "Standings";      break;
					case  4: $label = "Fees";           break;
					case  5: $label = "Draft";          break;
					case  6: $label = "Weekly Results"; break;
					case  7: $label = "Transactions";   break;
					case  8: $label = "Weekly Rosters"; break;
					case  9: $label = "Other Records";  break;
					case 10: $label = "Other Records";  break;
					case 11: $label = "Other Records";  break;
					case 12: $label = "Record Book";    break;
					case 13: $label = "Top Players";    break;
					case 14: $label = "Honors";		   break;
					case 15: $label = "NFL Picks Standings";		  break;
					case 16: $label = "Message Board";  break;
					case 17: $label = "Auction";        break;
					case 18: $label = "Survivor Pool";        break;
				}
            if (4 == $row->Archive_Type && $league->is_guest) continue;
				if (16 == $row->Archive_Type)
					$conf_list[$row->Conference_Id]->files .= '<li><a href=" ' . BasketballPresentation::URL('display-archived-message-board.php') .
						"&YEAR=" . $row->Year . "&FILE=" . $row->Archive_File . "\">" . $label . "</a></li>";
				else
					$conf_list[$row->Conference_Id]->files .= "<li><a target=_blank href=/basketball-" . $row->Year . "/" . $row->Archive_File . ">" . $label . "</a></li>";
			} else {
				$custom_files .= "<li><a target=_blank href=/basketball-" . $row->Year . "/custom/" . $row->Archive_File . ">" . $row->Custom_Name . "</a></li>";
			}
		}

	   $prev = -1;
	   foreach ($conf_list as $conf_id => $conf) {
         if (!strlen($conf->files)) continue;
         if ( $prev != $conf_id && strlen($conf->name) > 0 ) echo '<tr><td><u>' . trim($conf->name) . ":</u></td></tr>";
         echo "<tr><td><ul>" . $conf->files . "</ul></td></tr>";
         $prev = $conf_id;
	   }
		  
		if (strlen($custom_files)) echo "<tr><td>Custom Files:</td></tr><tr><td><ul>" . $custom_files . "</ul></td></tr>";

		echo '</tbody></table></div>';
	}
}
echo '</div>'; // end row

if (false && $league->is_commissioner) {   
   echo '<br><div class="row"><div class="col-tn-12 text-center">To add custom league archives for previous years, click ';
   echo '<a href="' . BasketballPresentation::URL('archive-edit.php') . "&YEAR=" . ($year ? $year - 1 : NBA::Season - 1) . '">here</a>.';
   echo "</div></div>";
}

?>
<script>
$(document).ready(function() {
   MatchHeight('.season');
});
</script>
<?

$theme->ResponsiveFooter();
?>