<?php
/**
 * File Name : register.php
 * Author : Everplex
 * Created On : 16th May 2013 2:56 PM 
 * Description : registration page 
 */
require 'login_header.php';

// Create an instance of the Group class with the $pdo object
$obj_group = new Group($pdo);

// Fetch groups
$groups = $obj_group->fetchVisibleGroups();

// Function to sort groups alphabetically by group_name
usort($groups, function($a, $b) {
    return strcmp($a['group_name'], $b['group_name']);
});
?>
<div class="login register">
<center><h1>New User Registration</h1></center>
            <font class="red error">
                <?php if(isset($_SESSION['errors'])){
                    foreach($_SESSION['errors'] as $session_key => $session_value)
                    {
                       echo $session_value."<br/>";
                    }
                   
                } unset($_SESSION['errors']);?>
            </font>
    <form name="register" action="actions/user/user_a.php" method="post">
        <input type="hidden" name="action" value="register">
        <?php /* <h2>Step 1</h2> */ ?>
        <div class="box">
            <table>
                <tr>
                    <?php /* <td>
                        <b>Group</b>  
                    </td> */ ?>
                    <td>    
					<select name="group_id">
						<option value="">Select Group</option>
						<?php if(!empty($groups)){
							foreach($groups as $groups_val){
								if ($groups_val['group_name'] !== 'Super Administrator') { ?>
									<option value="<?php echo $groups_val['id'];?>" 
											data-group-name="<?php echo htmlspecialchars($groups_val['group_name']);?>">
										<?php echo htmlspecialchars($groups_val['group_name']);?>
									</option>
						<?php   }
							}
						}?>
					</select>
					<input type="hidden" name="group_name" id="group_name">
                   </td>
                </tr>
                <tr>
                    <?php /* <td>
                        <span class="light-text">Enter your Group Password</span>
                    </td> */ ?>
                    <td>
                        <input type="password" name="pass" id="pass" value="" autocomplete="off" placeholder="Enter your Group Password">
                    </td>
                </tr>
            </table>
        </div>
        <?php /* <h2>Step 2</h2> */ ?>
        <div class="box">
            <div id="enter-text" class="light-text">Enter your email to use for logging into the app.</div>
            <table id="email-table"><tr>
            <?php /* <td>
                <b>Email Address</b>
            </td> */ ?>
            <td>
                <input type="text" name="email" id="email" value="<?php echo (isset($_SESSION['session']['email']))? $_SESSION['session']['email']:'' ?>" placeholder="Enter your email address">
            </td>
            </tr></table>
        </div>
        <button type="submit" class="submit-reg">Register</button><br>
    </form>
	<div id="login-link">
		<a href="<?php echo BASEURL;?>login.php">Back to Login Page</a>
	</div>
</div>
<script>
document.querySelector('select[name="group_id"]').addEventListener('change', function() {
    var selectedOption = this.options[this.selectedIndex];
    document.getElementById('group_name').value = selectedOption.getAttribute('data-group-name');
});
</script>
<?php
require 'login_footer.php';
?>
