<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<!DOCTYPE html>
<html>
  <head>
    <title>Admin &gt; Blokir Nomor</title>
    <?php include_once(VIEWPATH. 'includes/head.php'); ?>
    <style type="text/css">nav{float:right;margin-bottom: 15px;}</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>Blokir Nomor</span></li>
      </ul>
      <div class="row">
        <div class="col-sm-12">
          <div class="row" id="action-links">
            <div class="col-xs-6 col-sm-4 col-md-2">
              <a id="add-btn" class="btn btn-default btn-block" style="margin-bottom: 15px;" href="<?php echo site_url('admin/modules/blokir-nomor/add');?>" data-toggle="modal" data-target="#myModal"><i class="fa fa-plus"></i> Tambah</a>
            </div>
            <div class="col-xs-6 col-sm-4 col-md-2">
              <a id="download-btn" class="btn btn-default btn-block" style="margin-bottom: 15px;" href="#"><i class="fa fa-download"></i> Download</a>
            </div>
            <div class="col-sm-4 col-md-2 hidden-xs">
              <a class="btn btn-danger btn-block" style="margin-bottom: 15px;" href="<?php echo site_url('admin/modules/blokir-nomor/delete');?>" data-toggle="modal" data-target="#myModal"><i class="fa fa-trash"></i> Kosongkan</a>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-offset-2 col-md-4">
              <form action="<?php echo site_url('admin/modules/blokir-nomor');?>" method="GET" style="margin-bottom: 15px;">
                <div class="input-group">
                  <input class="form-control" type="text" name="q" value="<?php echo html_escape($search);?>" minlength="4" maxlength="32" placeholder="Cari nomor" />
                  <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 data nomor yang diblokir.</div>
          <?php else:?>
          <?php echo form_open('admin/modules/blokir-nomor/delete','id="form-confirm"');?>
          <div class="panel panel-default">
            <div class="panel-heading">
              <div class="dropdown pull-right">
                <a href="#" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true" style="font-size: 18px;color:#333;cursor:pointer;" title="Urutkan">
                  <i class="fa fa-sort"></i>
                </a>
                <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
                  <li class="<?php echo ($sort == 1 ? 'active' : '');?>"><a href="<?php echo site_url('admin/modules/blokir-nomor?q='.urlencode($search).'&amp;sort=1');?>">Nomor</a></li>
                  <li class="<?php echo ($sort == 2 ? 'active' : '');?>"><a href="<?php echo site_url('admin/modules/blokir-nomor?q='.urlencode($search).'&amp;sort=2');?>">Terakhir Ditambahkan</a></li>
                  <li class="<?php echo ($sort == 3 ? 'active' : '');?>"><a href="<?php echo site_url('admin/modules/blokir-nomor?q='.urlencode($search).'&amp;sort=3');?>">Terakhir Order</a></li>
                </ul>
              </div>
              <h3><i class="fa fa-ban"></i> Blokir Nomor</h3>
            </div>
            <div class="table-responsive">
              <table class="table table-bordered table-hover" style="margin-bottom: 0;">
                <thead>
                  <tr>
                    <th class="text-center" style="width: 40px;"><input type="checkbox" id="select-all"/></th>
                    <th style="width: 180px;">Nomor</th>
                    <th>Alasan Pemblokiran</th>
                    <th style="width: 160px;">User</th>
                    <th style="width: 160px;">Tanggal Order</th>
                  </tr>
                </thead>
                <tbody>
                  <?php foreach ($results as $result):?>
                  <tr>
                    <td class="text-center"><input type="checkbox" class="checkbox1" name="bn_id[]" value="<?php echo $result->bn_id;?>"/></td>
                    <td style="width: 180px;"><a data-toggle="modal" data-target="#myModal" href="<?php echo site_url('admin/sms/kirim?phone='.urlencode($result->bn_nomor));?>" title="Kirim SMS"><?php echo ($search ? preg_replace("/$search/i",'<span style="color:red;font-weight:bold">${0}</span>', $result->bn_nomor) : $result->bn_nomor);?></a></td>
                    <td><?php echo $result->bn_alasan ? html_escape(substr($result->bn_alasan, 0, 60)) : '';?></td>
                    <td style="width: 160px;"><?php echo $result->us_name ? '<a href="'.site_url('admin/users/profil/'.$result->us_id).'" data-toggle="modal" data-target="#myModal">'.html_escape($result->us_name).'</a>' : '';?></td>
                    <td style="width: 160px;"><span style="white-space: nowrap;"><?php echo $result->bn_tgl_order ? format_tanggal($result->bn_tgl_order).' ('.$result->bn_total_order.')' : '';?></span></td>
                  </tr>
                  <?php endforeach;?>
                </tbody>
              </table>
            </div>
          </div>
          <div class="row">
            <div class="col-sm-4 col-md-3 col-lg-2">
              <button class="btn btn-danger btn-block btn-sm" type="submit" name="delete" value="1" style="margin-bottom: 15px;"><i class="fa fa-trash"></i> Hapus yang ditandai</button>
            </div>
            <div class="col-sm-8 col-md-9 col-lg-10"><?php echo pagination(site_url('admin/modules/blokir-nomor/index'),$start,$total,$this->system->set['list_per_page'],'/%d?q='.urlencode($search).'&amp;sort='.$sort);?></div>
          </div>
          <?php endif;?>
        </div>
      </div>
    </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('bn_id')>0){$("#myModal").modal("show");$.ajax({type:"POST",url:$(this).attr("action"),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;});
    var errors = 0;
    function download(page) {
      $.get("<?php echo site_url('admin/modules/blokir-nomor/download/ajax');?>/"+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(data.next_page);
          }, 500);
        } else {
          setTimeout(function() {
            $("#action-links").show();
            $("#progress").hide();
            window.location = "<?php echo site_url('admin/modules/blokir-nomor/download');?>";
          }, 1000);
        }
      }).fail(function() {
        if (errors >= 5) {
          $("#action-links").show();
          $("#progress").hide();
          alert("Terjadi kesalahan!");
        } else {
          errors += 1;
          setTimeout(function() {
            download(page);
          }, 500);
        }
      });
    }
    $("#download-btn").click(function() {
      $("#action-links").hide();
      $(".progress-bar").attr("aria-valuenow", "0").css("width", "0%").text("0%");
      $("#progress").show();
      download(1);
      return false;
    });
    <?php if ($upload):?>
    function upload(saved, reason, page) {
      $.get("<?php echo site_url('admin/modules/blokir-nomor/upload/ajax');?>/"+saved+"/"+reason+"/"+page, function(data) {
        $(".progress-bar").attr("aria-valuenow", data.progress).css("width", data.progress+"%").text(data.progress+"%");
        if (data.next_page > 0) {
          setTimeout(function() {
            upload(saved + data.saved, reason, data.next_page);
          }, 500);
        } else {
          $("#action-links").show();
          $("#progress").hide();
          setTimeout(function() {
            window.location = "<?php echo site_url('admin/modules/blokir-nomor');?>?saved="+(saved + data.saved);
          }, 1000);
        }
      }).fail(function() {
        $("#action-links").show();
        $("#progress").hide();
        alert("Terjadi kesalahan!");
      });
    }
    $("#action-links").hide();
    $("#progress").show();
    upload(0, '<?php echo $reason;?>', 1);
    <?php endif;?>
    </script>
  </body>
</html>