<?php 
session_start();
require_once 'includes/database.php';
require_once 'includes/funcs.php';
if (!isset($_SESSION['reusername']) || ($_SESSION['reusergroup'] > 1)) { echo 'Sorry You Dont Have permissions to View this Page!'; exit();}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head> <title>Modify Users Access</title>
        <script type="text/javascript" src="js/jquery-1.10.2.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.10.4.custom.js"></script>
        <link rel="stylesheet" href="css/allcss.css" type="text/css" />
        <link rel="stylesheet" href="css/jquery-ui-1.10.4.custom.css" type="text/css" />
        <style type="text/css">
            html, body {margin:0; padding:0; height:100%; width:100%; min-width:100%; min-height:100%;}
            .titletr {
            text-align: left;
            width:100%;
            padding-left:10px; padding-right:5px; 
            background: #DEDEB4;
            box-shadow: 0px 0px 5px rgba(0,0,0,0.5); 
            font-family:Calibri, Arial, Helvetica, sans-serif;
            font-weight:bold;
            font-size:14px;
            color:#7A2525;
            }
        </style>
</head>
<body>		
<p class="titletr">Modify Users <?php echo str_repeat('&nbsp;', 180); ?><a href="#"><img src="images/close.png" onclick="window.close();"> </a></p> 
<?php
if (isset($_POST['addsavuname'])) {         //Save users table
        $rl = intval($_POST['rl']); $orl = intval($_POST['orl']);  
		$db=new MySQLDatabase();
		$db->open_connection();
        $i=1;
        while ($i < $orl) {
            $uname = htmlspecialchars($_POST['uname'.$i]);
			$pwd = htmlspecialchars($_POST['pwd'.$i]);
			$grp = htmlspecialchars($_POST['grp'.$i]);
            $fid = htmlspecialchars($_POST['fid'.$i]);
            $ts = date('Y-m-d h:i:s');
            $query = "UPDATE accesslevels SET username = '$uname', password = '$pwd', user_group = '$grp', ts = '$ts' WHERE uid = '$fid'";
            $result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
            $i++;
        }
        while ($i < $rl) {
            $uname = htmlspecialchars($_POST['uname'.$i]);
			$pwd = htmlspecialchars($_POST['pwd'.$i]);
			$grp = htmlspecialchars($_POST['grp'.$i]);
            $ts = date('Y-m-d h:i:s');
            $query = "INSERT INTO accesslevels (username, password, user_group, ts) VALUES ('$uname','$pwd', '$grp', '$ts')";
            $result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
            $i++;
        }
		$db->close_connection();
        if(!$result) { echo "ERROR in Saving Users Table!"; }
        else { echo '<div id="dialog-message" title="Users Modification Status">
                        <p><span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
                        User Credentials Updated!</p></div>';
                }
}
elseif (isset($_GET['deluname'])) {         //Delete uname Code
	$id = htmlspecialchars($_REQUEST['id']);
	$db=new MySQLDatabase();
	$db->open_connection();
	$query = "DELETE FROM accesslevels WHERE uid = '$id'";
	$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
	$db->close_connection();
	if(!$result) { echo "ERROR in Deleting User!"; }
    else { echo '<div id="dialog-message" title="User Modification Status">
                        <p><span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
                        User Deleted!</p></div>';
          }	
}

else {
   echo '<form id="unamelist" method="post" action="modpss.php" > <center>'; 
   echo '<table cellspacing="10px" align="center" style="width:80%; margin-left:10px;" >'; 
   echo '<tr><td align="center"><input type="text" id="btnadduname" name="btnadduname" onclick="getunamerow();" class="text ui-widget-content ui-corner-all" value="   Add New User" style="width:160px;font-weight:bold;"/></td></tr>';	 
   echo '</table><br/>';
   
   echo '<table id="tablelist" style="width:90%;margin-left:10px;" class="display">
    <thead>
    <tr> <th align="center" style="width:40px;">No. </th> <th align="center" style="width:180px;">User Name </th> <th align="center" style="width:180px;">Password </th> <th align="center" style="width:120px;">Group Code </th> <th align="center" style="width:140px;">ACTION </th> </tr>
    </thead>'; 
	$db=new MySQLDatabase();
	$db->open_connection();
    $query = 'SELECT * FROM accesslevels ORDER BY username;';
    $result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
    $odd = true; $i=1;
    while ($row = mysqli_fetch_assoc($result)) 
        {
        echo ($odd == true) ? '<tr class = "odd_row">' : '<tr class = "alt">';
        $odd = !$odd;
        echo'<td>';
		echo '<input type="text" id="id'.$i.'" name="id'.$i.'" value="'.$row['uid'].'" size="8" readonly="readonly">';
        echo '</td><td>';
        echo '<input type="text" id="uname'.$i.'" name="uname'.$i.'" value="'.$row['username'].'" size="30">';
        echo '</td><td>';
		echo '<input type="text" id="pwd'.$i.'" name="pwd'.$i.'" value="'.$row['password'].'" size="30">';
		echo '</td><td>';
		echo '<input type="text" id="grp'.$i.'" name="grp'.$i.'" value="'.$row['user_group'].'" size="8">';
        echo '</td><td>';
        echo '<a href="modpass.php?deluname=true&id='. $row['uid'].'"> DELETE</a> <input type="hidden" id="fid'.$i.'" name="fid'.$i.'" value="'.$row['uid'].'">';
        echo '</td></tr>';
        $i++;
        }   
    echo '<input type="hidden" id="rl" name="rl" value="'.$i.'"><input type="hidden" id="orl" name="orl" value="'.$i.'">';
    echo '<tr id="trow"><br/></tr>';    
    echo '<tr align="center"><td colspan=3><input type="submit" id="addsavuname" name="addsavuname"  class="text ui-widget-content ui-corner-all" value=" Save Users  " style="width:120px;font-weight:bold;"/></td></tr>';
    echo '</table>';
    echo '</center></form>';
	$db->close_connection();
}
?>

</body>

</html>
<script type="text/javascript"> 
$(function() {
	// dialog boxes
    $( "#dialog:ui-dialog" ).dialog( "destroy" );

		$( "#dialog-message" ).dialog({
			modal: true,
			width: 400,
			buttons: {
				Ok: function() {
					window.close();
				}
			}
		});
});

function getunamerow(){ 
var rl = $("#rl").val();    
 $('#trow').before('<tr><td><input type="text" id="id'+rl+'" name="id'+rl+'" value="New" readonly="readonly" size="8"></td>'
                        +'<td><input type="text" id="uname'+rl+'" name="uname'+rl+'" value="" size="30"></td>'
						+'<td><input type="text" id="pwd'+rl+'" name="pwd'+rl+'" value="" size="30"></td>' 
						+'<td><input type="text" id="grp'+rl+'" name="grp'+rl+'" value="" size="8"></td><tr>');   
 document.getElementById("rl").value = parseInt(rl)+1;
}; 
</script>


