<?php 
if(!isset($_SESSION)) { 
  session_start(); 
  include('../includes/dbcon.php');
}
if(empty($_SESSION['rm_id'])):
header('Location:../index.php');
endif;

if(isset($_REQUEST['userid']))

  {
    $rid=$_GET['userid'];
    $stringParts = explode("*", $rid);
    $userid  = $stringParts[0];
    $loc_status= $stringParts[1];
    if ($loc_status=="Yes"){
      $cval=0;
    }
    else {
      $cval=1;
    }
    mysqli_query($con,"update users_master set Active=$cval where userid=$userid ")or die(mysqli_error());
}



?>
<!DOCTYPE html>
<html lang="en">


<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>Salesmgt | User Management </title>
  <!-- plugins:css -->
  <?php include('../includes/commonheader.php');?>



</head>
<body>
 <?php include('../includes/header.php');?>
 <?php include('../includes/leftsidebar.php');?>
           <!-- partial -->
 <div class="main-panel">        
        <div class="content-wrapper">

          <div class="page-header">
            <h3 class="page-title">
                New User
            </h3>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="#">User Management</a></li>
                <li class="breadcrumb-item active" aria-current="page">New User</li>
                </ol>
            </nav>
          </div>
          <div class="col-12 grid-margin">
              <div class="card">
                <div class="card-body">

              
         
                 
    <form class="needs-validation" novalidate method="post" action="usermgt_add.php" enctype="multipart/form-data" onsubmit="return validateForm()"> 
                  
                    <div class="row">
                      <div class="col-md-6">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Employee </label>
                          <div class="col-sm-9">
                           <select class="form-control select2" style="width: 100%;" name="emp" id="emp" required >
          <?php            
          $queryemp=mysqli_query($con,"select * from employee_master order by emp_name ")or die(mysqli_error());
          while($rowd=mysqli_fetch_array($queryemp)){
          ?>
          <option value="<?php echo $rowd['emp_id'];?>"><?php echo $rowd['emp_name'];?></option>        
          <?php }?>
          </select> 
                          </div>
                        </div>
                      </div>
                      <div class="col-md-6">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">User Name</label>
                          <div class="col-sm-9">
                            <input type="text" class="form-control" id="uname" name="uname" placeholder="User Name">
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="row">
                      <div class="col-md-6">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">New Password</label>
                          <div class="col-sm-9">
                            <input type="password" class="form-control" id="password" name="password" placeholder="Password">
                          </div>
                        </div>
                      </div>
                      <div class="col-md-6">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Confirm Password</label>
                          <div class="col-sm-9">
                            <input type="password" class="form-control" id="password1" name="password1" placeholder="Password">
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="row">
                      <div class="col-md-6">
                



    

                    
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">User Roll</label>
                          <div class="col-sm-9">
                              <select class="form-control select2" style="width: 100%;" name="alevel" id="alevel" required >
          <?php            
          $querydoc=mysqli_query($con,"select * from authority_levels ")or die(mysqli_error());
          while($rowd=mysqli_fetch_array($querydoc)){
          ?>
          <option value="<?php echo $rowd['Authority_Level'];?>"><?php echo $rowd['Authority_Des'];?></option>        
          <?php }?>
          </select> 
                          </div>        
      </div>

                        </div>
       
                  
                      </div>



  <div class="col-md-6">
                    </div>                                     
                 
                     <button type="submit" class="btn btn-primary">Save</button>
                    <button class="btn btn-light">Cancel</button>
                  </form>
                </div>
              </div>
            </div>

 <div class="col-12 grid-margin">
              <div class="card">
                <div class="card-body">
<h4 >Users List </h4>
                  <p class="card-description">
                    <button type="button" onclick="document.location='home.php'" class="btn btn-warning "><i class="fas fa-arrow-left"></i> Go Back
                    
                  </button>
                  </p>
                  <div class="table-responsive">
                    <table class="table table-hover">
                      <thead>
                        <tr>
                          <th>#</th>
                          <th>User Name</th>
                          <th>Employee</th>
                          <th>User Roll</th>
                          <th>Active</th>
                          <th>Edit</th> 
                        </tr>
                      </thead>
                      <tbody>
