<!-- Page Heading -->

          <!-- <h1 class="h3 mb-2 text-gray-800">Tables</h1>

          <p class="mb-4">DataTables is a third party plugin that is used to generate the demo table below. For more information about DataTables, please visit the <a target="_blank" href="https://datatables.net">official DataTables documentation</a>.</p> -->

          <!-- DataTales Example -->

  

          <div class="card shadow mb-4">

            <div class="card-header py-3">

              <h6 class="m-0 font-weight-bold text-primary">Data User</h6>

            </div>

            <div class="card-body">

              <div class="row">

                    <input type="hidden" name="no_page[]" value="1">

                    <div class="form-group col-md-6">

                      <select class="form-control" id="valid">

                        <option value="semua">Semua</option>

                        <option value="0">Belum ACC</option>

                        <option value="1">Aktif</option>

                        <option value="2">Tolak</option>

                      </select>

                      <small id="emailHelp" class="form-text text-muted">Kelompok berdasarkan.</small>

                    </div>

              </div>

              <div class="table-responsive">

                <table class="table table-bordered" id="tabel_data_user" width="100%" cellspacing="0">

                  <thead>

                    <tr>

                      <th>No</th>

                      <th>Nama User</th>

                      <th>Email</th>

                      <th>Unit Misioner</th>

                      <th>Status</th>

                      <th>Oleh</th>

                      <th>Aksi</th>

                    </tr>

                  </thead>

                  <!-- <tfoot>

                    <tr>

                      <th>Name</th>

                      <th>Position</th>

                      <th>Office</th>

                      <th>Age</th>

                      <th>Start date</th>

                      <th>Salary</th>

                    </tr>

                  </tfoot> -->

                  <tbody>

                  </tbody>

                </table>

              </div>

            </div>

          </div>

<script type="text/javascript">

