<?php

  include("html_functions.php");



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

  $lgid=$_SESSION['lgid'];

  $lgdate=$_SESSION['lgdate'];

  $date=new Date($lgdate);

  $lgyear=$date->format("Y");

  $hmid=$_SESSION['user_id'];

  

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

  $title='Draft Pool Generation';



  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("draft","Draft");

	  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("draft","Draft");

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

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

  }



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

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

  

  ##### Get League Draft Date

  $query="SELECT if(draft_date<'$lgdate',CONCAT('".($lgyear+1)."','-',MONTH(draft_date),'-',DAY(draft_date)),draft_date) as draft_date FROM leagues WHERE league_id=$lgid;";

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

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

  $row=mysqli_fetch_array($result);

  $draftDate=new Date($row['draft_date']);

  $draftYear=$draftDate->format("Y");

  $draftDay=$draftDate->format("M-d");



  ##### Get Admin Settings

  $query="SELECT identifier,value FROM ootp_sql_admin WHERE scope=$lgid AND module='Draft' AND setting='feeder'";

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

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

  if (mysqli_num_rows($result))

  {

	  $feederText.=" - FEEDER LEAGUE SETTINGS<br/>";

	  while ($row=mysqli_fetch_array($result))

	  {

		  $identifier=$row['identifier'];

		  $value=$row['value'];



		  $feeders[$identifier]=$value;

		  $feederText.=" -> League ID: $identifier graduates at $value<br/> ";



	  }

  }

  ## Generate Feeder Filter

  if (isset($feeders))

  {

	  $feederFilter="";

	  foreach ($feeders as $flgid=>$fage)

	  {

		  if (($flgid=="")||($fage=="")) {continue;}

		  $feederFilter.=" OR (p.league_id=$flgid AND p.draft_league_id=$lgid AND DATEDIFF(CONCAT(YEAR(date_of_birth)+$fage,'-',MONTH(date_of_birth),'-',DAY(date_of_birth)),'".$draftDate->format("Y-m-d")."')<=0)";

	  }

  }



  ##### Find Draft Eligible Players

  $query="SELECT player_id FROM players as p WHERE retired=0 AND hidden=0 AND draft_year=0 AND (team_id=0 AND draft_eligible=1 AND draft_league_id=$lgid)$feederFilter;";

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

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

  $query="";

  if (mysqli_num_rows($result))

  {

	  while ($row=mysqli_fetch_array($result))

	  {

		  $pid=$row['player_id'];



		  $query.="INSERT INTO ootp_sql_draft_pool (league_id,player_id)";

		  $query.=" VALUES ($lgid,$pid);";

		

		  $cnt++;

	  }

  }



  if ($query!="")

  {

	  $clearQuery="DELETE FROM ootp_sql_draft_pool WHERE league_id=$lgid";

	  mysqli_query($db,$clearQuery);



	  $q=explode(";",$query);

	  foreach ($q as $key => $impQuery)

	  {

		  mysqli_query($db,$impQuery);

	  }



	  echo "Imported ".($cnt-1)." players to your draft pool<br/> - NOTE: Player eligibility subject to accurate settings on draft admin page<br/>\n";

	  if (isset($feeders)) {echo $feederText;}

  }

  else

  {

	  echo "No draft eligible players found.<br/>\n";

  }



  echo "Return to the <a href='./draftAdmin.php'>draft admin</a> page<br/>\n";



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

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

  page_foot($_SESSION['lgpath']);



?>

