<link href="<?=base_admin();?>assets/plugins/chosen/chosen.min.css" rel="stylesheet" type="text/css" />
<script src="<?=base_admin();?>assets/plugins/chosen/chosen.jquery.min.js" type="text/javascript"></script>


          <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1>
                        Manage Data Wilayah
                    </h1>
                        <ol class="breadcrumb">
                        <li><a href="<?=base_index();?>"><i class="fa fa-dashboard"></i> Home</a></li>
                        <li><a href="<?=base_index();?>data-wilayah">Data Wilayah</a></li>
                        <li class="active">Data Wilayah List</li>
                    </ol>
                </section>

                <!-- Main content -->
                <section class="content">
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="box">
                                <div class="box-body table-responsive">
  <button class="btn btn-primary" id="down_kelas"><i class="fa fa-cloud-download"></i> Download Update Data Kecamatan</button>
  <div class="row" id="show_progress" style="display: none">
   <div class="col-md-11">
    Mohon Tunggu sedang Download Data <img src="<?=base_admin();?>assets/dist/img/squares.gif">
    </div>
               <div class="col-md-11">
                        <div class="progress">
                          <div class="progress-bar progress-bar-striped" id="progressbar" role="progressbar"  aria-valuemin="0" aria-valuemax="100" style="width: 10%">
                            10%
                          </div>

                        </div></div><div class='col-md-1' id="message"><span class='current-count'>1</span>/<span class="total-count">13</span></div></div>
<p>
<div class="box box-danger">
            <div class="box-header with-border">
              <h3 class="box-title">Filter</h3>
            </div>
            <div class="box-body">
           <form class="form-horizontal" method="post" action="<?=base_admin();?>modul/data_wilayah/download_data.php">
                      <div class="form-group">
                        <label for="Semester" class="control-label col-lg-2">Provinsi</label>
                        <div class="col-lg-3">
                        <select id="provinsi" name="provinsi" data-placeholder="Pilih Provinsi ..." class="form-control chzn-select" tabindex="2">
                        <option value="all">Semua</option>
               <?php foreach ($db->fetch_custom("select id_wil,nm_wil from data_wilayah where id_level_wil='1'") as $isi) {
                  echo "<option value='$isi->id_wil'>$isi->nm_wil</option>";
               } ?>
              </select>

 </div>
                      </div><!-- /.form-group -->
                           <div class="form-group">
                        <label for="Semester" class="control-label col-lg-2">Kabupaten</label>
                        <div class="col-lg-3">
                        <select id="kabupaten" name="kabupaten" data-placeholder="Pilih Kabupaten ..." class="form-control chzn-select" tabindex="2">
                        <option value="all">Semua</option>
              </select>

 </div>
                      </div><!-- /.form-group -->

                      <div class="form-group">
                        <label for="tags" class="control-label col-lg-2">&nbsp;</label>
                        <div class="col-lg-10">
                          <span id="filter" class="btn btn-primary"><i class="fa fa-refresh"></i> Filter</span>
                          <button type="submit" class="btn btn-success"><i class="fa fa-cloud-download"></i> Download
                        </div>
                      </div><!-- /.form-group -->
                    </form>
            </div>
            <!-- /.box-body -->
          </div>
                                    <table id="dtb_data_wilayah" class="table table-bordered table-striped">
                                   <thead>
                                     <tr>

                          
                          <th>Provinsi</th>
                          <th>Kabupaten</th>
                          <th>ID Kab</th>
													<th>Kecamatan</th>
                          <th>ID Kecamatan</th>
                         
                        </tr>
                                      </thead>
                                        <tbody>
                                        </tbody>
                                    </table>
                                </div><!-- /.box-body -->
                            </div><!-- /.box -->
                        </div>
                    </div>
                </section><!-- /.content -->
        <script type="text/javascript">
$(document).ready(function() {
 $("#provinsi").chosen();

$('#provinsi').on('change', function() {

 $("#kabupaten").chosen();
    $.ajax({
      url : "<?=base_admin();?>modul/data_wilayah/get_kabupaten.php",
      type: "post",
      data : {provinsi: $(this).val()},
      success:function(data) {
       // console.log(data);
        $("#kabupaten").html(data);
        $("#kabupaten").trigger('chosen:updated');

      }
    })
 });


});



var dataTable = $("#dtb_data_wilayah").dataTable({

           'bProcessing': true,
            'bServerSide': true,
    
            'ajax':{
              url :'<?=base_admin();?>modul/data_wilayah/data_wilayah_data.php',
            type: 'post',  // method  , by default get
          error: function (xhr, error, thrown) {
              console.log(xhr);

            }
          },
          "language": {
             searchPlaceholder: "Cari Nama Kecamatan",
          "processing": "<i class=\"fa fa-spinner fa-spin\"></i> Loading data, please wait..." //add a loading image,simply putting <img src="loader.gif" /> tag.
          },
        });

