<?php include("sass/icon_for_datepicker_and_datatable.php");?>
              
        <div class="wrapper row-offcanvas row-offcanvas-left">
            <!-- Left side column. contains the logo and sidebar -->
            <?php include("sass/sidebar.php");?>

            <!-- Right side column. Contains the navbar and content of the page -->
               <aside class="right-side">
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1>
                       <?php echo $company_name ; ?>
                        <small>Control panel</small>
                    </h1>
                    <ol class="breadcrumb">
                        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
                        <li class="active">Dashboard</li>
                    </ol>
                </section>
                 <section class="content">
                 	 <div class="row">
                        <div class="col-md-12" id="formdiv">
                 <!---Main Content--->

	<?php
$errMsg=""; 
if(isset($_POST['submit'])) // To check 'submmited or not' by if(isset($_POST['submit'])) 
{
	$user_name = mysqli_real_escape_string($connection,$_POST['user_name']);
	$password = mysqli_real_escape_string($connection,md5($_POST['password']));
	$type = $_POST['type'];
	$branch_id = $_POST['branch_id'];
	
	
	
	
	if(empty($user_name)) $errMsg="enter users name";
	if(empty($errMsg))
	{
		
		$check=mysqli_query($connection,"select * from users where user_name='$user_name'");
		$checkrows=mysqli_num_rows($check);
		if($checkrows>0){$errMsg="You have entered this User Name before";}
		
		else{
		
		mysqli_query($connection,"INSERT INTO`users`(`user_name`, `password`, `type`, `branch_id`) VALUES ('$user_name', '$password', '$type', '$branch_id') ")
		or die(mysqli_error()); 
		$errMsg="User Added Successfully";
	}
}
}



 if(isset($_POST['update']))
    {
	
$users_id = $_POST['users_id'];
$user_name = mysqli_real_escape_string($connection,$_POST['user_name']);
	$pass = $_POST['password'];
	$type = $_POST['type'];
	$branch_id = $_POST['branch_id'];
if(!empty($pass)){
	$password = mysqli_real_escape_string($connection,md5($_POST['password']));
	mysqli_query($connection,"UPDATE users SET password ='$password' WHERE users_id = '$users_id'")
			or die(mysqli_error()); 
}			
mysqli_query($connection,"UPDATE users SET user_name ='$user_name', type ='$type' 
, branch_id ='$branch_id' WHERE users_id = '$users_id'")
			or die(mysqli_error());  
	$errMsg="User Successfully Updated";
	}

?>
                 <?php
							if (!empty($errMsg))
							{echo "<div class='alert alert-warning alert-dismissible' role='alert'>$errMsg";
							echo "<button type='button' class='close' data-dismiss='alert'><span aria-hidden='false'>&times;</span><span class='sr-only'>		Close</span></button>
                        </div>";}
                        ?>
                 			<div class="box box-primary">
                                <div class="box-header">
                                    <h3 class="box-title">User Entry</h3>
                                </div><!-- /.box-header -->
                                <!-- form start -->
                                
                            
                                <form role="form"  action="<?php echo "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" method="post" id="login_form">
                                    <div class="box-body">
                                    	<div class="col-md-12">
                                        <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">User Name</label>
                                            <input type="text" class="form-control" name="user_name">
                                        </div>
										
                    <div class="form-group">
					<label for="exampleInputEmail1">Password</label>
                        <input type="password" class="form-control"  name="pass1" id="pass1" placeholder="New Password" required>
                    </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Confirm Password</label>
											 <span id="confirmMessage" class="confirmMessage"></span>
                                            <input type="password" class="form-control" name="password" id="pass2" onKeyUp="checkPass(); return false;" required>
                                        </div>
										
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Type</label>
											 <select class="form-control" name="type">
                                            	<option value="2"> Staff </option>
                                                <option value="1"> Admin </option>
                                                </select>  
                                        </div>
                                        
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Branch</label>
											<select name="branch_id" class="form-control">
                                            
												<?php  
                                                $query = "select branch_id, branch_name from branch";
                                                $result_techname = mysqli_query($connection,$query);
                                                
                                               
                                                while($row_techname=mysqli_fetch_array($result_techname))
                                                {
                                                
                                                echo "<option value='" . $row_techname['branch_id'] . "'>" . $row_techname['branch_name'] . "</option>";}?> 
                                            
                                            </select>
                                        </div>
                                        
                                        </div>
                                     
									</div>
												<!-- /.box-body -->

                                    <div class="box-footer">
                                        <button type="submit" name="submit" class="btn btn-primary">Submit</button>
                                    </div>
                                </form>
                            </div>
                 
                 		</div>
                    </div>
                                          <img src="img/ajax-loader1.gif" style="display: none; margin-left:30%;" id="loading_image" width="50%">      
                 
                 
                 </section>
                <script type="text/javascript">
function checkPass()
{
    //Store the password field objects into variables ...
    var pass1 = document.getElementById('pass1');
    var pass2 = document.getElementById('pass2');
    //Store the Confimation Message Object ...
    var message = document.getElementById('confirmMessage');
    //Set the colors we will be using ...
    var goodColor = "#66cc66";
    var badColor = "#ff6666";
    //Compare the values in the password field 
    //and the confirmation field
    if(pass1.value == pass2.value){
        //The passwords match. 
        //Set the color to the good color and inform
        //the user that they have entered the correct password 
        pass2.style.backgroundColor = goodColor;
        message.style.color = goodColor;
        message.innerHTML = "Passwords Match!"
    }else{
        //The passwords do not match.
        //Set the color to the bad color and
        //notify the user.
        pass2.style.backgroundColor = badColor;
        message.style.color = badColor;
        message.innerHTML = "Passwords Do Not Match!"
    }
}  
</script> 
                 
                 
                 <section class="content">
					<div class="box-body table-responsive" id="tbldiv">

                                    <table id="example1" class="table table-bordered table-striped">
                                        <thead>
                                            <tr>
                                                <th>User Name</th>
                                                 <th>Type</th> 
                                                <th>Branch</th>                                     
                                                <th>Action</th>
                                            </tr>

                                        </thead>
                                        <tbody>
										<?php
                                        
                                        $query = "SELECT * FROM users";
                                        $result = mysqli_query($connection,$query);
                                        
										 $i=0;
                                        while($row=mysqli_fetch_array($result))
                                        {
                                        
                                         $i++;
										 $users_id = $row['users_id'];
											$user_name = $row['user_name'];
											$type = $row['type'];
											$branch_id = $row['branch_id'];
											
											
											if($type=="1")
											{
											$typetxt = "Admin";
											}
											elseif($type=="2")
											{
											$typetxt = "Staff";
											}
                                      $result_customer_name = mysqli_query($connection,"SELECT * FROM branch WHERE branch_id = '$branch_id'");
		$test_customer_name = mysqli_fetch_array($result_customer_name);
				
				$branch_name=$test_customer_name['branch_name'];     
                                        
                                        echo "<tr>
                                        <td>$user_name
										<input type='hidden' id='user_name$i' value='$user_name'/>
										</td>
                                        <td>$typetxt
										<input type='hidden' id='type$i' value='$type'/>
										</td>
                                        <td>$branch_name
										<input type='hidden' id='branch_id$i' value='$branch_id'/>
										</td>
                                        
                                        <td>
                                       <input type='hidden' id='zipsearch$i' value='$users_id'/> <button type='button' class='btn btn-primary' data-toggle='modal' data-target='#myModal' onClick='sync$i()'><i class='fa fa-edit'></i> &nbsp;Edit</button>
									   
									   <button type='button' class='btn btn-danger' data-toggle='modal' data-target='#delmodel$i'><i class='fa fa-times'></i> &nbsp;Delete</button>
                                        </td> 
                                        </tr>";
										
										
										echo "<script>
function sync$i()
{
  var collect = document.getElementById('zipsearch$i');
  var entry = document.getElementById('xpaytype');
  
  
  var user_namecollect = document.getElementById('user_name$i');
  var user_nameentry = document.getElementById('user_name');
 
  var typecollect = document.getElementById('type$i');
  var typeentry = document.getElementById('type');
  
  
  var branch_idcollect = document.getElementById('branch_id$i');
  var branch_identry = document.getElementById('branch_id');
 
  
  entry.value = collect.value;
  user_nameentry.value = user_namecollect.value;
  typeentry.value = typecollect.value;
  branch_identry.value = branch_idcollect.value;
  
}
</script> ";

echo "<div class='modal fade' id='delmodel$i' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>
  <div class='modal-dialog'>
    <div class='modal-content'>
      <div class='modal-header'>
        <button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button>
        <h4 class='modal-title' id='myModalLabel'>Delete Package</h4>
      </div>
      <div class='modal-body'>
    Are You sure you want to delete  $user_name ?
      </div>
      <div class='modal-footer'>
        <button type='button' class='btn btn-default' data-dismiss='modal'>No</button>
        <a href='delete.php?tbl=users&idcol=users_id&id=$users_id&redirect=add_user'><button type='button' class='btn btn-danger'>Yes</button></a>
      </div>
    </div>
  </div>
</div>";

										
										}	
                                        ?>

                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <th>User Name</th>
                                                 <th>Type</th> 
                                                <th>Branch</th>                                     
                                                <th>Action</th>
                                            </tr>

                                        </tfoot>
                                    </table>
                                    
                                    
                                   <form method='post' action='<?php echo $_SERVER['SCRIPT_NAME']; ?>'>
										<div class='modal fade' id='myModal' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>
  <div class='modal-dialog'>
    <div class='modal-content'>
      <div class='modal-header'>
        <button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button>
        <h4 class='modal-title' id='myModalLabel'>Update User</h4>
      </div>
      <div class='modal-body'>
      <input type="hidden" id='xpaytype' name='users_id' class='form-control'/>
        
                    <div class="form-group">
                        <input type="text" id='user_name' name="user_name" class="form-control"/>
                    </div>
                    <div class="form-group">
                        <input type="text" name="password" class="form-control" placeholder="password"/>
                    </div>
                    <div class="form-group">
						<select class="form-control" id='type' name="type">
                                            	<option value="2"> Staff </option>
                                                <option value="1"> Admin </option>
                                                </select>  
                    </div>
                    <div class="form-group">
                        <select id='branch_id' name="branch_id" class="form-control">
                                            
												<?php  
                                                $query = "select branch_id, branch_name from branch";
                                                $result_techname = mysqli_query($connection,$query);
                                                
                                               
                                                while($row_techname=mysqli_fetch_array($result_techname))
                                                {
                                                
                                                echo "<option value='" . $row_techname['branch_id'] . "'>" . $row_techname['branch_name'] . "</option>";}?> 
                                            
                                            </select>
                    </div>
                    
      </div>
      <div class='modal-footer'>
        <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
        <button type='submit'  name="update" class='btn btn-primary'>Save</button>
      </div>
    </div>
  </div>
</div>
</form>








                                  
                                </div>
                </section>
                 
                </aside>
                <!-- /.right-side -->
        </div><!-- ./wrapper -->

        <!-- add new calendar event modal -->
<script type="text/javascript">
	 $('#login_form').submit(function() {
	$('#formdiv').hide();
	$('#tbldiv').hide();
    $('#loading_image').show(); // show animation
    return true; // allow regular form submission
});
 </script>

        <!-- jQuery 2.0.2 -->
         <?php include("sass/footer_for_datepicker_and_datatable.php");?>

    </body>
</html>