
<?= $this->extend('Page_layout') ?>
<?= $this->section('content') ?>
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1>User Data</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
            
              <li class="breadcrumb-item active">Master User</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->
    <section class="content">

      <!-- Default box -->
      <div class="card card-primary">
        <div class="card-header">
          <h3 class="card-title">User Data</h3>

          <div class="card-tools">
            <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
              <i class="fas fa-minus"></i>
            </button>
            <button type="button" class="btn btn-tool" data-card-widget="remove" title="Remove">
              <i class="fas fa-times"></i>
            </button>
          </div>
        </div>
        <div class="card-body">


             <div class="col-12" style="overflow-x:scroll">
             <table id="table-data" style="width:1000px;" class="table table-bordered table-hover">
                  <thead>
                  <tr>
                    <th></th>
                    <th>ID</th>
                    <th>USERNAME</th>
                    <th>LEVEL</th>
                    <th>STATUS AKTIF</th>
                  </tr>
                  </thead>
                  <tbody>
                
                 
                  
                  </tbody>
                  
                </table>
                 </div>
        </div>
        <!-- /.card-body -->
        <div class="card-footer">
           <button type="submit" class="btn btn-danger" data-toggle="modal" data-target="#form-add">Add new</button>
        </div>
        <!-- /.card-footer-->
      </div>
      <!-- /.card -->

    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
  </div>
  <?php include('User_add.php');?>
  <?php include('User_edit.php');?>
  <?php include('User_delete.php');?>


   <?= $this->endSection() ?> 

  <?=$this->section("scripts")?>
<script src="<?=base_url('public/plugins/jquery-ui/jquery-ui.js')?>"></script>  
<script src="<?=base_url('public/plugins/datatables/jquery.dataTables.min.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-bs4/js/dataTables.bootstrap4.min.js')?>"></script>

<script src="<?=base_url('public/plugins/datatables-responsive/js/dataTables.responsive.min.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-responsive/js/responsive.bootstrap4.min.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-buttons/js/dataTables.buttons.min.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-buttons/js/buttons.bootstrap4.min.js')?>"></script>
<script src="<?=base_url('public/plugins/jszip/jszip.min.js')?>"></script>
<script src="<?=base_url('public/plugins/pdfmake/pdfmake.min.js')?>"></script>
<script src="<?=base_url('public/plugins/pdfmake/vfs_fonts.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-buttons/js/buttons.html5.min.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-buttons/js/buttons.print.min.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-buttons/js/buttons.colVis.min.js')?>"></script>


