<?php

  include("html_functions.php");



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

  $lgid=$_SESSION['lgid'];

  $lgpath=$_SESSION['lgpath'];



  # Check Admin Table

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

  

  # Get Leagues

  $query="SELECT league_id,name,abbr,parent_league_id,league_level,rules_fa_minimum_years,rules_salary_arbitration_minimum_years,rules_min_service_days FROM leagues WHERE league_id=$lgid ORDER BY league_id";

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

  $lgCnt=0;

  if (mysqli_num_rows($result))

  {

	  while ($row=mysqli_fetch_array($result))

	  {

		  $league_id=$row['league_id'];

		  $leagues[$league_id]['name']=$row['name'];

		  $leagues[$league_id]['abbr']=$row['abbr'];

		  $leagues[$league_id]['parent']=$row['parent_league_id'];

		  $leagues[$league_id]['level']=$row['league_level'];

		  $leagues[$league_id]['rules_min_service_days']=$row['rules_min_service_days'];

		  $leagues[$league_id]['rules_salary_arbitration_minimum_years']=$row['rules_salary_arbitration_minimum_years'];

		  $leagues[$league_id]['rules_fa_minimum_years']=$row['rules_fa_minimum_years'];

		  $leagues[$lgCnt]=$league_id;

		  $lgCnt++;

	  }

	  $lgTotCnt=count($leagues)/2;

      $leagues[9999]['name']="Unaffiliated";

  }



  # Get Teams

  $query="SELECT team_id,name,nickname,abbr FROM teams WHERE 1 ORDER BY team_id";

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

  if (mysqli_num_rows($result))

  {

	  while ($row=mysqli_fetch_array($result))

	  {

		  $tid=$row['team_id'];

		  $teams[$tid]['name']=$row['name']." ".$row['nickname'];

		  $teams[$tid]['abbr']=$row['abbr'];

	  }

  }



  mysqli_close($db);



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

  $title='StatsLab Users';



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

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

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



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

  {

	  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');

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

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

  }



  if ($lgTotCnt==0)

  {

	  echo "<p>No leagues defined.  Verify leagues table exists.</p>\n";

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

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

	  page_foot($_SESSION['lgpath']);

	  exit;

  }

  else

  {

	  if ($lgTotCnt>$maxCols) {$lgCnt=$maxCols;}

	  else {$lgCnt=$lgTotCnt;}

  }



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

  

  ##### Check for settings changes #####

  $editableSettings=Array('slUpdateSubscribe','draftSubscribe');

  if (isset($_GET['human_manager_id']))

  {

		if (isset($_GET['setting'])&&(in_array($_GET['setting'],$editableSettings)))

		{

			$query="INSERT INTO ootp_sql_admin (scope, module, setting, identifier, value) VALUES (";

			$query.="\"ALL\", \"users\", \"".$_GET['setting']."\", \"".$_GET['human_manager_id']."\", \"".$_GET['value']."\"";

			$query.=") ON DUPLICATE KEY UPDATE value=\"".$_GET['value']."\";";

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

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

		}

	  

  }



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

  echo " <div class='textbox'>\n";

    $query="SELECT hm.*,t.abbr,t.name,t.nickname FROM teams as t RIGHT JOIN (SELECT human_manager_id,is_commish,first_name,last_name,team_id,if(league_id=0,9999,league_id) as league_id FROM human_managers";

    //if ($settings['ALL']['admin'][-1]['showAllUsers']!=1) {$query.=" WHERE is_commish=1 OR team_id>0 OR organization_id>0";}

    $query.=") as hm ON t.team_id=hm.team_id ORDER BY league_id,is_commish DESC,last_name,first_name";

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

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

    if (mysqli_num_rows($result)>0)

    {

        while ($row=mysqli_fetch_array($result))

        {

            if ($row['team_id']>0) {$tname=$row['abbr'];}

            elseif ($row['is_commish']==1) {$tname="CMSH";}

            else {$tname="N/A";}

            

            $hmid=$row['human_manager_id'];

            $users[$hmid]['name']=$row['first_name']." ".$row['last_name'];

            $users[$hmid]['sortname']=$row['last_name'].", ".$row['first_name'];

            $users[$hmid]['tid']=$row['team_id'];

            $users[$hmid]['tabbr']=$row['abbr'];

            $users[$hmid]['tname']=$tname;

            $users[$hmid]['is_commish']=$row['is_commish'];

            $users[$hmid]['lgid']=$row['league_id'];

        }

        

        // Get user settings

        $query="SELECT * FROM ootp_sql_admin WHERE module='users' AND setting IN ('email','commish','slUpdateSubscribe','draftSubscribe','lastLogin') ORDER BY identifier,setting";

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

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

        if (mysqli_num_rows($result)>0)

        {

            while ($row=mysqli_fetch_array($result))

            {

                $hmid=$row['identifier'];

                $setting=$row['setting'];

                $users[$hmid][$setting]=$row['value'];

            }

        }

		$query="SELECT identifier,value FROM ootp_sql_admin WHERE setting='Last Login';";

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

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

		if (mysqli_num_rows($result)>0)

		{

			while ($row=mysqli_fetch_array($result))

			{

				$hmid=$row['identifier'];

				$users[$hmid]['logintime']=$row['value'];

			}

		}        

		

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

        echo "      <TR class='title'><TD style='padding:2px;'>StatsLab Users Accounts</TD></TR>\n";

        echo "      <tr><td>\n";

        echo "       <table cellpadding=2 cellspacing=0 border=0 class='sortable' style='width:935px;'>";

        echo "        <thead>\n";

        echo "        <tr class='headline'>";

        echo "<td class='hsc2'>ID#</td>";

        echo "<td class='hsc2_l'>Human Manager</td>";

        echo "<td class='hsc2_l'>Team</td>";

        echo "<td class='hsc2_l'>Email</td>";

        echo "<td class='hsc2'>Is Commish</td>";

        echo "<td class='hsc2' title='StatsLab Updates'>SL Updates</td>";

        echo "<td class='hsc2' title='Draft Updates'>Draft</td>";

        echo "<td class='hsc2'>Last Login</td>";

        echo "</tr>\n";

        echo "        <thead>\n";

        echo "        <tbody>\n";

        

        $rowCnt=0;

        $lastLgID=-1;

        foreach ($users as $hmid => $val)

        {

            $lgid=$users[$hmid]['lgid'];

            if ($lgid!=$lastLgID)

            {

                echo "     <tr class='headline'><td colspan=8 style='font-weight:bold;font-style:italic;'>".$leagues[$lgid]['name']."</td></tr>\n";

                

            }

            

            $cls='s'.($rowCnt%2+1);

            echo "        <tr class='$cls'>";

            echo "<td>$hmid</td>";

            echo "<td class=\"".$cls."_l\" sorttable_customkey=\"".$users[$hmid]['sortname']."\"><a href=\"./manager.php?human_manager_id=$hmid\">".$users[$hmid]['name']."</a></td>";

            echo "<td class=\"".$cls."_l\">".$users[$hmid]['tabbr']."</td>";

            echo "<td class=\"".$cls."_l\"><a href=\"mailto:".$users[$hmid]['email']."\">".$users[$hmid]['email']."</a></td>";

            echo "<td sorttable_customkey=\"".max($users[$hmid]['is_commish'],$users[$hmid]['commish'])."\">".(($users[$hmid]['is_commish']==1)||($users[$hmid]['commish']==1)?"Y":"N")."</td>";

            echo "<td sorttable_customkey=\"".$users[$hmid]['slUpdateSubscribe']."\"><a href=\"./userAdmin.php?human_manager_id=$hmid&setting=slUpdateSubscribe&value=".($users[$hmid]['slUpdateSubscribe']==1?0:1)."\" title='Toggle On/Off'>".($users[$hmid]['slUpdateSubscribe']==1?"Y":"N")."</a></td>";

            echo "<td sorttable_customkey=\"".$users[$hmid]['draftSubscribe']."\"><a href=\"./userAdmin.php?human_manager_id=$hmid&setting=draftSubscribe&value=".($users[$hmid]['draftSubscribe']==1?0:1)."\" title='Toggle On/Off'>".($users[$hmid]['draftSubscribe']==1?"Y":"N")."</a></td>";

			# Last Login

			echo "<td";

			if (isset($users[$hmid]['logintime'])) 

			{

				$loginTime=$users[$hmid]['logintime'];

				echo " sorttable_customkey='$loginTime'>".date("D M j, Y H:i",$loginTime);

			}

			else {echo " sorttable_customkey='0'>Never";}

			echo "</td>";

		  

            echo "</tr>\n";

            $rowCnt++;



            $lastLgID=$lgid;

        }



        echo "        </tbody>\n";

        echo "       </table>\n";

        echo "     </td></tr>\n";

        echo "    </table>\n";

    }



    

  else

  {

	  echo "<p>No human managers found.</p>\n";

  }

  echo " </div>\n";

  

  mysqli_close($db);



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

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

  page_foot($_SESSION['lgpath']);





?>

