<?   
include('auth.php');
include('common.php');
$update_success=false;
$insert_success=false;

$lid = $_GET['lid'];

$mode="";
$SQL="SELECT * FROM learnermatch WHERE learner_id=".$_GET['lid'];
$results = llnb_query($SQL);
$row = llnb_fetch_assoc($results);
if ($row)
	$mode="edit";
else
	$mode="new";


if (count($_POST) > 0) //form has been submitted by user
{

	
	$smoke= secure_user_input($_POST['smoke']);
	$tsmoke= secure_user_input($_POST['tsmoke']);
	$gender= secure_user_input($_POST['gender']);
	$age= secure_user_input($_POST['age']);
	$timeofday= serialize($_POST['timeofday']);
	$timesperweek= secure_user_input($_POST['timesperweek']);
	$montime= secure_user_input($_POST['montime']);
	$tuetime= secure_user_input($_POST['tuetime']);
	$wedtime= secure_user_input($_POST['wedtime']);
	$thutime= secure_user_input($_POST['thutime']);
	$fritime= secure_user_input($_POST['fritime']);
	$sattime= secure_user_input($_POST['sattime']);
	$suntime= secure_user_input($_POST['suntime']);
	$meet= secure_user_input($_POST['meet']);
	$meetother= secure_user_input($_POST['meetother']);
	$transport= secure_user_input($_POST['transport']);
	
	$lid = $_POST['lid'];
	
	if ($mode == 'new')
	{
		$SQL = "INSERT INTO learnermatch
		(
		learner_id,
		smoke 	,
		tsmoke 	,
		gender 	,
		age 	,
		timeofday ,
		timesperweek,
		montime ,
		tuetime ,
		wedtime ,
		thutime ,
		fritime ,
		sattime ,
		suntime ,
		meet 	,
		meetother ,
		transport			
		)			
		 VALUES 
		(
		'".$lid."',
		'".$smoke."' 	,
		'".$tsmoke."' 	,
		'".$gender."' 	,
		'".$age."' 	,
		'".$timeofday."' ,
		'".$timesperweek."',
		'".$montime."' ,
		'".$tuetime."' ,
		'".$wedtime."' ,
		'".$thutime."' ,
		'".$fritime."' ,
		'".$sattime."' ,
		'".$suntime."' ,
		'".$meet."'	,
		'".$meetother."' ,
		'".$transport."'			
		)";						

		if (!llnb_query($SQL))
			echo "ERROR on INSERT SQL";
		//echo $SQL;
		
		$insert_success=true;
		header("Location: ./learner_overview.php?lid=".$lid);

	}
	else if ($mode="edit" )
	{
		$SQL = "UPDATE learnermatch SET
		learner_id=     '".$lid."',
		smoke 	=	 '".$smoke."' 	,
		tsmoke 	=	 '".$tsmoke."' 	,
		gender 	=	 '".$gender."' 	,
		age 	=		 '".$age."' 	,
		timeofday =	 '".$timeofday."' ,
		timesperweek=	 '".$timesperweek."',
		montime =		 '".$montime."' ,
		tuetime =		 '".$tuetime."' ,
		wedtime =		 '".$wedtime."' ,
		thutime =		 '".$thutime."' ,
		fritime =		 '".$fritime."' ,
		sattime =		 '".$sattime."' ,
		suntime =		 '".$suntime."' ,
		meet 	=	 '".$meet."'	,
		meetother =	 '".$meetother."' ,
		transport	=	'".$transport."'			
		WHERE learner_id=".$_POST['lid'];

		//echo $SQL;
		$results = llnb_query($SQL);
		$update_success = true;
		header("Location: ./learner_overview.php?lid=".$lid);

	}
	
	
}
else if (isset($_GET['lid']) && $mode !="new")
{
	$SQL="SELECT * FROM learnermatch WHERE learner_id=".$_GET['lid'];
	$results = llnb_query($SQL);
	$row = llnb_fetch_assoc($results);

	$smoke= secure_user_input($row['smoke']);  if ($row['smoke'] == 1)	$smokeyes="checked"; else $smokeno="checked";
	$tsmoke= secure_user_input($row['tsmoke']); if ($row['tsmoke'] == 1)	$tsmokeyes="checked"; else $tsmokeno="checked";
	$gender= secure_user_input($row['gender']); if ($row['gender'] == 1)	$genderm="checked"; else if ($row['gender'] == 2) $genderd="checked"; else $genderf="checked";
	$meet= secure_user_input($row['meet']); if ($row['meet'] == 1)	$meetL="checked"; else if ($row['meet'] == 2)	$meetOpen="checked"; else $meetOther="checked";  
	$transport= secure_user_input($row['transport']); if ($row['transport'] == 1)	$transportyes="checked"; else $transportno="checked";

	$timeofday= unserialize($row['timeofday']);

}