$(document).ready(function(){

    

    var base_url = "<?=base_url()?>";

    

    $("#valid").change(function(){

      data_user($("#valid").val())

    })

    $.extend({

      xResponse: function(url, data) {

        // local var

        var theResponse = null;

        // jQuery ajax

        $.ajax({

          url: url,

          type: 'POST',

          data: data,

          dataType: "json",

          async: false,

          success: function(respText) {

            theResponse = respText;

          }

        });

        // Return the response text

        return theResponse;

      }

    });

    data_user = function(valid){

        $('#tabel_produk').hide();

        $.ajax({ 

            url: base_url + 'user/data_user',

            type: "post",

            // data:{<?php echo $this->security->get_csrf_token_name(); ?>:'<?php echo $this->security->get_csrf_hash(); ?>'},

            data:{valid:valid},

            dataType: "json",

            async : 'false',

            success: function(result){

              // alert(JSON.stringify(result))

              var data = [];

                for ( var i=0 ; i<result.length ; i++ ) {

                  // alert(result[i].kode_pka)

                    var no = i+1;

                    

                    // var satuan;

                    // if(result[i].satuan == 1){

                    //     satuan = 'Pcs';

                    // }else{

                    //     satuan = 'Kg';

                    // }

                    // var link_edit = "<a href='javascript:void(0)' onclick=\"getpopup('aksesoris/edit','"++"','popupedit','"+result[i].id_kma+"');\"><span class='btn btn-info fa fa-edit' title='Edit Aksesoris'></span></a>";

                    

                    

                    if(result[i].valid == "0"){

                      var v = `

                        <span class="mr-2">

                          <i class="fas fa-circle text-primary"></i> Belum Direspon

                        </span>

                      `;

                      var link_hapus = "";

                      var link_acc = `<a href="javascript:void(0)" onclick="terima(`+result[i].id_user+`)" class="btn btn-success btn-icon-split btn-sm">

                                        <span class="icon text-white-50">

                                          <i class="fas fa-user-check"></i>

                                        </span>

                                        <span class="text">ACC</span>

                                      </a>`;

                      var link_tolak = `<a href="javascript:void(0)" onclick="tolak(`+result[i].id_user+`)" class="btn btn-warning btn-icon-split btn-sm">

                                        <span class="icon text-white-50">

                                          <i class="fas fa-user-times"></i>

                                        </span>

                                        <span class="text">Tolak</span>

                                      </a>`;

                    }else if(result[i].valid == "1"){

                      var v = `

                        <span class="mr-2">

                          <i class="fas fa-circle text-success"></i> Aktif

                        </span>

                      `;

                      var link_hapus = `<a href="javascript:void(0)" onclick="hapus_user(`+result[i].id_user+`)" class="btn btn-danger btn-sm">

                                          <i class="fas fa-trash-alt"></i>

                                      </a>`;

                      var link_acc ="";

                      var link_tolak ="";

                    }else{

                      var v = `

                        <span class="mr-2">

                          <i class="fas fa-circle text-warning"></i> Ditolak

                        </span>

                      `;

                      var link_hapus = `<a href="javascript:void(0)" onclick="hapus_user(`+result[i].id_user+`)" class="btn btn-danger btn-sm">

                                          <i class="fas fa-trash-alt"></i>

                                      </a>`;

                      var link_acc ="";

                      var link_tolak ="";

                    }

                    var d ='<div class="btn-group" role="group" aria-label="Basic example">';

                    var dd = '</div>';

                    var valid_oleh = $.xResponse(base_url+'user/ambil_valid_oleh', {kode_user: result[i].valid_oleh}); 

                    if (valid_oleh == null) {

                      oleh = "";

                    }else{

                      oleh =valid_oleh;

                    }

                    data.push([no,result[i].username,result[i].email,result[i].nama_um,v,oleh,d+link_hapus+" "+link_acc+" "+link_tolak+dd]);

                }

                $('#tabel_data_user').DataTable({

                    //"bJQueryUI"     : true,

                    data                : data,

                    deferRender         : true,

                    processing          : true,

                    ordering            : true,

                    retrieve            : false,

                    paging              : true,

                    deferLoading        : 57,

                    bDestroy            : true,

                    autoWidth           : false,

                    bFilter             : true,

                    iDisplayLength      : 10,

                    responsive: true,

                    language: {

                      searchPlaceholder: 'Cari',

                      sSearch: '',

                      lengthMenu: '_MENU_',

                    },

                });

               

            },

                error: function(jqXHR, textStatus, errorThrown) {

                  $('#loading').modal('hide');

                  Command: toastr["error"](errorThrown, "Error");

                },

                beforeSend: function () {

                  $('#loading').modal('show');

                },

                complete: function () {

                    $('#loading').modal('hide');

                }

        });

    }

    data_user("semua");

    tolak  = function(id_user){

      bootbox.confirm("Apakah Anda Yakin Akan Menolak User?", function(result){ 

        if (result==true) {

      $.ajax({ 

            url: base_url + 'user/tolak_user',

            type: "post",

            // data:{<?php echo $this->security->get_csrf_token_name(); ?>:'<?php echo $this->security->get_csrf_hash(); ?>'},

            data:{id_user:id_user},

            dataType: "json",

            async : 'false',

            success: function(result){

              Command: toastr["success"]("Anda Sudah Berhasil Menolak User", "Berhasil");

              getcontens('page/data_user_admin');

            },

                error: function(jqXHR, textStatus, errorThrown) {

                  $('#loading').modal('hide');

                  Command: toastr["error"](errorThrown, "Error");

                },

                beforeSend: function () {

                  $('#loading').modal('show');

                },

                complete: function () {

                    $('#loading').modal('hide');

                }

          });

        }

      });

    }

    terima  = function(id_user){

      bootbox.confirm("Apakah Anda Yakin Akan Menerima User?", function(result){ 

        if (result==true) {

      $.ajax({ 

            url: base_url + 'user/terima_user',

            type: "post",

            // data:{<?php echo $this->security->get_csrf_token_name(); ?>:'<?php echo $this->security->get_csrf_hash(); ?>'},

            data:{id_user:id_user},

            dataType: "json",

            async : 'false',

            success: function(result){

              Command: toastr["success"]("Anda Sudah Berhasil Menerima User", "Berhasil");

              getcontens('page/data_user_admin');

            },

                error: function(jqXHR, textStatus, errorThrown) {

                  $('#loading').modal('hide');

                  Command: toastr["error"](errorThrown, "Error");

                },

                beforeSend: function () {

                  $('#loading').modal('show');

                },

                complete: function () {

                    $('#loading').modal('hide');

                }

          });

        }

      });

    }

    hapus_user = function(id_user){

      bootbox.confirm("Apakah Anda Yakin Akan Menghapus User?", function(result){ 

        if (result==true) {

          $.ajax({ 

            url: base_url + 'user/hapus_user',

            type: "post",

            data:{id_user:id_user},

            dataType: "json",

            async : 'false',

            success: function(result){

              Command: toastr["success"]("Anda Sudah Berhasil Menghapus User", "Berhasil");

              getcontens('page/data_user_admin');

            },

                error: function(jqXHR, textStatus, errorThrown) {

                  $('#loading').modal('hide');

                  Command: toastr["error"](errorThrown, "Error");

                },

                beforeSend: function () {

                  $('#loading').modal('show');

                },

                complete: function () {

                    $('#loading').modal('hide');

                }

          });

        }

      })

    }

  });

</script>

