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



if (isset($_SESSION['GROUPOFUSE']) && ($_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>Εγγεγραμμένοι Χρήστες</h1>\n";

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

$display = 10; // 10 lines of data per page

if (isset($_GET['p']) && is_numeric($_GET['p'])) {
    $pages = $_GET['p'];
} else {
    $q = "SELECT COUNT(Username)as a FROM users";
    $r = mysqli_query($dbc, $q);
    $row = mysqli_fetch_array($r, MYSQLI_ASSOC);
    $records = $row['a'];
//echo  $records;
    $num=$records;
    if ($records > $display) {
        $pages = ceil($records / $display);
    } else {
        $pages = 1;
    }
}

if (isset($_GET['s']) && is_numeric($_GET['s'])) {
    $start = $_GET['s'];
} else {
    $start = 0;
}

$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'un';
switch ($sort) {
    case 'un':
        $order_by = 'Username ASC';
        break;
    case 'unr':
        $order_by = 'Username DESC';
        break;
    case 'gr':
        $order_by = 'Groupofuse ASC';
        break;
    case 'grr':
        $order_by = 'Groupofuse DESC';
        break;
     
    default:
        $order_by = 'Username ASC';
        $sort = 'un';
        break;
}

$q = "SELECT Username, Password, Groupofuse
    FROM users ORDER BY $order_by
    LIMIT $start, $display";

$r = mysqli_query($dbc, $q);

//$num = mysqli_num_rows($r);

if ($r) {
    //Correct execution
    $sort_un = ($sort == 'un')? 'unr': 'un';
    $sort_gr = ($sort == 'gr')? 'grr': 'gr';
   
    //$num=$records;
   if ((!isset($num)) OR ($num==null)){
       $num=$_GET['r'];
   }
    echo "<p>Αυτή τη στιγμή είναι εγγεγραμμένοι $num χρήστες.</p>\n";
    echo '<table align="center" cellspacing="3" cellpadding="3" width="75%">
          <tr>
             <td align="left"><b>Edit</b></td>
             <td align="left"><b>Delete</b></td>
             <td align="left"><b>
             <a href="view_users.php?sort=' . $sort_un . '">Username</a></b></td>
             <td align="left"><b>
             <a href="#">Password</a></b></td>
             <td align="left"><b>
             <a href="view_users.php?sort=' . $sort_gr . '">Group of Use</a></b></td>     

          </tr>
          ';
    $bg = '#eeeeee';
    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
        $bg = ($bg == '#eeeeee' ? '#ffffff' : '#eeeeee');
        echo '<tr bgcolor="' . $bg . '">
            <td align="left"><a href="edit_user.php?id=' .
        $row['Username'] . '">Edit</a></td>
            <td align="left"><a href="diagrafi_user.php?id=' .
        $row['Username'] . '">Delete</a></td>
            <td align="left">' . $row['Username'] . '</td>
            <td align="left">' . $row['Password'] . '</td>
            <td align="left">' . $row['Groupofuse'] . '</td>
           
              </tr>
              ';
    }
    echo '</table>';

    mysqli_free_result($r);
} else {
    //Incorrect query execution
    echo '<p class="error">The current users could not be retrieved.
        We apologize for any inconvenience.</p>
        ';
    echo '<p>' . mysqli_error($dbc) . '<br>Query: ' . $q . '</p>';
}
mysqli_close($dbc);

if ($pages > 1) {
    echo '<br><p>';
    $current_page = ($start / $display) + 1;

    // If not the first page, create a Previous button
    if ($current_page != 1) {
        echo '<a href="view_users.php?s=' . ($start - $display) .
        '&p=' . $pages . '&sort=' . $sort . '&r='.$num.'">Προηγούμενη</a> ';
    }

    // Create all the numbered pages
    for ($i = 1; $i <= $pages; $i++) {
        if ($i != $current_page) {
            echo '<a href="view_users.php?s=' . (($i - 1) * $display) .
            '&p=' . $pages . '&sort=' . $sort . '&r='.$num.'">' . $i . '</a> ';
        } else {
            echo $i . ' ';
        }
    }

    // If not the last page, create a Next button
    if ($current_page != $pages) {
        echo ' <a href="view_users.php?s=' . ($start + $display) .
        '&p=' . $pages . '&sort=' . $sort . '&r='.$num.'">Επόμενη</a>';
    }
}

?>


    
    
    
    
    
    
    
    

    <!-- SIDEBAR -->



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




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