<?php include("conn.php");
$page='Users Management';?>
<!DOCTYPE html>
<html lang="en">
<?php include("common/head.php")?>
<body>
<?php include("common/menu.php")?>
<!---side nav bar end-->
<?php include("common/tittle.php")?>
            <!--Main body Start-->
            <div id="accordions" class="col-sm-12 mt-2">
              
              
                   <hr class="bg-success">
   
                <!--Orders start-->

                <!--order-items-->
                <!--order-items-one-->
               <div style="overflow: scroll; height:100vh;"  id="collapseOne" class="  col-sm-12  " data-parent="#accordions">
                    <h5 class="text-center">User Management</h5>
                    <button class="btn btn-primary btn-sm m-4"  data-toggle="modal" data-target="#exampleModal">Add User</button>

                    <table id="example_table" class="table-customer table-striped table-hover  w-100 table-bordered">
                        <thead>
                        <tr>
                            <th>id</th>
                            <th>Image</th>
                            <th>User Name</th>
                            <th>Number</th>
                            <th>Email</th>
                            <th>Role</th>
                            <th>Status</th>
                            <th>Action</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php

      

$sql="select * from users order by id desc";
$result = mysqli_query($conn,$sql)or die( mysqli_error($conn));
while ($row=mysqli_fetch_array($result)) {
 $data=$row['id'];
   $fname=$row['fname'];
   $sname=$row['sname'];
   $number=$row['number'];
   $email=$row['email'];
   $image=$row['image'];
   $status=$row['status'];
   $role=$row['role'];
   

 $link="delete/del_blog.php?id=".urlencode(base64_encode($data));
 $edit_user="edit/edit_user.php?id=".urlencode(base64_encode($data));
 $activate="edit/user_status.php?id=".urlencode(base64_encode($data));
 $dat=$data;


 $text='';

 if ($status==='1') {
   $text='Active';
   $read='btn-success';
   $de='deactivate';
   $sta=0;
 }
                            else {
   $text='Inactive';
   $read='btn-danger';
  $de='activate';
  $sta=1;
 }

?>
                        <tr>
                            <td><?php echo $data ?></td>
                            <td><img style="width: 70px;" src="user_image/<?php echo $image ?>" alt=""></td>
                            <td><?php echo $fname ." ".$sname ?></td>
                            <td><?php echo $number ?></td>
                            <td><?php echo $email ?></td>
                            <td><?php echo $role ?></td>
                            <td>
                           

                            <input type="button" name="view" value="<?php echo $text ?>" id="<?php echo $row["id"]; ?>" class="btn   <?php echo $read ?> btn-sm view_data" />
                        
                        
                        </td>
                            <td>
                                
                            <input  type="button" name="view" value="Delete" id="<?php echo $row["id"]; ?>" class="btn  btn-danger btn-sm view_datas" />

                                <a href="<?=$edit_user; ?>" type="button" class="btn btn-success view-details btn-sm">Edit</a></td>
                        </tr>
                        <?php } ?>
                        <thead>
                            <th>id</th>
                            <th>Image</th>
                            <th>User Name</th>
                            <th>Number</th>
                            <th>Email</th>
                            <th>Role</th>
                            <th>Status</th>
                            <th>Action</th>
                    </tbody>
                    </table>
    

                </div>

        


       

         
                <!--Order end-->
            </div>
         


<!-- Small modal -->
<div id="dataModal"  class="modal fade bd-example-modal-sm" tabindex="-1" data-backdrop="static" data-keyboard="false" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-sm">
    <div id="employee_detail" class="modal-content text-dark">
     
    
    
    </div>
  </div>
</div>

<!-- Small modal -->
<div id="dataModals"  class="modal fade bd-example-modal-sm" tabindex="-1" data-backdrop="static" data-keyboard="false" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-sm">
    <div id="employee_details" class="modal-content text-dark">
     
    
    
    </div>
  </div>
</div>

<!-- Small modal -->
<div id="dataModalse"  class="modal fade bd-example-modal-sm" tabindex="-1" data-backdrop="static" data-keyboard="false" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-sm">
    <div id="employee_detailse" class="modal-content text-dark">
     
    
    
    </div>
  </div>