<?php
$rcount=0;
$query=mysqli_query($con,"SELECT users_master.userid,users_master.username,users_master.Active,employee_master.emp_name,
authority_levels.Authority_Des,authority_levels.Authority_Level,employee_master.emp_id
 FROM users_master
LEFT JOIN authority_levels ON authority_levels.Authority_level=users_master.al
LEFT JOIN `employee_master` ON employee_master.emp_id=users_master.emp_id")or die(mysqli_error()); 
 while($row=mysqli_fetch_array($query)){   $rcount=$rcount+1;
             
                $username=$row['username'];
                $userid=$row['userid'];
                $emp_name=$row['emp_name']; 
                $Active=$row['Active'];
                $Authority_Des=$row['Authority_Des'];

                if ($Active==1) {
                    $locactive="Yes";
                }
             
                if ($Active==0) {
                    $locactive="No";
                }

 ?>
   <tr>
                         <td><?php echo $rcount;  ?></td>
                         <td><?php echo $row['username'];?></td>
                         <td><?php echo $row['emp_name'];?></td>
                         <td><?php echo $row['Authority_Des'];?></td>
                         <td><?php if ($row['Active']>0) { ?> &#10004;<?php } else { ?> &#10006; <?php } ?><a href="usermgt_user.php?userid=<?php echo $row['userid'];echo "*";echo $locactive;?>"> Change</a></td>


                 


<td>
  <td><a href="#addvoucher<?php echo $row['userid'];?>" data-target="#addvoucher<?php echo $row['userid'];?>"data-toggle="modal"     </div> <i class="fas fa-edit "></i>  </i> </a> </td>         
</td> 
<!----------------------Model ------------------------------->
     <div id="addvoucher<?php echo $row['userid'];?>" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
          <div class="modal-dialog" style="width:80%; aria-hidden="true>
          <div class="modal-content" style="height:auto">        
          <div class="modal-body">

             <form class="form-horizontal" method="post" action="admin_user_edit.php" enctype='multipart/form-data'>

    <div class="form-group">
                  <label for="exampleInputPassword1">User Name</label>
                <input type="hidden" class="form-control" id="userid_edit" name="userid_edit" value="<?php echo $row['userid']; ?>">
                 <input type="text" class="form-control" id="username_edit" name="username_edit" value="<?php echo $row['username']; ?>">

                  </div>  
<div class="form-group">
                  <label for="exampleInputPassword1">Employee</label>
 <select class="form-control select2" style="width: 100%;" name="item_type_edit" id="item_type_edit" required >           
          <?php            
          $querydoc=mysqli_query($con,"select * from employee_master order by emp_name"
)or die(mysqli_error());          
          while($rowd=mysqli_fetch_array($querydoc)){
            if ($row['emp_id']==$rowd['emp_id']){ ?>            
            <option value="<?php echo $rowd['emp_id'];?>" selected><?php echo $rowd['emp_name'];?></option>
            <?php } else {
          ?>
          <option value="<?php echo $rowd['emp_id'];?>"><?php echo $rowd['emp_name'];?></option>        
          <?php } }?>
          </select>                  
</div>

<div class="form-group">
                  <label for="exampleInputPassword1">User Roll</label>
 <select class="form-control select2" style="width: 100%;" name="roll_edit" id="roll_edit" required >           
          <?php            
          $querydoc=mysqli_query($con,"select * from authority_levels "
)or die(mysqli_error());          
          while($rowd=mysqli_fetch_array($querydoc)){
            if ($row['Authority_Level']==$rowd['Authority_Level']){ ?>            
            <option value="<?php echo $rowd['Authority_Level'];?>" selected><?php echo $rowd['Authority_Des'];?></option>
            <?php } else {
          ?>
          <option value="<?php echo $rowd['Authority_Level'];?>"><?php echo $rowd['Authority_Des'];?></option>        
          <?php } }?>
          </select>                  
</div>
                </div>  





                   <div class="modal-footer">
                   <button type="submit" class="btn btn-primary">Save changes</button>
                   <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                   </div>                   
          </form>

   


          </div>
        </div>
      </div>
    </div>
<!--------------- End of Model ------------------------------------>


 <?php }  ?>
</tr>
                      </tbody>
                    </table>



        </div>  <!----- <div class="content-wrapper">------>

                </div>
              </div>
            </div>



        </div>  <!----- <div class="content-wrapper">------>
  

 </div><!-----End of main panel--~~~~~~~~~~~~~~~~~~~~~~~--------------->
 </div><!-----Dont Delete this DIV--~~~~~~~~~~~~~~~~~~~~~~~--------------->
  <!-- container-scroller -->
<?php include('../includes/footer.php');?>
<?php include('../includes/commonjs.php');?>
 
</body>


</html>

<script>
    $( document ).ready(function() {
         // alert("Please Select Order Type");

    });

function validateForm() { 

    if($('#uname').val().trim().length == 0) {
           alert("Please enter the User Name !!!");               
            return false;
    } ////````````````````````````````````````

   if ($('#emp').val() === "0") {
        alert("Please select a valid  Employee !");
        return false;
    }
    
     if ($('#alevel').val() === "0") {
        alert("Please select a user roll !");
        return false;
    }
//alevel



 } ///validateForm() { 

</script>

 <!-- End custom js for this page-->
  <script>
  $(function () {
    //Initialize Select2 Elements
    $('.select2').select2()

    //Initialize Select2 Elements
    $('.select2bs4').select2({
      theme: 'bootstrap4'
    })
  })

</script>