//parse serialized checkboxes
if (!$timeofday)
	$timeofday = array();
	
$thetimesofday = array('1', '2', '3', '4');
$thetimesofdaynames = array('Morning', 'Afternoon', 'Evening', 'Doesn\'t Matter');
for ($i = 0; $i < count($thetimesofday); $i++)
{
	$checked = in_array($thetimesofday[$i], $timeofday)? " checked" : "";
	$tod.= "<label class=\"check\"><input type=\"checkbox\" name=\"timeofday[]\" value=\"$thetimesofday[$i]\"$checked> $thetimesofdaynames[$i] </label> \n";
}
?>



<!doctype html>
<html class="no-js" lang="en"> <!--<![endif]-->

<head>
<meta charset="utf-8">

<!-- Use the .htaccess and remove these lines to avoid edge case issues.
       More info: h5bp.com/i/378 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>LLNB System</title>
<meta name="description" content="">

<!-- Mobile viewport optimized: h5bp.com/viewport -->
<meta name="viewport" content="width=device-width, maximum-scale=1.0">

<!-- Place favicon.ico and apple-touch-icon.png in the root directory: mathiasbynens.be/notes/touch-icons -->
  <LINK REL="SHORTCUT ICON" HREF="favicon.ico">

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

<!-- More ideas for your <head> here: h5bp.com/d/head-Tips -->

