<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<!DOCTYPE html>
<html>
  <head>
    <title>Daftar Member</title>
    <?php include_once(VIEWPATH. 'includes/head.php'); ?>
    <style type="text/css">
    .popover-content>.list-group{margin: -9px -15px;border-radius:0;}
    .popover-content>.list-group>.list-group-item{padding:5px 8px;border-left:0;border-right:0;}
    .popover-content>.list-group>.list-group-item:first-child{border-top:0;border-top-left-radius:0;border-top-right-radius:0;}
    .popover-content>.list-group>.list-group-item:last-child{border-bottom:0;}
    nav{margin-bottom: 15px;float: right;}
    </style>
  </head>
  <body>
    <?php include_once(VIEWPATH. 'admin/_navbar.php'); ?>
    <div class="container">
      <ul class="breadcrumb">
        <li><a href="<?php echo site_url();?>"><i class="fa fa-home"></i></a></li>
        <li><a href="<?php echo site_url('admin');?>">Admin Panel</a></li>
        <li class="active"><span>Member</span></li>
      </ul>
      <div id="menu-btn" class="row">
        <div class="col-xs-6 col-sm-3 col-lg-2" style="margin-bottom: 15px;">
          <a class="btn btn-default btn-block" href="<?php echo site_url('admin/users/add');?>" data-toggle="modal" data-target="#myModal"><i class="fa fa-user-plus"></i> Menambahkan</a>
        </div>
        <div class="col-xs-6 col-sm-3 col-lg-2" style="margin-bottom: 15px;">
          <a class="btn btn-default btn-block" href="<?php echo site_url('admin/users/download');?>" data-toggle="modal" data-target="#myModal"><i class="fa fa-download"></i> Download</a>
        </div>
        <div class="col-xs-12 col-sm-3 col-lg-3" style="margin-bottom: 15px;">
          <div class="input-group">
            <span class="input-group-addon" data-toggle="tooltip" data-title="Filter"><i class="fa fa-filter"></i></span>
            <select class="form-control" onchange="window.location.href=this.value">
              <option value="<?php echo site_url('admin/users');?>">-- Filter --</option>
              <optgroup label="Jenis Kelamin">
                <option value="<?php echo site_url('admin/users?filter=gender&amp;data=male');?>"<?php echo ($filter == 'gender' && $data == 'male' ? ' selected="selected"' : '');?>>Laki-laki</option>
                <option value="<?php echo site_url('admin/users?filter=gender&amp;data=female');?>"<?php echo ($filter == 'gender' && $data == 'female' ? ' selected="selected"' : '');?>>Perempuan</option>
              </optgroup>
              <optgroup label="Saldo">
                <option value="<?php echo site_url('admin/users?filter=balance&amp;data=terbanyak');?>"<?php echo ($filter == 'balance' && $data == 'terbanyak' ? ' selected="selected"' : '');?>>Terbanyak</option>
                <option value="<?php echo site_url('admin/users?filter=balance&amp;data=terendah');?>"<?php echo ($filter == 'balance' && $data == 'terendah' ? ' selected="selected"' : '');?>>Terendah</option>
              </optgroup>
              <optgroup label="Reseller">
                <option value="<?php echo site_url('admin/users?filter=reseller&amp;data=active');?>"<?php echo ($filter == 'reseller' && $data == 'active' ? ' selected="selected"' : '');?>>Aktif</option>
                <option value="<?php echo site_url('admin/users?filter=reseller&amp;data=inactive');?>"<?php echo ($filter == 'reseller' && $data == 'inactive' ? ' selected="selected"' : '');?>>Tidak aktif</option>
                <option value="<?php echo site_url('admin/users?filter=reseller&amp;data=blocked');?>"<?php echo ($filter == 'reseller' && $data == 'blocked' ? ' selected="selected"' : '');?>>Diblokir</option>
              </optgroup>
              <optgroup label="Pemblokiran">
                <option value="<?php echo site_url('admin/users?filter=block&amp;data=blocked');?>"<?php echo ($filter == 'block' && $data == 'blocked' ? ' selected="selected"' : '');?>>Diblokir</option>
                <option value="<?php echo site_url('admin/users?filter=block&amp;data=unblocked');?>"<?php echo ($filter == 'block' && $data == 'unblocked' ? ' selected="selected"' : '');?>>Tidak diblokir</option>
              </optgroup>
              <optgroup label="Waktu Pendaftaran">
                <option value="<?php echo site_url('admin/users?filter=regdate&amp;data=today');?>"<?php echo ($filter == 'regdate' && $data == 'today' ? ' selected="selected"' : '');?>>Hari ini</option>
                <option value="<?php echo site_url('admin/users?filter=regdate&amp;data=week');?>"<?php echo ($filter == 'regdate' && $data == 'week' ? ' selected="selected"' : '');?>>Minggu ini</option>
                <option value="<?php echo site_url('admin/users?filter=regdate&amp;data=month');?>"<?php echo ($filter == 'regdate' && $data == 'month' ? ' selected="selected"' : '');?>>Bulan ini</option>
              </optgroup>
              <optgroup label="Verifikasi">
                <option value="<?php echo site_url('admin/users?filter=verified&amp;data=verified');?>"<?php echo ($filter == 'verified' && $data == 'verified' ? ' selected="selected"' : '');?>>Terverifikasi</option>
                <option value="<?php echo site_url('admin/users?filter=verified&amp;data=pending');?>"<?php echo ($filter == 'verified' && $data == 'pending' ? ' selected="selected"' : '');?>>Menunggu Verifikasi</option>
                <option value="<?php echo site_url('admin/users?filter=verified&amp;data=email');?>"<?php echo ($filter == 'verified' && $data == 'email' ? ' selected="selected"' : '');?>>Email Terverifikasi</option>
                <option value="<?php echo site_url('admin/users?filter=verified&amp;data=phone');?>"<?php echo ($filter == 'verified' && $data == 'phone' ? ' selected="selected"' : '');?>>No. HP Terverifikasi</option>
                <option value="<?php echo site_url('admin/users?filter=unverified&amp;data=email');?>"<?php echo ($filter == 'unverified' && $data == 'email' ? ' selected="selected"' : '');?>>Email Belum Terverifikasi</option>
                <option value="<?php echo site_url('admin/users?filter=unverified&amp;data=phone');?>"<?php echo ($filter == 'unverified' && $data == 'phone' ? ' selected="selected"' : '');?>>No. HP Belum Terverifikasi</option>
              </optgroup>
              <optgroup label="Lainnya">
                <option value="<?php echo site_url('admin/transaksi/top-users');?>">Transaksi Terbanyak</option>
                <option value="<?php echo site_url('admin/users?filter=lastdate');?>"<?php echo ($filter == 'lastdate' ? ' selected="selected"' : '');?>>Terakhir Masuk</option>
                <option value="<?php echo site_url('admin/users?filter=admin&amp;data=10');?>"<?php echo ($filter == 'admin' && $data == '10' ? ' selected="selected"' : '');?>>Admin Utama</option>
              </optgroup>
            </select>
          </div>
        </div>
        <div class="col-xs-12 col-sm-3 col-lg-offset-2 col-lg-3" style="margin-bottom: 15px;">
          <form method="get" action="<?php echo site_url('admin/users');?>">
            <div class="input-group">
              <input type="text" class="form-control" name="search" value="<?php echo $search;?>" placeholder="Cari pengguna..." data-toggle="tooltip" data-title="Cari nama pengguna, nama lengkap, email atau nomor handphone."/>
              <span class="input-group-btn"><button class="btn btn-primary" type="submit"><i class="fa fa-search"></i></button></span>
            </div>
          </form>
        </div>
      </div>
      <div class="progress" id="progress" style="display: none;">
        <div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%">0%</div>
      </div>
      <?php if (!$total):?>
        <div class="alert alert-warning">Tidak ada pengguna</div>
      <?php else:?>
      <?php echo form_open(current_url(),'id="form-confirm"');?>
      <div class="panel panel-default">
        <div class="panel-heading">
          <h3><?php echo ($filter == 'lastdate' ? '<span class="pull-right"><i class="fa fa-refresh fa-spin"></i></span>' : '');?><i class="fa fa-users"></i> Member</h3>
        </div>
        <div class="table-responsive">
          <table class="table table-striped">
            <thead>
              <tr>
                <?php if ($filter != 'lastdate'):?><th class="text-center" style="width: 40px;"><input type="checkbox" id="select-all"/></th><?php endif;?>
                <th>ID</th>
                <th>Username</th>
                <th>Nama</th>
                <th>Email</th>
                <th>Nomor HP</th>
                <th>Saldo</th>
                <th style="width: 120px;">Tgl Mendaftar</th>
                <?php if ($filter == 'lastdate'):?>
                <th style="width: 120px;">Terakhir Masuk</th>
                <?php else:?>
                <th class="text-center">Tindakan</th>
                <?php endif;?>
              </tr>
            </thead>
            <tbody>
              <?php foreach ($results as $user):?>
              <tr id="user-<?php echo $user->us_id;?>">
                <?php if ($filter != 'lastdate'):?>
                <?php if ($user->us_rights == '10'):?>
                <td class="text-center"><input type="checkbox" disabled="disabled"/></td>
                <?php else:?>
                <td class="text-center"><input type="checkbox" class="checkbox1" name="users[]" value="<?php echo $user->us_id;?>"/></td>
                <?php endif;?>
                <?php endif;?>
                <td style="vertical-align: middle;"><?php echo $user->us_id;?></td>
                <td style="vertical-align: middle; white-space: nowrap;">
                  <?php if ($user->us_lastdate > (time() - 300)):?>
                  <i class="fa fa-eye" title="Terakhir masuk <?php echo format_tanggal($user->us_lastdate);?>"></i>&nbsp;
                  <?php endif;?>
                  <?php if ($user->us_block):?>
                  <span class="text-danger"><i class="fa fa-ban" title="Diblokir"></i></span>&nbsp;
                  <?php else:?>
                  <?php if ($user->us_rights == 10):?>
                  <i class="fa fa-shield" title="Administartor"></i>&nbsp;
                  <?php else:?>
                  <?php echo $user->us_verified ? '<span class="text-success" title="Terverifikasi"><i class="fa fa-check-circle"></i></span>'  : '<!-- <span data-toggle="tooltip" data-title="Belum Terverifikasi"><i class="fa fa-check-circle"></i></span> -->';?>
                  <?php endif;?>
                  <?php endif;?>
                  <a href="<?php echo site_url('admin/users/profil/'.$user->us_id);?>"><?php
                  if ($search) {
                    echo preg_replace("/$search/i",'<span style="color:red;font-weight:bold">${0}</span>', $user->us_username) . PHP_EOL;
                  } else {
                    echo $user->us_username;
                  }
                  ?></a>
                </td>
                <td style="vertical-align: middle;"><?php echo ($search ? preg_replace("/$search/i",'<span style="color:red;font-weight:bold">${0}</span>', $user->us_name) : $user->us_name);?></td>
                <td style="vertical-align: middle;"><?php echo ($search ? preg_replace("/$search/i",'<span style="color:red;font-weight:bold">${0}</span>', $user->us_email) : $user->us_email);?></td>
                <td style="vertical-align: middle;"><a data-toggle="modal" data-target="#myModal" href="<?php echo site_url('admin/sms/kirim?phone=' . $user->us_phone);?>"><?php echo ($search ? preg_replace("/$search/i",'<span style="color:red;font-weight:bold">${0}</span>', $user->us_phone) : $user->us_phone);?></a></td>
                <td style="vertical-align: middle;"><?php echo format_uang($user->us_balance);?></td>
                <td style="vertical-align: middle;width: 120px;white-space: nowrap;"><?php echo format_tanggal($user->us_regdate);?></td>
                <?php if ($filter == 'lastdate'):?>
                <td style="vertical-align: middle;width: 120px;white-space: nowrap;""><?php echo format_tanggal($user->us_lastdate);?></td>
                <?php else:?>
                <td style="vertical-align: middle;" class="text-center">
                  <?php if ($this->user->data['us_id'] != $user->us_id):?>
                  <?php
                    $link = '<a class="list-group-item" href="'.site_url('admin/users/login-as/'.$user->us_id).'" data-toggle="modal" data-target="#myModal"><i class="fa fa-sign-in"></i> Login as '.html_escape($user->us_name).'</a>';
                    $link .= '<a class="list-group-item" href="'.site_url('admin/users/kirim-notifikasi/'.$user->us_id).'" data-toggle="modal" data-target="#myModal"><i class="fa fa-bell"></i> Kirim pemberitahuan</a>';
                    $link .= '<a class="list-group-item" href="'.site_url('admin/users/edit/'.$user->us_id).'"><i class="fa fa-edit"></i> Edit akun</a>';
                    $link .= '<a class="list-group-item" href="'.site_url('admin/users/ubah-sandi/'.$user->us_id).'" data-toggle="modal" data-target="#myModal"><i class="fa fa-lock"></i> Ubah kata sandi</a>';
                    $link .= '<a class="list-group-item" href="'.site_url('admin/transaksi?username='.$user->us_username).'"><i class="fa fa-history"></i> Riwayat transaksi</a>';
                    $link .= '<a class="list-group-item" href="'.site_url('admin/riwayat-deposit/index/'.$user->us_username).'"><i class="fa fa-money"></i> Riwayat deposit</a>';
                    $link .= '<a class="list-group-item" href="'.site_url('admin/riwayat-saldo/index/'.$user->us_username).'"><i class="fa fa-credit-card"></i> Mutasi saldo</a>';
                    $link .= '<a class="list-group-item" href="'.site_url('admin/users/edit-saldo/'.$user->us_id).'"><i class="fa fa-money"></i> Edit saldo</a>';
                    if ($user->us_block)
                      $link .= '<a class="list-group-item" href="'.site_url('admin/users/unblock/'.$user->us_id).'" data-toggle="modal" data-target="#myModal"><i class="fa fa-ban"></i> Lepas pemblokiran</a>';
                    else
                      $link .= '<a class="list-group-item" href="'.site_url('admin/users/block/'.$user->us_id).'" data-toggle="modal" data-target="#myModal"><i class="fa fa-ban"></i> Blokir</a>';
                    $link .= '<a class="list-group-item" href="'.site_url('admin/users/delete/'.$user->us_id).'" data-toggle="modal" data-target="#myModal"><i class="fa fa-user-times"></i> Hapus</a>';
                  ?>
                  <button type="button" class="btn btn-default btn-xs" data-container="body" data-toggle="popover" data-placement="left" data-html="true" data-content="<?php echo htmlentities('<div class="list-group">'.$link.'</div>');?>" data-title="<?php echo html_escape($user->us_name);?>" data-trigger="focus"><i class="fa fa-chevron-down"></i></button>
                  <?php endif;?>
                </td>
                <?php endif;?>
              </tr>
              <?php endforeach;?>
            </tbody>
          </table>
        </div>
      </div>
      <?php if ($filter != 'lastdate'):?>
      <div class="row">
        <div class="col-sm-4 col-md-3">
          <div class="input-group" style="margin-bottom: 15px;">
            <select class="form-control input-sm" name="tindakan">
              <option value="">-- Yang ditandai --</option>
              <option value="delete">Hapus</option>
              <option value="block">Blokir</option>
              <option value="unblock">Lepas pemblokiran</option>
            </select>
            <span class="input-group-btn"><button class="btn btn-primary btn-sm" type="submit">GO</button></span>
          </div>
        </div>
        <div class="col-sm-8 col-md-9"><?php echo pagination('', $start, $total, $this->system->set['list_per_page'], site_url('admin/users?filter='.html_escape(strtr($filter, array('%' => '%%'))).'&amp;data='.html_escape(strtr($data, array('%' => '%%'))).'&amp;page=%d'));?></div>
      </div>
      <?php endif;?>
      <?php echo form_close();?>
      <?php endif;?>
    </div>
    <?php include_once(VIEWPATH.'includes/foot.php'); ?>
    <script type="text/javascript">
    $(document).ready(function(){$('#select-all').click(function(event){if(this.checked){$('.checkbox1').each(function(){this.checked=true;})}else{$('.checkbox1').each(function(){this.checked=false;})}});});
    $(document).on("submit","#form-confirm", function() {
      var formData = $(this).serialize();
      if (formData.indexOf('users') > 0) {
        var tindakan = $('select[name="tindakan"] option:selected').val();
        if (tindakan != ''){
          var url;
          if (tindakan == 'delete')
            url = "<?php echo site_url('admin/users/delete');?>";
          else if (tindakan == 'block')
            url = "<?php echo site_url('admin/users/block');?>";
          else if (tindakan == 'unblock')
            url = "<?php echo site_url('admin/users/unblock');?>";
          $("#myModal").modal("show");
          $.ajax({
            type: "POST",
            url: url,
            data: formData,
            success: function (data) {
              var csrf = $(data).find('[name="<?php echo $this->security->get_csrf_token_name();?>"]').val();
              $('#form-confirm [name="<?php echo $this->security->get_csrf_token_name();?>"]').val(csrf);
              $("#myModal .modal-content").html(data);
            },
            error: function() {
              alert("Terjadi kesalahan!");
            }
          });
        }
      }
      return false;
    });
    <?php if ($dl_userdata):?>
    var errors = 0;
    function download_kontak(type, email, phone, blocked, page) {
      $.get("<?php echo site_url('admin/users/download');?>/"+type+"/ajax?email_verified="+email+"&phone_verified="+phone+"&blocked="+blocked+"&page="+page, function(data) {
        $(".progress-bar").attr("aria-valuenow", data.progress).css("width", data.progress+"%").text(data.progress+"%");
        if (data.next_page > 0) {
          setTimeout(function() {
            download_kontak(type, email, phone, blocked, data.next_page);
          }, 500);
        } else {
          $("#menu-btn").show();
          $("#progress").hide();
          setTimeout(function() {
            window.location = "<?php echo site_url('admin/users/download');?>/"+type;
          }, 1000);
        }
      }).fail(function() {
        if (errors >= 5) {
          $("#menu-btn").show();
          $("#progress").hide();
          alert("Terjadi kesalahan!");
        } else {
            errors += 1;
            setTimeout(function() {
            download_kontak(type, email, phone, blocked, page)
          }, 1000);
        }
      });
    }
    $("#menu-btn").hide();
    $("#progress").show();
    download_kontak('<?php echo $dl_userdata[0];?>',<?php echo $dl_userdata[1];?>, <?php echo $dl_userdata[2];?>, <?php echo $dl_userdata[3];?>, 1);
    <?php endif;?>
    <?php if ($filter == 'lastdate'):?>
    var lastID = "";
    setInterval(function(){
      $.get("<?php echo site_url('admin/users?filter=lastdate');?>", function(data) {
        var tbody = $(data).find('tbody');
        var NewLastID = tbody.find("tr:first-child").find("td:last-child").text();
        if (lastID != NewLastID) {
          lastID = NewLastID;
          $("tbody").html(tbody.html());
        }
      });
    }, 5000);
    <?php endif;?>
    </script>
  </body>
</html>