<?php
include_once 'header.php';
?>

<div class="page-wrapper">
        <!-- ============================================================== -->
        <!-- Bread crumb and right sidebar toggle -->
        <!-- ============================================================== -->
        <div class="page-breadcrumb">
          <div class="row">
            <div class="col-12 d-flex no-block align-items-center">
              <h4 class="page-title">All User List</h4>
              <div class="ms-auto text-end">
                <nav aria-label="breadcrumb">
                  <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="#">Dashboard</a></li>
                    <li class="breadcrumb-item active" aria-current="page">
                     Configuration
                    </li>
                    <li class="breadcrumb-item"><a id="add" href="#">Add User</a></li>
                  </ol>
                </nav>
              </div>
            </div>
          </div>
        </div>
        <!-- ============================================================== -->
        <!-- End Bread crumb and right sidebar toggle -->
    
        <div class="container-fluid row">
            <div class="col-12">  
              <div class="card">
                <div class="card-body">
                  <h5 class="card-title"> </h5>
                  <div class="table-responsive">
                    <table id="zero_config" class="table table-striped table-bordered">
                      <thead>
                        <tr>
                          <th>S.No</th>
                          <th>User Name</th>
                          <th>User Id</th>
                          <th>Access Level</th>
                          <th>Status</th>
                          <th>Action</th>
                        </tr>
                      </thead>
                      <tbody id="tbody">
                      <?php
                                
                                $obj=new Database();
                                $obj->select('user','*',null,null,'id DESC',null);
                                $result=$obj->getResult(); 
                                $c=0;
                                                              
                                foreach($result as list("id"=>$id,"user_name"=>$b_no,"user_id"=>$name,"access_level"=>$dob,"status"=>$date))
                                {                                 
                                  $c=$c+1;
                                  echo "<tr><td>$c</td><td>$b_no</td><td>$name</td><td>$dob</td><td>$date</td><td><button class='badge btn-danger' name='edit[]' data-id=$id id='edit'>Edit</button>&nbsp;&nbsp;<button class='badge btn-danger' name='id[]' data-id=$id id='id'>Delete</button></td></tr>";
                                }                              
                                ?>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <!-- ============================================================== -->
          <!-- End PAge Content -->
          <!-- ============================================================== -->
          <!-- ============================================================== -->

<!-- The Modal -->
<div class="modal" id="myModal">
  <div class="modal-dialog">
    <div class="modal-content">

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Add User</h4>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>

      <!-- Modal body -->
      <div class="modal-body">
        <form id="modelForm">
                    <div class="form-group">
                        <input type="text" id="user_name" class="form-control" placeholder="Enter User Name">
                    </div>
                    <div class="form-group">
                        <input type="email" id="user_id" class="form-control" placeholder="Enter User Id">
                    </div>
                    <div class="form-group">
                        <input type="password" id="password" class="form-control" placeholder="Enter Password">
                    </div>
                    <label class="col-sm-3 control-label col-form-label" for="level">Access Level</label>
                    <div class="col-md-9" id="level">
                      <div class="form-check">
                        <input type="radio" class="form-check-input" id="radio1" name="optradio" value="1" checked>Admin
                        <label class="form-check-label" for="radio1"></label>
                      </div>
                      <div class="form-check">
                        <input type="radio" class="form-check-input" id="radio2" name="optradio" value="2">Staff
                        <label class="form-check-label" for="radio2"></label>
                      </div>
                  </div>

        </form>
      </div>
      <!-- Modal footer -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" id="save-data">Save Record</button>
      </div>

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

