<meta charset="utf-8">
<link href="../../css/templatemo_style.css" rel="stylesheet" type="text/css" />
<?php
session_start();



if ($_SESSION['GROUPOFUSE'] == 0) {
    $pagetitle = "Διαχειριστή!!";
    include('../../divs/header.php');
} else {
    require_once('../../login/login_functions.inc.php');
    $url = absolute_url('../../login/login.php');
    header("Location: $url");
    exit();
}
?>


<!--  MENU  -->

<div id="templatemo_menu_wrapper">

    <div id="templatemo_menu">
        <ul>
            <li><a href="loggedin0.php" class="current"><span></span>Αρχική</a></li>
         
             
                <li><a href="eggrafi_user.php" target="_parent" ><span></span>Εγγραφή νέου Χρήστη</a></li>
                <li><a href="view_users.php" target="_parent" ><span></span>Εμφάνιση-Επεξεργασία Χρηστών</a></li>
             
            <li><a href="logout.php" target="_parent"><span></span>Logout </a></li>
        </ul>    
        
	</div>
        </ul>   
        
	</div>
    
    

</div>
</div> <!-- end of templatemo_menu -->

<div id="templatemo_main"> <span class="tm_bottom"></span>


    <!-- CONTENT -->
<?php


if (isset($_POST['submitted'])) {
    $errors = array();

    require_once('../../login/mysqli_connect.php');


    if (empty($_POST['groupofuse'])) {
        $errors[] = 'You forgot to enter the Group of Use.';
    } else {
        $gou = mysqli_real_escape_string($dbc, trim($_POST['groupofuse']));
    }
    
    if (empty($_POST['usern'])) {
        $errors[] = 'You forgot to enter your user name.';
    } else {
        $un = mysqli_real_escape_string($dbc, trim($_POST['usern']));
        $q = "SELECT Username from users where Username='$un'";
        $r = mysqli_query($dbc, $q);
        if (mysqli_num_rows($r) != 0) {
            $errors[] = 'The user name you provided already exists';
        }
    }

    if (empty($_POST['pass1'])) {
        $errors[] = 'You forgot to enter your password.';
    } else {
        if ($_POST['pass1'] != $_POST['pass2']) {
            $errors[] = 'The two passwords provided do not match.';
        } else
            $p = mysqli_real_escape_string($dbc, trim($_POST['pass1']));
    }

    if (!empty($errors)) {
        echo '<h1>Error!</h1>
            <p class="error">The following error(s) occured:<br>';
        foreach ($errors as $msg) {
            echo " -  $msg<br>\n";
        }
        echo '</p><p>Please try again.</p><p><br></p>';
    } else {
                        
        $q= "INSERT INTO users 
            (Username, Password, Groupofuse)
            VALUES (?, SHA1(?), ?)";
                       
        $stmt = mysqli_prepare($dbc, $q);
        
        mysqli_stmt_bind_param($stmt, 'sss', $un, $p, $gou);
        mysqli_stmt_execute($stmt);
                     
        if (mysqli_stmt_affected_rows($stmt) == 1) {
            echo '<h1>Thank you!</h1>
                <p>The user is now registered.</p>';
        } else {
            echo '<h1>System Error</h1>
                <p class="error">New user could not be registered due to a system error.
                We apologize for any inconvenience.</p>';
            echo '<p>' . mysqli_error($dbc) . '<br> <br>
                Query: ' . $q . '</p>';
        }
                     
        
        mysqli_close($dbc);
        //include('includes/footer.html');
        exit();
    }
    mysqli_close($dbc);
}
?>

<h1>Εγγραφή νέου Χρήστη</h1>
<form action="eggrafi_user.php" method="post">
    
    <p>User Name: 
        <input type="text" name="usern" size="20"
               maxlength="60" 
                              value="<?php if (isset($_POST['usern'])) echo $_POST['usern']; ?>">
    </p>
    <p>Group of use: 
        <input type="text" name="groupofuse" size="10"
               maxlength="40"
                              value="<?php if (isset($_POST['groupofuse'])) echo $_POST['groupofuse']; ?>">
    </p>
    <p>Password: 
        <input type="password" name="pass1" size="10"
               maxlength="20" >
    </p>
    <p>Confirm Password: 
        <input type="password" name="pass2" size="10"
               maxlength="20" >
    </p>
    <p><input type="submit" name="submit" value="Εγγραφή" ></p>
    <input type="hidden" name="submitted" value="TRUE">
</form>

    
    
    
    
    
    
    
    
    
    
    

    <!-- SIDEBAR -->



    <div class="cleaner"></div>
</div> 




<?php
include('../../divs/footer.php');
?>