$('#filter').on('click', function() {
  dataTable.fnDestroy();
 $("#dtb_data_wilayah").DataTable({

           'bProcessing': true,
            'bServerSide': true,
    
            'ajax':{
              url :'<?=base_admin();?>modul/data_wilayah/data_wilayah_data.php',
            type: 'post',  // method  , by default get
            data : {provinsi : $("#provinsi").val(),kabupaten:$("#kabupaten").val()},
          error: function (xhr, error, thrown) {
              console.log(xhr);

            }
          },
          "language": {
             searchPlaceholder: "Cari Nama Kecamatan",
          "processing": "<i class=\"fa fa-spinner fa-spin\"></i> Loading data, please wait..." //add a loading image,simply putting <img src="loader.gif" /> tag.
          },

        });

      });

  
$(document).ready(function() {


$("#down_kelas").click(function(){

  //alert('tes');
          /*$(this).attr("disabled", true);
          var start_time = new Date().getTime();
          $("#show_progress").show();*/
          total();
          /*
          total_data = parseInt(total());

          var bagi = Math.ceil(total_data/100);
          
          getData(bagi,total_data,start_time);*/
          //$("#loadnya").hide();
   
});


$("#ok_info_download").click(function(){
      location.reload();
  });


});


function millisToMinutesAndSeconds(s) {
  var ms = s % 1000;
  s = (s - ms) / 1000;
  var secs = s % 60;
  s = (s - secs) / 60;
  var mins = s % 60;
  var hrs = (s - mins) / 60;

  return (hrs < 1 ? '' : hrs+' Jam : ') + (mins < 1 ? '' : mins+' Menit : ') + secs + ' detik';
}

 function proses(percent){
   $("#progressbar").css("width",percent+"%");
      $("#progressbar").html(percent+"%");
/*    if(percent>10){
      
     
      } else {
        $("#progressbar").css("width",10+"%");
        $("#progressbar").html(10+"%");
      }*/
    } 

function total() {
          $("#show_progress").show();
          $("#down_kelas").attr("disabled", true);
          var start_time = new Date().getTime();
          var totaldata;
             $.ajax({
              url: "<?=base_admin();?>modul/data_wilayah/get_jumlah.php",
              type : "post",
              dataType: 'json',
              //'async':false,
              success: function(data) {
                 totaldata = data.jumlah;
                  total_data = parseInt(totaldata);
                  var bagi = Math.ceil(total_data/300);
                    
                    getData(bagi,total_data,start_time);
              }
            });
}



   
    var counters = 0;
    var persen = 0;
    var progress=300;
    var all_id = "";
    var error_msg = "";
    var token = "";
    var last = "";

    var data_recs = [];

   
   
    
   
    
window.getData=function(bagi,total_data,start_time)
{
    var start = start_time;


     
 
    if ((bagi*300)==progress) {
      data = {
          error_msg : error_msg,
          total_data : total_data,
          offset : counters,
          token : token,
          last : 'yes'
          }
    } else {
      data = {
            total_data : total_data,
            offset : counters,
            token : token,
            //data_rec : data_rec,
            last : 'no'
          }
    }




    $.ajax({
        /* The whisperingforest.org URL is not longer valid, I found a new one that is similar... */
        url: "<?=base_admin();?>modul/data_wilayah/get_latest.php",
        //async:false,
        data : data,
        type : "post",
        dataType: 'json',
        success:function(data){
          $.each(data, function(index) {
            persen = ((progress/total_data)*100).toFixed(1);
            if (persen>100) {
              persen=100+ "%";
              progress = total_data;
            } else {
              persen=persen+ "%";
              progress = progress;
            }

            //data_rec.push(data[index].data_rec);
           
            $(".current-count").html(progress);
            $(".total-count").html(total_data);
            persen = parseInt(persen);
            proses(persen);
              
              counters+=300;
              progress+=300;
              token = data[index].token;

              //console.log(data[index].offset);
               if (counters < total_data) {
                  getData(bagi,total_data,start);
                } else {
                 $("#loadnya").hide();
                  var end_time = new Date().getTime();
                  waktu = "Total Waktu Generate : "+millisToMinutesAndSeconds(end_time-start);
                  alert('Generate File Download Selesai');
                  $("#isi_informasi_download").html(data[index].last_notif.concat(waktu));
                  $('#informasi_download').modal('show');
                  //console.log('done');
                } 
              });

        },
      error: function (xhr, ajaxOptions, thrownError) {
        alert('oops ada error');
        $("#loadnya").hide();
         $("#ada_error").show();
        $(".isi_error").html(xhr.responseText);
        
        }

    });
}
</script>