<script src="js/libs/modernizr-2.5.3.min.js"></script>
</head>
<body>
<div id="wrap"> 
  <!-- Prompt IE 6 users to install Chrome Frame. Remove this if you support IE 6.
       chromium.org/developers/how-tos/chrome-frame-getting-started --> 
  <!--[if lt IE 7]><p class=chromeframe>Your browser is <em>ancient!</em> <a href="http://browsehappy.com/">Upgrade to a different browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to experience this site.</p><![endif]-->
  <header><a href="index.php" class="btn lfr" style="position:relative; top: 15px; right: 0;">Logout</a>
    <h1>Laubach Literacy - <?=uType();?></h1>
    <nav id="menu" class="clearfix navmenu"><a href="home.php" class="btn lfl">Home</a>
    <a href="learner_list.php" class="btn lfl">Learner List</a>
    <a href="learner_overview.php?lid=<?=$lid;?>" class="btn lfl">Learner Overview</a></nav>
  </header>
  <div role="main" id="main" class="clearfix">
    <div id="location">
            <H2><span>Learner:</span> <?= fetchLearnerName($lid); ?></H2>
    </div>
    <!-- end of location -->
    <div>
    <H2> Match Information</H2>
    
    	 <form class="midform" name="learnertutormatch" action="learner_tutormatchinfo.php?lid=<?= $_GET['lid'];?>" method="POST">

    <div class="input">
		<label>Do you smoke?</label>
			<label class="radio" style="margin-right:15px;">
			<input name="smoke" type="radio" value="1"   <?=$smokeyes;?>>Yes</label>
			<label class="radio">
			<input name="smoke" type="radio" value="0" <?=$smokeno;?>>No</label>
    </div>

    <div class="input plusleft">
		<label>Would it bother you if your tutor smoked?</label>
			<label class="radio" style="margin-right:15px;">
			<input name="tsmoke" type="radio" value="1"   <?=$tsmokeyes;?>>Yes</label>
			<label class="radio">
			<input name="tsmoke" type="radio" value="0"  <?=$tsmokeno;?>>No</label>
    </div>
    
    <div style="clear:both;"></div>
	<div class="input">
		<label>For the purpose of matching you with a tutor please indicate what type of person you would be most comfortable working with. </label>
        <label class="radio"><input name="gender" type="radio" value="0" <?=$genderf;?>>Female</label> 
        <label class="radio"><input name="gender" type="radio" value="1" <?=$genderm;?>>Male</label>
        <label class="radio"><input name="gender" type="radio" value="2"  <?=$genderd;?>>Doesn&rsquo;t Matter</label>
        
        <label style="display:inline-block; margin-right: 2px;" class="plusleft">Age Range:</label>
        <input name="age" type="text" value="<?=$row['age'];?>" class="sminp" style="display:inline-block;">
        <label style="display:inline-block;">eg: 25-40</label>
     
     </div>
     <div style="clear:both;"></div>
   



    <div class="input" >
		<label>What time of day is best for you to meet with your tutor?</label>
		<?= $tod;?>
	</div>
    <div style="clear:both;"></div>


    <label style="display:block;">How many times per week would you like to meet your tutor?</label>
    <select name="timesperweek" class="" title="Client of" >
	<?=	buildTimesPerWeekDropdown($row['timesperweek']);  ?>
    </select>
    <div style="clear:both;"></div>


    <div class="input" >
	<label>If you know your schedule now, please indicate your preferred days and hours for tutoring: <br>
	  eg. (2:00 pm - 4:00pm)</label>
    	<div class="input">
                <label><input name="montime" value="<?=$row['montime'];?>" type="text" class="sminp">Monday</label>
                <label><input name="wedtime" value="<?=$row['wedtime'];?>" type="text" class="sminp">Wednesday</label>
                <label><input name="fritime" value="<?=$row['fritime'];?>" type="text" class="sminp">Friday</label>
                <label><input name="suntime" value="<?=$row['suntime'];?>" type="text"  class="sminp">Sunday</label>
        </div>
    	<div class="input plusleft">
                <label><input name="tuetime" value="<?=$row['tuetime'];?>" type="text" class="sminp">Tuesday</label>
                <label><input name="thutime" value="<?=$row['thutime'];?>" type="text" class="sminp">Thursday</label>
                <label><input name="sattime" value="<?=$row['sattime'];?>" type="text" class="sminp">Saturday</label>
        </div>
    	<div class="input plusleft">
        </div>
     </div>
    <div style="clear:both;"></div>


<div class="input" >
	<div class="input" style="margin-right:15px;">
		<label>Where would you like to meet?</label>
		<label class="check">
		<input name="meet" type="radio" value="1"  <?=$meetL;?>>Public Library</label> 
	      <label class="check">
	      <input name="meet" type="radio" value="2"  <?=$meetOpen;?>>Open to Suggestions</label> 
            <label class="check">
            <input name="meet" type="radio" value="3"  <?=$meetOther;?>>Other</label></div>
      <div class="input">
        <label>Other:</label>
        <input name="meetother" type="text" value="<?=$row['meetother'];?>"  class="lrginp">
      </div>
</div>

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


	<div class="input">
		<label>Do you have  reliable Transportation?</label>
		<label class="radio">
		<input name="transport" type="radio" value="1"  <?=$transportyes;?>>Yes</label> 
        <label class="radio">
        <input name="transport" type="radio" value="0" <?=$transportno;?>>No</label>
        </div>
	<div style="clear:both;"></div>
   
   
            
      <p class="clearfix">
            <input type="submit" class="btn lfr" value="Save &amp; Close"> 
	       <a href="learner_overview.php?lid=<?=$lid?>" class="btn lfr">Cancel</a></p>
  		 <input type="hidden" name="lid" value="<?=$lid;?>">
      
      </p>
    </form>
  </div>
</div>
<!-- end of main -->
<aside>
</aside>
    <div style="clear:both;"></div>
<footer>
  <?php include('footer.php'); ?>
</footer>
</div>
<!-- end of wrap --> 



</body>
</html>