<script>



 function loaduser(){
   
     $.ajax({
            type: "GET",
            url: "<?= site_url('user/fetch')?>",
        
            success: function (response) {
            //  console.log(response.data);
             $.each(response.data, function (key, value) { 
              var active;
              if(value['IS_ACTIVE'] == 1){
                active = "<div class=\"bg-success  color-palette text-center\"><span>Active</span></div>";
              }else{
                active = "<div class=\"bg-secondary  color-palette text-center\"><span>Non aktif</span></div>";
              }
              var body = "<tr>";
              body += "<td><div class=\"btn-group\"><a  class=\"btn btn-info btn-sm edit-btn\">Edit</a>";
              body += "<a href=\"#\" class=\"btn btn-danger btn-sm delete-btn\">Delete</a></div></td>";
              body += "<td class=\"data-id\">"+value['USER_ID']+"</td>";
              body += "<td >"+value['USER_NAME']+"</td>";
              body += "<td>"+value['USER_LEVEL']+"</td>";
              body += "<td><div class=\"col-sm-4 col-md-10\">"+active+"</div></td>";
              body += "</tr>";
              $("#table-data tbody").append(body);
               
             });
              $("#table-data").DataTable();
            }
          });

       
  }


  $(document).ready(function () {

  loaduser();
  // show edit pop up
    $(document).on('click','.edit-btn', function () {
      var id = $(this).closest('tr').find('.data-id').text();
    $.ajax({
      type: "POST",
      url: "<?= site_url('user/edit')?>",
      data: {'id' : id},
    
      success: function (response) {
       // console.log(response.data);
       $.each(response, function (key, userval) { 
       
          $('#form-edit').find('input').val('');
          $( "#e_password" ).prop( "disabled", true );
          $( "#ex_password" ).prop( "disabled", true );
          $('#e_resetpass').prop('checked', false);
          $("#e_username").val(userval['USER_NAME']);
          $("#e_level").val(userval['USER_LEVEL']).change();
          $("#e_userid").val(id);
          if(userval['IS_ACTIVE'] == 1 ){$('#e_active').prop('checked', true);} else {$('#e_active').prop('checked', false);}
          $("#form-edit").modal("show");
       });
      }
    });

    });
    // function show delete pop up
    $(document).on('click','.delete-btn', function () {
      var id = $(this).closest('tr').find('.data-id').text();
     
    $.ajax({
      type: "POST",
      url: "<?= site_url('user/edit')?>",
      data: {'id' : id},
    
      success: function (response) {
       // console.log(response.data);
       $.each(response, function (key, userval) { 
          $("#d_userid").val(id);
          $(".d_username").text(userval['USER_NAME'] + "[" +id+ "]");
          $("#form-delete").modal("show");
       });
      }
    });

    });


    $(document).on('click','.save-user', function () {
       if( $("#username").val() == "" || $("#ipassword").val() == "" || $("#xpassword").val() == "" || $("#level").val() == "-")
       {
        // Display an info toast with no title
          toastr.error("Mohon isi form dengan lengkap !")
           return false;
       }
       else
       { // jika sudah di isi semua.
          if($("#ipassword").val() != $("#xpassword").val())
          {
             toastr.error("Password tidak sama !")
             return false;
          }
          else if($("#ipassword").val().length < 8 || $("#xpassword").val().length < 8)
          {
              toastr.error("Password minimal 8 karakter !")
             return false;
          }
          var $check = "0";
          if($('#active').is(':checked')){ $check = "1" }
          var $data = {
            'username' : $("#username").val(),
            'password' : $("#ipassword").val(),
            'level' : $("#level").val(),
            'active' : $check
          };
          $.ajax({
            type: "POST",
            url: "<?= site_url('user/simpan')?>",
            data: $data,
           
            success: function (response) {
              $('#form-add').modal('hide');
              $('#form-add').find('input').val('');

               $("#table-data tbody").html("");
               loaduser();
              toastr.success(response.status);
              
           
            }
          });
         

       }
    });
  });

  // confirm Update data
  $(document).on('click','.edit-user', function () {
       if($("#e_level").val() == "-")
       {
        // Display an info toast with no title
           toastr.error("Silakan pilih level user !")
           return false;
       }
       else
       { // jika sudah di isi semua.

        if($('#e_resetpass').is(':checked')){
          if($("#e_password").val() != $("#ex_password").val())
                {
                  toastr.error("Password tidak sama !")
                  return false;
                }
                else if($("#e_password").val().length < 8 || $("#ex_password").val().length < 8)
                {
                    toastr.error("Password minimal 8 karakter !")
                  return false;
                }
          }
          
          var $xcheck = "0";
          if($('#e_active').is(':checked')){ $xcheck = "1" }
          var $edata = {
           
            'password' : $("#e_password").val(),
            'level' : $("#e_level").val(),
            'user_id' : $("#e_userid").val(),
            'active' : $xcheck
          };
    
          $.ajax({
            type: "POST",
            url: "<?= site_url('user/update')?>",
            data: $edata,
           
            success: function (response) {
              $('#form-edit').modal('hide');
              $('#form-edit').find('input').val('');

               $("#table-data tbody").html("");
              loaduser();
              toastr.success(response.status);
              
           
            }
          });
         

       }
    });


    $(document).on('click','.delete-user', function () {
      
          var $ddata = {
            'user_id' : $("#d_userid").val(),     
          };
    
          $.ajax({
            type: "POST",
            url: "<?= site_url('user/delete')?>",
            data: $ddata,          
            success: function (response) {
              $('#form-delete').modal('hide');
              $("#table-data tbody").html("");
              loaduser();
              toastr.success(response.status);
              
           
            }
          });
    });


  $("#e_resetpass").change( function () {
    if($('#e_resetpass').is(':checked'))
    { 
      $( "#e_password" ).prop( "disabled", false );
      $( "#ex_password" ).prop( "disabled", false );
    }
    else
    {
      $( "#e_password" ).prop( "disabled", true );
      $( "#ex_password" ).prop( "disabled", true );
    }
    
  });

  
 
</script>

<?=$this->endSection()?>
