<?php

  include("html_functions.php");



  ################## PAGE INITIALIZATION #########################

  $lgid=$_SESSION['lgid'];



  # Check Admin Table

  include($_SESSION['adminpath']."/dbopen.php");

  $query="SHOW TABLES LIKE 'ootp_sql_admin';";

  $result=mysqli_query($db,$query);

  if (mysqli_num_rows($result)) 

  {

	  # Get Settings

	  $query="SELECT * FROM ootp_sql_admin WHERE scope IN ('ALL',$lgid) AND module IN ('admin','Last Sim')";

	  if ($_GET['showq']==1) {echo "$query<br/>\n";}

	  $result=mysqli_query($db,$query);

	  if (mysqli_num_rows($result))

	  {

		  while ($row=mysqli_fetch_array($result))

		  {

			  $scope=$row['scope'];

			  $module=$row['module'];

			  $setting=$row['setting'];

			  $identifier=$row['identifier'];

			  $value=$row['value'];



			  $settings[$scope][$module][$identifier][$setting]=$value;

		  }

	  }

  }

  if ($settings['ALL']['Last Sim'][-1]['pythExp']=="") {$settings['ALL']['Last Sim'][-1]['pythExp']=1.82;}

  # Get Current Date

  $query="SELECT leagues.current_date FROM leagues LIMIT 1";

  $result=mysqli_query($db,$query);

  $row=mysqli_fetch_array($result);

  $lgdate=$row['current_date'];
    

    ## Get First Round Name
    $query="SELECT round_names0 FROM league_playoffs WHERE league_id=$lgid;";
    $result=mysqli_query($db,$query);
    $row=mysqli_fetch_array($result);
    $firstRndName=$row['round_names0'];
    if ($firstRndName=="") {$firstRndName="First";}



  mysqli_close($db);





  ################## PAGE INITIALIZATION #########################

  $title='Season Admin';



  page_head($title,$_SESSION['lgpath']);

  echo " <div id='topbar'>\n";

  main_titlebar($_SESSION['lgid'],$_SESSION['lgpath'],$title);



  if ($_SESSION['user_is_commish']!=1)

  {

	  nav_toolbar("season","Season");

	  echo " </div>   <!-- END topbar DIV -->\n";

	  echo " <div id='contentpane'>\n";

	  echo "You must be a commisioner to access this page";

	

	  ################## END OF PAGE #########################

	  echo " </div>   <!-- END contentpane DIV -->\n";

	  page_foot($_SESSION['lgpath']);

	  exit;

  }

  else

  {

	  nav_toolbar('admin','Admin Pages');

	  nav_toolbar("season","Season");

	  echo " </div>   <!-- END topbar DIV -->\n";

	  echo " <div id='contentpane'>\n";

  }



  ################## START OF PAGE CONTENT #########################

  $module="Last Sim";

  echo "   <FORM ACTION=\"save_settings.php\" METHOD=\"post\">\n";

  echo "    <input type='hidden' name='module' value='".$module."'>\n";

  if ($_GET['debug']==1) {echo "    <input type='hidden' name='debug' value='1'>\n";}

  echo "    <table cellpadding=2 cellspacing=0>";

  echo "     <TR class='title'><TD colspan=2>Last Sim Settings</TD></TR>\n";

  echo "     <TR><TD>Auto Determine Sim Length:</TD><TD align='left'><INPUT type=\"checkbox\" name=\"autoSimLength\" value=1";

  if ($settings['ALL']['Last Sim'][-1]['autoSimLength']==1) {echo " checked";}

  echo "></TD></TR>\n";

  echo "     <TR><TD>Last SQL Load (YYYY-MM-DD):</TD><TD align='right'>".$settings['ALL']['admin'][-1]['LastDate']."</TD></TR>\n";

  echo "     <TR><TD>Calculated Sim Length:</TD><TD align='right'>".$settings['ALL']['admin'][-1]['calcLength']." days</TD></TR>\n";

  echo "     <TR><TD>Sim Length Override";

  if ($settings['ALL']['Last Sim'][-1]['autoSimLength']==1) {echo " (uncheck Auto Determine Sim Length to activate)";} else {echo " (active)";}

  echo ":</TD><TD align='left'><INPUT type=\"text\" size=20 name=\"simLength\" value=\"".$settings['ALL']['Last Sim'][-1]['simLength']."\"></TD></TR>\n";

  echo "     <TR class='headline'><TD colspan=2>Playoff Odds Report</TD></TR>\n";

  echo "     <TR><TD>Enable Playoff Odds:</TD><TD align='left'><INPUT type=\"checkbox\" name=\"poEnable\" value=1";

  if ($settings[$lgid]['Last Sim'][-1]['poEnable']==1) {echo " checked";}

  echo "></TD></TR>\n";

  echo "     <TR><TD>Seasons to Simulate:</TD><TD align='left'><INPUT type=\"text\" name=\"nSeasons\" value=\"".$settings['ALL']['Last Sim'][-1]['nSeasons']."\"></TD></TR>\n";

  echo "     <TR><TD>Estimate Future Success Based On:</TD><TD>";

  $methods=array('Pythagorean Win Pct (exp)','Pythagorean Win Pct (Pythagenpat)','Actual Win Pct');

  echo "<SELECT name='pythMeth'>";

  for ($i=0;$i<count($methods);$i++)

  {

	  echo "<OPTION name='".$methods[$i]."'";

      	  if ($methods[$i]==$settings['ALL']['Last Sim'][-1]['pythMeth']) {echo " selected";}

	  echo ">".$methods[$i]."</OPTION>";

  }

  echo "</SELECT>";

  echo "</TD></TR>\n";

  echo "     <TR><TD>Pythagorean Exponent (typically 1.82):</TD><TD align='left'><INPUT type=\"text\" name=\"pythExp\" value=\"".$settings['ALL']['Last Sim'][-1]['pythExp']."\"></TD></TR>\n";

    echo "     <TR><TD>Number of Playoff Teams per League (inc div winners):</TD><TD align='left'><INPUT type=\"text\" name=\"nPOTeams\" value=\"".$settings[$lgid]['Last Sim'][-1]['nPOTeams']."\"></TD></TR>\n";
    echo "     <TR><TD>Number of Playoff Teams with a Bye in the $firstRndName".((substr($firstRndName,-5)=="Round")?"":" Round").":</TD><TD align='left'><INPUT type=\"text\" name=\"nR1Byes\" value=\"".$settings[$lgid]['Last Sim'][-1]['nR1Byes']."\"></TD></TR>\n";

  echo "     <TR class='headline'><TD colspan=2>Additional Pages</TD></TR>\n";

  echo "     <TR><TD>Milestone/Record Watch:</TD><TD align='left'><INPUT type=\"checkbox\" name=\"mrEnable\" value=1";

  if ($settings[$lgid]['Last Sim'][-1]['mrEnable']==1) {echo " checked";}

  echo "></TD></TR>\n";

  echo "     <TR><TD>Sim Preview:</TD><TD align='left'><INPUT type=\"checkbox\" name=\"spEnable\" value=1";

  if ($settings[$lgid]['Last Sim'][-1]['spEnable']==1) {echo " checked";}

  echo "></TD></TR>\n";

  echo "     <TR><TD>Playoff Summary:</TD><TD align='left'><INPUT type=\"checkbox\" name=\"pstEnable\" value=1";

  if ($settings[$lgid]['Last Sim'][-1]['pstEnable']==1) {echo " checked";}

  echo "></TD></TR>\n";

  echo "     <TR><TD>Stat Of The Day:</TD><TD align='left'><INPUT type=\"checkbox\" name=\"sotdEnable\" value=1";

  if ($settings[$lgid]['Last Sim'][-1]['sotdEnable']==1) {echo " checked";}

  echo "></TD></TR>\n";

  echo "     <TR><TD></TD><TD><INPUT type=\"submit\" value=\"Save\"></TD></TR>\n";

  echo "    </table>";

  echo "   </FORM>\n";



  ################## END OF PAGE #########################

  echo " </div>   <!-- END contentpane DIV -->\n";

  page_foot($_SESSION['lgpath']);





?>

