<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



echo '<h1>Edit a User</h1>';

    if ((isset($_GET['id'])) ) {
        $id = $_GET['id'];
        } elseif ((isset($_POST['id'])) ) {
        $id = $_POST['id'];
    } else {
        echo '<p class="error">This page has been accessed in error.</p>';
        include('../../divs/footer.php');
        exit();
    }
    
    
require_once('../../login/mysqli_connect.php');


 $qgr = "SELECT groupofuse, groupname FROM groups";
 $rgr = mysqli_query($dbc, $qgr);
    
if (isset($_POST['submitted'])) {
    $errors = array();


    if (empty($_POST['group'])) {
        $errors[] = 'You forgot to enter the Group of Use.';
    } else {
        $gou = mysqli_real_escape_string($dbc, trim($_POST['group']));
    }

    
   

    if (empty($errors)) {

            
        $q = "UPDATE users
                  SET Groupofuse='$gou'
                  WHERE Username='$id'";
            $r = mysqli_query($dbc, $q);
            

            if (mysqli_affected_rows($dbc) == 1) {
                echo '<p>The user has been edited.</p>';
            } else {
                echo '<p class="error">The user could not be edited due to a system error.
                    We apologize for any inconvenience.
                    </p>';
              }
        
    } else {
        echo '<p class="error">The following error(s) occured:<br>';
        foreach ($errors as $msg) {
            echo " - $msg<br>\n";
        }
        echo '</p><p>Please try again.</p>';
    }
}


if ((isset($_GET['submitted2'])) && ($_GET['submitted2'])==TRUE) {
    
//    echo 'submitted2';

    $q = "UPDATE users
                  SET Password=SHA1('a')
                  WHERE Username='$id'";
            $r = mysqli_query($dbc, $q);
            

            if (mysqli_affected_rows($dbc) == 1) {
                echo '<p>The password has been reseted. Please inform the user to change it immediatelly</p>';
            } else {
                echo '<p class="error">The user could not be edited due to a system error,
                    or the password has already been reseted.
                    We apologize for any inconvenience.
                    </p>';
              }
    
} 

$q = "SELECT Username, Password, Groupofuse from users WHERE Username=?";
$stmt = mysqli_prepare($dbc, $q);
mysqli_stmt_bind_param($stmt, 's', $id);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);

if (mysqli_stmt_num_rows($stmt) == 1) {
    mysqli_stmt_bind_result($stmt, $id, $pas, $gou);
    mysqli_stmt_fetch($stmt);
    mysqli_stmt_close($stmt);
} else {
    echo '<p class="error">This page has been accessed in error dbc.</p>';
    mysqli_close($dbc);
    include('../../divs/footer.php');
    exit();
}
?>

    
<form name="Stoixeia" action="edit_user.php" method="post">
    
    <p>User Name: <b><?php echo $id; ?></b> Group of Use: <b><?php echo $gou?></b></p>
    
    <p>Group Of Use: 
        <select type="text" name="group" >
            <?php 
            
    while ($rowgr = mysqli_fetch_array($rgr, MYSQLI_ASSOC)) {
    if ($rowgr['groupofuse']== $gou) {
        echo '<option value="'.$rowgr['groupofuse'].'" selected>('.$rowgr['groupofuse'].') '.$rowgr['groupname'].'</option>';
        } else {  
    echo '<option value="'.$rowgr['groupofuse'].'">('.$rowgr['groupofuse'].') '.$rowgr['groupname'].'</option>';
    } }  
        ?>    
        </select>
        
    
    <p><input type="submit" name="submit" value="Αλλαγή Group"></p>
    <input type="hidden" name="submitted" value="TRUE">
    <input type="hidden" name="id" value="<?php echo $id; ?>"></input>
</form>
    <form name="Reset" action="edit_user.php" method ="get">
        <p>Reset password (press key) 
        <input type="submit" name="submit" value="Reset Password">
        <input type="hidden" name="submitted2" value="TRUE">
        <input type="hidden" name="id" value="<?php echo $id; ?>">
    </form>    

    
<?php
mysqli_close($dbc);


?>


    
    
    
    
    
    
    
    

    <!-- SIDEBAR -->



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




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