<div class="modal" id="myModal1">
  <div class="modal-dialog">
    <div class="modal-content">

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Update User</h4>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>

      <!-- Modal body -->
      <div class="modal-body">
        <form id="modelForm">
                    <div class="form-group">
                    <label class="col-sm-3 control-label col-form-label" for="level">User Name</label>
                        <input type="hidden" id="id1" class="form-control" >
                        <input type="text" id="user_name1" class="form-control">
                    </div>
                    <div class="form-group">
                    <label class="col-sm-3 control-label col-form-label" for="level">User ID</label>
                        <input type="email" id="user_id1" class="form-control">
                    </div>
                    <div class="form-group">
                    <label class="col-sm-3 control-label col-form-label" for="level">Password</label>
                        <input type="password" id="password1" class="form-control">
                    </div>
              

        </form>
      </div>
      <!-- Modal footer -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" id="edit-data">Update Record</button>
      </div>

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


          <footer class="footer text-center">
          All Rights Reserved by ECI. Designed and Developed by
          <a href="https://www.ntlogix.in">NTLogix</a>.
        </footer>
       
   <!-- All Jquery -->
    <!-- ============================================================== -->
    <script src="../assets/libs/jquery/dist/jquery.min.js"></script>
    <!-- Bootstrap tether Core JavaScript -->
    <script src="../assets/libs/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <!-- slimscrollbar scrollbar JavaScript -->
    <script src="../assets/libs/perfect-scrollbar/dist/perfect-scrollbar.jquery.min.js"></script>
    <script src="../assets/extra-libs/sparkline/sparkline.js"></script>
    <!--Wave Effects -->
    <script src="../dist/js/waves.js"></script>
    <!--Menu sidebar -->
    <script src="../dist/js/sidebarmenu.js"></script>
    <!--Custom JavaScript -->
    <script src="../dist/js/custom.min.js"></script>
    <!-- this page js -->
    <script src="../assets/extra-libs/multicheck/datatable-checkbox-init.js"></script>
    <script src="../assets/extra-libs/multicheck/jquery.multicheck.js"></script>
    <script src="../assets/extra-libs/DataTables/datatables.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
   
    <script>
      var msg;
      var icon;
      
      function showAlert(msg,icon) {
          Swal.fire({
              title: msg,
              icon: icon,
              confirmButtonText: 'Close'
          });
      }
      
      $(document).ready(function(){
              $("#zero_config").DataTable();
              $('#add').click(function(e){ 
                    e.preventDefault();
                    $("#modelForm").get(0).reset();                  
                    setTimeout(function(){$("#myModal").modal('show');}, 100);
                });

              $('#save-data').click(function(){
                   var user_name = $("#user_name").val();
                   var user_id = $("#user_id").val();
                   var password = $("#password").val();
                   if($("#radio1").is(':checked')){
                       var level='Admin';
                    } else {
                       var level='Staff';
                    }   
                    
                    if((user_name.length===0)||(user_id.length===0)||(password.length===0))
                    {
                      showAlert('some field is empty!','warning');
                      return 0;
                    }                  
                         $.ajax({
                             url:"core/user.php",
                             method:"post",
                             data: {
                                 user_name:user_name,
                                 user_id:user_id,
                                 password:password,
                                 level:level,
                                 type:'add',                          
                             },
                             success: function(data){                         
                               if(data==1)
                               {
                                $("#myModal").modal('toggle');
                                showAlert('User Successfully Added!','success');
                                setTimeout(function(){window.location='./user.php';}, 2000); 
                               
                               }
                             }
                         });                
               });
               $('#edit-data').click(function(){
                   var user_name = $("#user_name1").val();
                   var user_id = $("#user_id1").val();
                   var password = $("#password1").val();
                   var id = $("#id1").val();
                    
                    if((user_name.length===0)||(user_id.length===0)||(password.length===0))
                    {
                      showAlert('some field is empty!','warning');
                      return 0;
                    }                  
                         $.ajax({
                             url:"core/user.php",
                             method:"post",
                             data: {
                                 user_name:user_name,
                                 user_id:user_id,
                                 password:password,
                                 id:id,
                                 type:'edit',                         
                             },
                             success: function(data){                         
                               if(data==1)
                               {
                                $("#myModal").modal('toggle');
                                showAlert('User Successfully Updated!','success');
                                setTimeout(function(){window.location='./user.php';}, 2000); 
                               
                               }
                             }
                         });                
               });
               $("#zero_config").on("click", "#id", function() {              
                var id = $(this).data("id");     
                if (confirm('Do you really want to Delete User List!',"")) {                   
                    $.ajax({
                                url:"core/delete.php",
                                method:"post",
                                data: {
                                   id:id,
                                   state:'usr',                          
                                },
                                success: function(data){                        
                                  if(data.length===1)
                                  {
                                    //$("#id").closest('tr').remove();
                                    showAlert('User Successfully Deleted!','success');
                                    setTimeout(function(){window.location='./user.php';}, 2000); 
                                    
                                  }
                                }
                            });
                          }     
              });
              $("#zero_config").on("click", "#edit", function() {              
                var id = $(this).data("id");                               
                $.ajax({
                                url:"core/fetch-student.php",
                                method:"post",
                                data: {
                                   id:id,
                                   state:'user_edit',                          
                                },
                                success: function(data){                        
                                  $.each(data,function(key,value){  
                                          $('#id1').val(id);
                                          $('#user_name1').val(value['user_name']);                           
                                          $('#user_id1').val(value['user_id']);                                            
                                          $("#myModal1").modal('toggle');                                      
                                        })
                                }
                            });
              });
         
        });
    
</script>

  </body>
</html>