                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1>
                        Download Prestasi 
                    </h1>
                        <ol class="breadcrumb">
                        <li><a href="<?=base_index();?>"><i class="fa fa-dashboard"></i> Home</a></li>
                        <li><a href="<?=base_index();?>download-mahasiswa/prestasi">Download Prestasi Mahasiswa Feeder</a></li>
                        <li class="active">Download Prestasi Mahasiswa Feeder</li>
                    </ol>
                </section>

                <!-- Main content -->
                <section class="content">
<div class="row">
        <div class="col-md-3">
          <div class="box box-solid">
            <div class="box-header with-border">
              <h3 class="box-title">Download Data </h3>

              <div class="box-tools">
                <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
                </button>
              </div>
            </div>

            <div class="box-body no-padding">
              <ul class="nav nav-pills nav-stacked">
                <li class="<?=($path_act=='mahasiswa')?'active':($path_act=='')?'active':'';?>"><a href="<?=base_index();?>download-mahasiswa/mahasiswa"><i class="fa fa-users"></i> Mahasiswa</a></li>
                <li class="<?=($path_act=='prestasi')?'active':'';?>"><a href="<?=base_index();?>download-mahasiswa/prestasi"><i class="fa fa-trophy"></i> Prestasi</a></li>
                <li class="<?=($path_act=='transkrip')?'active':'';?>"><a href="<?=base_index();?>download-mahasiswa/transkrip"><i class="fa fa-book"></i> Transkrip</a></li>
              </ul>
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->
        </div>
        <!-- /.col -->
        <div class="col-md-9">
          <div class="box box-primary">
            <div class="box-header with-border">
             <!--  <h3 class="box-title">Filter</h3> -->
            </div>
            <!-- /.box-header -->
            <div class="box-body no-padding">
           <form class="form-horizontal" onsubmit="return false">
<!--             <div class="form-group">
                        <label for="Semester" class="control-label col-lg-2">NIM</label>
                        <div class="col-lg-2" >
                        <select id="nim" name="nim" data-placeholder="Pilih Angkatan ..." class="form-control chzn-select" tabindex="2" style="z-index: 1" required="">
                        <option value="all">Semua</option>
                        <option value="nim">NIM</option>
              </select>
              <div id="error_sem"></div>
            </div>
            <div class="col-lg-3" style="padding-left: 0;">
              <input type="text" name="value_nim" id="value_nim" class="form-control" placeholder="Input NIM Mahasiswa" style="display: none" required="">
            </div>
                      </div> -->
                      <div class="form-group">
                      
                        <div class="col-lg-10" style="margin-left: 10px">
                        <button class="btn btn-primary" id="down_kelas"><i class="fa fa-cloud-download"></i> Download Data Prestasi</button>
                        </div>
                      </div><!-- /.form-group -->
                    </form>
              <!-- /.mail-box-messages -->
            </div>
            <!-- /.box-body -->
            <div class="box-footer no-padding">
              
            </div>
          </div>
          <!-- /. box -->
        </div>
        <!-- /.col -->
      </div>

                    <div class="row">
                        <div class="col-xs-12">
                            <div class="box">
                             
                                <div class="box-body table-responsive" style="overflow:visible;">
       

  <div class="row" id="show_progress" style="display: none">
   <div class="col-md-11">
    Mohon Tunggu sedang Generate File <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>
                      <div class="alert alert-danger alert-dismissible" id="ada_error" style="display: none">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                <h4><i class="icon fa fa-ban"></i> Error! , Contact saya ke wildannudin@gmail.com, screenshoot, kasih tahu saya error dibawah</h4><span class="isi_error"></span>
              </div>
                                </div><!-- /.box-body -->
                            </div><!-- /.box -->
                        </div>
                    </div>
 
                </section><!-- /.content -->

<script type="text/javascript">
  
$(document).ready(function() {
  $("#nim").change(function(){
    if (this.value=='nim') {
      $("#value_nim").show();
    } else {
      $("#value_nim").hide();
    }

  });

$("#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(){
   var currentBtn = $(this);
   uri = currentBtn.attr('data-uri');
   file_data = currentBtn.attr('data-file');

   //alert(uri);
        $.ajax({
            type: "POST",
            data: {uri:uri,file_data:file_data},
            url: "<?=base_admin();?>modul/download_mahasiswa/action.php?act=delete_download",
            success: function(data){
              console.log(data);
              window.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() {
        if ($("#value_nim").is(":visible") && $("#value_nim").val()=='') {
          return;
        } 
          $("#show_progress").show();
          //$("#down_kelas").attr("disabled", true);
          var start_time = new Date().getTime();
          var totaldata;
          var datas = {
             nim : $("#nim").val(),
             value_nim : $("#value_nim").val()
        }
             $.ajax({
              url: "<?=base_admin();?>modul/download_mahasiswa/download_prestasi/get_jumlah.php",
              type : "post",
              dataType: 'json',
              //'async':false,
              data : datas,
              success: function(data) {
                 totaldata = data.jumlah;
                  total_data = parseInt(totaldata);
                  var bagi = Math.ceil(total_data/50);
                    
                    getData(bagi,total_data,start_time);
              }
            });
}



   var random_number = Math.floor(1000 + Math.random() * 9000);
    var counters = 0;
    var persen = 0;
    var progress=50;
    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*50)==progress) {
      data = {
           nim : $("#nim").val(),
             value_nim : $("#value_nim").val(),
          id_data : all_id,
          error_msg : error_msg,
          total_data : total_data,
          offset : counters,
          random_number : random_number,
          token : token,
          data_rec : data_recs,
          last : 'yes'
          }
    } else {
      data = {
           nim : $("#nim").val(),
             value_nim : $("#value_nim").val(),
            total_data : total_data,
            random_number : random_number,
            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/download_mahasiswa/download_prestasi/download_file.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_recs.push(data[index].data_rec);

           console.log(data_recs);

            //data_rec.push(data[index].data_rec);
           
            $(".current-count").html(progress);
            $(".total-count").html(total_data);
            persen = parseInt(persen);
            proses(persen);
              
              counters+=50;
              progress+=50;
              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');
                  $("#ok_info_download").attr("data-uri",data[index].data_uri);
                  $("#ok_info_download").attr("data-file",data[index].json_file);
                  $("#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>