<?
include('auth.php');
include('common.php');

$do_close=0;
if ($_GET['lid']) 	$lid= $_GET['lid'];  else 	$lid = $_POST['lid'];
if ($_GET['sid']) 	$sid= $_GET['sid']; else	$sid = $_POST['sid'];	
if ($_GET['mode'])	$mode= $_GET['mode']; else	$mode = $_POST['mode'];	
if ($_GET['utype'])	$utype= $_GET['utype']; else	$utype = $_POST['utype'];	
if ($_GET['frompg']) $frompg= $_GET['frompg']; else	$frompg = $_POST['frompg'];	


if (count($_POST) > 0)
{
	$note = secure_user_input($_POST['note']);
	$d = $_POST['year']."-".$_POST['month']."-".$_POST['day'];
	$t = $_POST['hours'].":".$_POST['minutes'].":00";
	
	//do date comparision. date must be in the past or at least today
	$todays_date = date("Y-m-d");
	$today = strtotime($todays_date); 
	$datechosen = strtotime($d); 
	
	if ($datechosen <= $today) 
	{
	
		if ($_POST['sid']) //update
		{
			$SQL="UPDATE status 
				SET 
				user_id= '".$_POST['lid']."',
				thestatus = 0, 
				reason = ".$_POST['reason'].",
				usertype= '".$_POST['utype']."',
				note = '".$note."',
				thedate = '".$d."'
				WHERE id=".$_POST['sid'];
		}
		else //insert new record
		{
			$SQL="INSERT INTO status
			(user_id, thestatus,reason,usertype,note,thedate )
			VALUES 
			('".$_POST['lid']."', 0,'".$_POST['reason']."', '".$_POST['utype']."', '".$note."', '".$d."')";
		}

		//echo $SQL;

		$results = llnb_query($SQL);
		$do_close=1;
		
		//update the user table with their current status
		$DBtable = getTable($utype);

		$SQL="UPDATE ".$DBtable." 
						SET 
						active = 0
				WHERE id=".$_POST['lid'];
		$results = llnb_query($SQL);


		if ($_POST['frompg'] == "learnerstatus")
			$pgreturn= "learner_status.php?lid=".$_POST['lid'];
		else	if ($frompg == "voloverview")
			$pgreturn= "volunteer_overview.php?vid=".$_POST['lid'];
		else	if ($frompg == "volunteerstatus")
			$pgreturn= "volunteer_status.php?vid=".$_POST['lid'];	
		else	if ($frompg == "tutorstatus")
			$pgreturn= "tutor_status.php?tid=".$_POST['lid'];
		else	if ($frompg == "tutoroverview")
			$pgreturn= "tutor_overview.php?tid=".$_POST['lid'];	
		else	if ($frompg == "coordoverview")
			$pgreturn= "coordinator_overview.php?coordid=".$_POST['lid'];
		else	if ($frompg == "coordstatus")
			$pgreturn= "coordinator_status.php?coordid=".$_POST['lid'];		
		else
			$pgreturn= "learner_overview.php?lid=".$_POST['lid'];
	}
	else
		$mssg= "<p style=\"color: red;\">Date chosen cannot be in the future. </p>";
		
}
else 
{
	
	
	if ($_GET['mode']=="edit")
	{
		$SQL="SELECT * , 
			DAY( thedate ) AS theday, MONTH( thedate ) AS themonth, YEAR( thedate ) AS theyear
			FROM status 
			WHERE id=".$_GET['sid'];
			//echo $SQL;
			
		$results = llnb_query($SQL);
		$row = llnb_fetch_assoc($results);
	}
	
	

	
}
?><head>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/llnb-style.css">
</head>