</div>

                <!-- Modal Add users -->
  <div  class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content ">
        <div class="modal-header">
          <h5 class="modal-title text-center text-dark" id="exampleModalLabel">Add User</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
            <form method="post" class="text-dark">
                <div class="form-group">
                  <label for="exampleFormControlInput1">User FirstName</label>
                  <input type="text" class="form-control" name="fname" id="exampleFormControlInput1" placeholder=" User first name"  required>
                </div>
                <div class="form-group">
                  <label for="exampleFormControlInput1">User LastName</label>
                  <input type="text" class="form-control" name="sname" id="exampleFormControlInput1" placeholder=" User last name" required>
                </div>
                <div class="form-group">
                  <label for="exampleFormControlInput1">Phone Number</label>
                  <input type="number" class="form-control" name="number" id="exampleFormControlInput1" placeholder=" User Phone number"   pattern="^\d{10}$" title="Phone number must be upto 10 values" required>
                </div>
                <div class="form-group">
                  <label for="exampleFormControlInput1">Email Address</label>
                  <input type="email" class="form-control" name="email" id="exampleFormControlInput1" placeholder=" Email" required>
                </div>

                <div class="form-group">
                  <label for="exampleFormControlSelect1">Select Role</label>
                  <select class="form-control" name="role" id="exampleFormControlSelect1">
                  <option value="nan">Select Role</option>
                  <option value="Admin">Admin</option>
                    <option value="bar attendance">Bar attendance</option>
                     <option value="House keeper">House keeper</option>
                    <option value="Manager">Manager</option>
                    <option value="Cheif">Cheif</option>
                    <option value="Cashier">Cashier</option>
                  </select>
                </div>
                <input type="hidden" name="image" value="user.jpg">
                <input type="hidden" name="password" value="1234">
                <input type="hidden" name="date" value="<?php echo date("F j, Y") ?>">
                
                  <button class="btn btn-success btn-sm"  name="submit">Submit</button>
              </form>
        </div>
        <div class="modal-footer">
        </div>
      </div>
    </div>
  </div>

          



  <?php 


include('conn.php');


if(isset($_POST['submit'])){
 
    $fname=addslashes($_POST['fname']);
     $sname=addslashes($_POST['sname']);
     $email=$_POST['email'];
     $number=$_POST['number'];
    $password=$_POST['password'];
     $role=$_POST['role'];
     $date=$_POST['date'];
     $image=$_POST['image'];
     $sql = "SELECT `email` FROM `users` WHERE `email`='".$email."'";
     $result = $conn->query($sql);
     
     if($result->num_rows >= 1) {
       echo"<script>alert('Email already exits please use another email'); window.location='user'</script>";
     } else {
		// Image db insert sql
		$sql = "INSERT INTO `users`(`fname`, `sname`, `number`, `email`, `image`,`password`, `role`, `date`) VALUES ('$fname','$sname','$number','$email','$image','$password','$role','$date')";
		if(mysqli_query($conn,$sql)){

      echo"<script>alert('User added succefully'); window.location.href='input_user_role.php?email=$email'</script>";
		}
		else{
		  echo 'Error: '.mysqli_error($conn);
		}

     }

}

?>
      
       

   
    
    <!--  isotope plugin cdn  -->
  <!--  isotope plugin cdn  -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.isotope/3.0.6/isotope.pkgd.min.js" integrity="sha256-CBrpuqrMhXwcLLUd5tvQ4euBHCdh7wGlDfNz8vbu/iI=" crossorigin="anonymous"></script>
    <script src="js/script.js"></script>
    <!--  isotope plugin cdn  -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.isotope/3.0.6/isotope.pkgd.min.js" integrity="sha256-CBrpuqrMhXwcLLUd5tvQ4euBHCdh7wGlDfNz8vbu/iI=" crossorigin="anonymous"></script>
    <script src="js/script.js"></script>
     <!-- jQuery js-->
   <!-- jQuery Datatable js -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" ></script>
     <script src="https://cdn.datatables.net/2.0.5/js/dataTables.js"></script>
     <script src="https://cdn.datatables.net/buttons/3.0.2/js/dataTables.buttons.js"></script>
     <script src="https://cdn.datatables.net/buttons/3.0.2/js/buttons.dataTables.js"></script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js"></script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.js"></script>
     <script src="https://cdn.datatables.net/buttons/3.0.2/js/buttons.html5.min.js"></script>
     <script src="https://cdn.datatables.net/buttons/3.0.2/js/buttons.print.min.js"></script>

     <script>
      $(document).ready( function () {
        new DataTable('#example_table', {
            order:[[2,"asc"]],
            autoFill: true,
            pageLength : 30,
    lengthMenu: [[5, 10, 20, -1], [5, 10, 20, 'Todos']],
    layout: {
        topStart: {
            buttons: ['copy', 'csv', 'excel', 'pdf', 'print']
        }
    }
});
       } );


      
</script>
<script>
         $(document).ready(function(){  
      $('.view_data').click(function(){  
           var employee_id = $(this).attr("id");  
           $.ajax({  
                url:"user_status.php",  
                method:"post",  
                data:{employee_id:employee_id},  
                success:function(data){  
                     $('#employee_detail').html(data);  
                     $('#dataModal').modal("show");  
                }  
           });  
      });  
 });  
     </script>

<script>
         $(document).ready(function(){  
      $('.view_datas').click(function(){  
           var employee_ids = $(this).attr("id");  
           $.ajax({  
                url:"user_del.php",  
                method:"post",  
                data:{employee_ids:employee_ids},  
                success:function(data){  
                     $('#employee_details').html(data);  
                     $('#dataModals').modal("show");  
                }  
           });  
      });  
 });  
     </script>

<script>
         $(document).ready(function(){  
      $('.view_datase').click(function(){  
           var employee_idse = $(this).attr("id");  
           $.ajax({  
                url:"user_edit.php",  
                method:"post",  
                data:{employee_idse:employee_idse},  
                success:function(data){  
                     $('#employee_detailse').html(data);  
                     $('#dataModalse').modal("show");  
                }  
           });  
      });  
 });  
     </script>

</body>

</html>