<?php
  include("html_functions.php");

  ##### Clear Votes #####	
  if ($_GET['clear_votes']==1)
  {
	  include($_SESSION['adminpath']."/dbopen.php");

	  $lgid=$_GET['lgid'];
       	  $year=$_GET['year'];
	  $hmid=$_GET['human_manager_id'];
 	  $awID=$_GET['award'];
	  if ($year=="") 
      	  {
		  $query="DELETE FROM awards_vote WHERE league_id=$lgid AND award_id IN (4,5,6,7,200,300);";
  	  }
	  elseif ($hmid=="")
      	  {
		  $query="DELETE FROM awards_vote WHERE league_id=$lgid AND year=$year AND award_id IN (4,5,6,7,200,300);";
      	  }
	  else
      	  {
		  $query="DELETE FROM awards_vote WHERE league_id=$lgid AND year=$year AND human_manager_id=$hmid AND award_id";
  		  if ($awID!="") {$query.="=$awID;";}
		  else {$query.=" IN (4,5,6,7,200,300);";}
      	  }
	  mysqli_query($db,$query);
	  mysqli_close($db);
	  header("Location: ./awAdmin.php");
	  exit;
  }

  $debug=$_POST['debug'];

  include($_SESSION['adminpath']."/dbopen.php");
  
  ##### Get Settings #####
  $lgid=$_SESSION['lgid'];
  $module='Awards';
  $query="SHOW TABLES LIKE 'ootp_sql_admin';";
  $result=mysqli_query($db,$query);
  if (mysqli_num_rows($result)) 
  {
	  # Get Settings
	  $query="SELECT setting,value FROM ootp_sql_admin WHERE scope IN ('ALL',$lgid) AND module='$module'";
	  if ($debug==1) {echo "$query<br/>\n";}
	  $result=mysqli_query($db,$query);
	  if (mysqli_num_rows($result))
	  {
		  while ($row=mysqli_fetch_array($result))
		  {
			  $field=$row['setting'];
		 	  $val=$row['value'];
			  $val=stripslashes($val);
		 	  switch ($field)
			  {
	  		  case 'boyVotes'   : $boyVotes=$val;    break;
			  case 'poyVotes'   : $poyVotes=$val;    break;
	  		  case 'royVotes'   : $royVotes=$val;    break;
			  case 'splitOF'    : $splitOF=$val;     break;
	  		  case 'cmVotes'    : $cmVotes=$val;     break;
			  case 'gmVotes'    : $gmVotes=$val;     break;
			  }
		  }
      	  }
  }
  if ($boyVotes=="") {$boyVotes=10;}
  if ($poyVotes=="") {$poyVotes=5;}
  if ($royVotes=="") {$royVotes=3;}
  if ($cmVotes=="")  {$cmVotes=3;}
  if ($gmVotes=="")  {$gmVotes=3;}
  if ($splitOF=="") {$splitOF=0;}

  ##### Get User Input #####
  $date=new DateTime($_SESSION['lgdate']);
  $year=$_SESSION['lgyear'];

  $ballot=$_POST['dList'];
  $hmid=$_POST['hmid'];
  $slid=$_POST['slid'];
  $award=$_POST['award'];
	
  if ($debug==1)
  {
	  echo $ballot."<br/>\n";
	  echo $hmid."<br/>\n";
	  echo $slid."<br/>\n";
	  echo $award."<br/>\n";
  }

  switch ($award)
  {
  case 'boy': $awID=5;$maxVotes=$boyVotes; break;
  case 'poy': $awID=4;$maxVotes=$poyVotes; break;
  case 'roy': $awID=6;$maxVotes=$royVotes; break;
  case 'cm' : $awID=200;$maxVotes=$cmVotes; break;
  case 'gm' : $awID=300;$maxVotes=$gmVotes; break;
  }

  ##### Create Update Statements #####
  if ($award=='gg')
  {
	  $updText="DELETE FROM awards_vote WHERE league_id=$lgid AND sub_league_id=$slid AND year=$year AND human_manager_id=$hmid AND award_id=7;\n";
  	  $pids=explode("|",$ballot);
	  $tot=count($pids);
  	  for ($i=0;$i<$tot;$i++)
	  {
  		  switch ($i+1)
		  {
		  case 1: $pos=1; break;
		  case 2: $pos=2; break;
		  case 3: $pos=3; break;
		  case 4: $pos=4; break;
		  case 5: $pos=5; break;
		  case 6: $pos=6; break;
		  case 7: if ($splitOF==1) {$pos=7;} else {$pos=20;} break;
		  case 8: if ($splitOF==1) {$pos=8;} else {$pos=20;} break;
		  case 9: if ($splitOF==1) {$pos=9;} else {$pos=20;} break;
		  }
  		  $pid=$pids[$i];
  
		  $insQuery="INSERT INTO awards_vote (player_id,league_id,sub_league_id,year,human_manager_id,award_id,position,vote) VALUES ($pid,$lgid,$slid,$year,$hmid,7,$pos,1);";
		  $updText.=$insQuery."\n";
  	  }
  
  }
  else
  {
	  $updText="DELETE FROM awards_vote WHERE league_id=$lgid AND sub_league_id=$slid AND year=$year AND human_manager_id=$hmid AND award_id=$awID;\n";
  	  $pids=explode("|",$ballot);
	  $rank=0;
  	  foreach ($pids as $key => $pid)
	  {
  		  $rank=$rank+1;
		  $insQuery="INSERT INTO awards_vote (player_id,league_id,sub_league_id,human_manager_id,year,award_id,vote) VALUES ($pid,$lgid,$slid,$hmid,$year,$awID,$rank);";
  		  $updText.=$insQuery."\n";
	  }
  }

  if ($debug==1) {echo $updText;}
  
  ##### Write Votes to File #####
  if ($updText!="")
  {
	  $updText=trim($updText,",");
       	  $updText=trim($updText);
	  $e=explode("\n",$updText);
       	  foreach ($e as $key => $query)
	  {
	    	  mysqli_query($db,$query);
		  $err=mysqli_error($db);
	    	  if ($err!="") {echo $err."<br />\n";}
	  }
  }
 
  mysqli_close($db);
  header("Location: ./eos_ballot.php");
?>