<style type="text/css">
body { background-color: #fff; background-image:none; padding: 20px;}

div { font-family: sans-serif; color: #222; }

.btn	{ -moz-border-radius: 7px; -webkit-border-radius: 7px; -khtml-border-radius: 7px; border-radius: 7px; border: 1px #222 solid; background-image: url(img/btn-bg-nor.jpg) ; line-height: 25px; padding: 3px 10px 0; text-decoration: none; color: #222; }
.btn:hover	{ border-color: #33C; color: #33c;}

.btnhidden {border-color: #CCC; background-image: none; font-size: .8em;}

.btncnt	{ width: 160px; text-align:center; display: block; margin-bottom: 10px;}

.btnlrg	{width: 300px; padding-left: 20px;}
.btnsml	{ padding: 1px 10px; font-weight: bold;}
.btnpg	{ padding: 2px 4px 1px; font-size: .75em;margin-right: 4px;}

.lfr	{ float: right; margin-right: 10px;}
.lfl	{ float: left; margin-right: 10px;}
form label { display:block; margin-top: 10px; font-size: .8em; color: #555; }

</style>

<body >

<? 
if ($do_close) 
	echo "<script>parent.location.href='".$pgreturn."';</script>"; 

?>


<div style="width: 300px;">
<h2>Make Inactive<br />
<?
if ($utype == 1) 
{	
	$personname = fetchExecDirName($lid);
		$who = "Learner";

}
else if ($utype == 2) 
{
	$personname = fetchTutorName($lid);
		$who = "Tutor";

}
else if ($utype == 3) 
{
	$personname = fetchVolName($lid);
	$who = "Volunteer";

}
else if ($utype == 4) 
{
	$who = "Coordinator";
	$personname = fetchCoordName($lid);

}
?>
<?= $personname; ?></h2>

<form name="makeinactive" action="make_inactive_m.php" method="post">
	 	<?=$mssg;?>
        <div class="input">

        <label style="display:block;">Date <?=$who;?> becomes inacitve:</label>
 	
       
		<select name="month" >
				<?
				$mymon = ($_POST['month']) ? stripslashes($_POST['month']) : $row['themonth'];
				echo buildMonthDropdown($mymon, $_MONTHS_abbr); 
				?>
		</select>
		<select name="day" >
				<?
				$myday = ($_POST['day']) ? stripslashes($_POST['day']) : $row['theday'];
				echo buildDayDropdown($myday); 
				?>
		</select>

		<select name="year">
				<?
				$myyear = ($_POST['year']) ? stripslashes($_POST['year']) : $row['theyear'];
				echo buildYearDropdown($myyear, "2012"); 
				?>
		</select>
        </div>

		<div style="clear:both;"></div>

        <div class="input">
            <?
            	if ( $utype ==2 )
            	    $inactive_mssg = "<label>Information on tutor leaving the program.</label>";
            	else if ($utype==1)
                  $inactive_mssg = "<label>Learner attainment at the end of the tutoring period:</label>";
            	else if ($utype==3)
                  $inactive_mssg = "<label>Information on volunteer leaving:</label>";                  
             	else if ($utype==4)
                  $inactive_mssg = "<label>Information on coordinator leaving:</label>";              
            ?>
        
            <?=$inactive_mssg;?>
            <textarea name="note" cols="" rows="3"  class="talrg" style="margin-top:10px;"><?=$row['note'];?></textarea>
        </div>
        <div style="clear:both;"></div>

        
        <div class="input" style="width: 520px;">
            <label>Reason for leaving (select if applicable):</label>
     		<?
     		echo buildReasonForLeaving($row['reason'], $utype);
     		?>       
            <div style="clear:both;"></div>

	    </div>
        
        <div style="clear:both;"></div>


<p class="clearfix">
<input type="submit"  class="btnlfr_m" value="Save &amp; Close">
<a href="#" onclick="parent.SqueezeBox.close();"class="btn lfr">Cancel</a>
</p>

<input type="hidden" name="sid" value="<?=$sid;?>">
<input type="hidden" name="lid" value="<?=$lid;?>">
<input type="hidden" name="mode" value="<?=$mode;?>">
<input type="hidden" name="utype" value="<?=$utype;?>">
<input type="hidden" name="frompg" value="<?=$frompg;?>">


</form>
</div>