<style type="text/css">
  .nav-tabs-custom>.nav-tabs>li.active a {
    background: #c4e9ff;;
}
.error {
  color : #f00;
}
</style>   
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1>
                        Manage Update History Pendidikan
                    </h1>
                        <ol class="breadcrumb">
                        <li><a href="<?=base_index();?>"><i class="fa fa-dashboard"></i> Home</a></li>
                        <li><a href="<?=base_index();?>mahasiswa">Mahasiswa</a></li>
                        <li class="active">Mahasiswa List</li>
                    </ol>
                </section>

                <!-- Main content -->
                <section class="content">
                    <div class="row">
                        <div class="col-xs-12">
<div class="nav-tabs-custom">

            <div class="tab-content">
              <div class="tab-pane active" id="tab_1">
                    
                                <div class="box-header">
  <div class="row" id="show_progress" style="display: none">
               <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>
                              
                                                                 <?php
       foreach ($db->fetch_all("sys_menu") as $isi) {
                      if ($path_url==$isi->url) {
                          if ($role_act["insert_act"]=="Y") {
                    ?>
        <a class="btn btn-primary" id="import_data"><i class="fa fa-cloud-upload"></i> Upload Data Excel</a>
   <button class="btn btn-info btn-flat up_feeder"><i class="fa fa-mail-forward"></i> Import ke PDDIKTI feeder</button>
                          <?php
                          }
                       }
}
?>
<div id="hasil_up" style="display:none"></div>
<div id="isi_drop_up" style="display:none">
<p>&nbsp;</p>
<div class="row">
<div class="form-group">
                        <label for="Jurusan" class="control-label col-lg-2">Jenis Method</label>
                        <div class="col-lg-3">
                          <select id="jenis_method" name="jenis_method" data-placeholder="Pilih Metode Import ..." class="form-control" tabindex="2" required>
               <option value="">Pilih Metode Import</option>
               <option value="1">Update History</option>
                <option value="0">Tambah History</option>
                <option value="3">Ubah Prodi</option>
                <!-- <option value="3">Tambah History Alternative</option> -->
              </select>
              <div id="error_method"></div>
                        </div>
                      </div><!-- /.form-group -->
</div>
<p>
<div class="row">

<div class="form-group">
                        <label for="Jurusan" class="control-label col-lg-2">Mulai Semester</label>
                        <div class="col-lg-3">
                          <select id="semester_up" name="semester_up" data-placeholder="Pilih Semester ..." class="form-control chzn-select" tabindex="2" required>
              <option value="all">Semua Semester</option>
          <?php 
 $jurusan_sem = "";
        if ($_SESSION['level']!=1) {
              $jurusan_sem = "where history_pendidikan.kode_jurusan='".$_SESSION['jurusan']."'";
        }
foreach ($db->fetch_custom("select mulai_smt,nama_semester from history_pendidikan inner join semester on
 mulai_smt=semester.semester $jurusan_sem group by mulai_smt order by mulai_smt desc") as $isi) {
                  echo "<option value='$isi->mulai_smt'>$isi->mulai_smt</option>";
               } ?>
              </select>
              <div id="error_sem"></div>
                        </div>
                      </div><!-- /.form-group -->
</div>
<p>
<div class="row">
<div class="form-group">
                        <label for="Jurusan" class="control-label col-lg-2">Prodi</label>
                        <div class="col-lg-3">

                          <select id="jur_up" name="jur_up" data-placeholder="Pilih Jurusan ..." class="form-control chzn-select" tabindex="2">
              <option value="all">Semua</option>
              </select>
              <div id="error_sem"></div>
                        </div>
                      </div><!-- /.form-group -->
</div>
<p>
<div class="row">
<div class="form-group">
           <label for="Jurusan" class="control-label col-lg-2">&nbsp;</label>
            <div class="col-lg-3">
            <button class="btn btn-success btn-flat up_feeder_now">
<i class="fa fa-cloud-download"></i> Upload Data</button>
            </div>
</div>


</div>
</div>

<div class="row" id="progress_nya">
<br>
                <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
              <div class="progress hidden" id="script-progress">
                          <div class="progress-bar progress-bar-striped active" id="progress-bar-start" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: ">

                          </div>
                        </div>
                    <div id="script-output"><em></em></div>
                </div>
            </div>

<br>
<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/history_pendidikan/download_data.php" id="download_form">
                      <div class="form-group">
                        <label for="Semester" class="control-label col-lg-2">Mulai Semester</label>
                        <div class="col-lg-3">
                        <select id="sem_filter" name="sem_filter" data-placeholder="Pilih Angkatan ..." class="form-control chzn-select" tabindex="2">
                        <option value="all">Semua</option>
              <?php 
 $jurusan_sem = "";
        if ($_SESSION['level']!=1) {
              $jurusan_sem = "where history_pendidikan.kode_jurusan='".$_SESSION['jurusan']."'";
        }
foreach ($db->fetch_custom("select mulai_smt,nama_semester from history_pendidikan inner join semester on
 mulai_smt=semester.semester $jurusan_sem group by mulai_smt order by mulai_smt desc") as $isi) {
                  echo "<option value='$isi->mulai_smt'>$isi->mulai_smt</option>";
               } ?>
              </select>

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

 </div>
                      </div><!-- /.form-group -->
<div class="form-group">
                        <label for="Jurusan" class="control-label col-lg-2">Jenis Method</label>
                        <div class="col-lg-3">
                          <select id="filter_jenis_method" name="filter_jenis_method" data-placeholder="Pilih Metode Import ..." class="form-control" tabindex="2" required data-msg-required="Untuk Download Pilih Jenis">
               <option value="">Semua</option>
               <option value="1">Update History</option>
                <option value="0">Tambah History</option>
                  <option value="3">Ubah Prodi</option>
              </select>
              <div id="error_method"></div>
                        </div>
                      </div><!-- /.form-group -->
<div class="form-group">
                        <label for="Kode Matakuliah" class="control-label col-lg-2">Status</label>
                        <div class="col-lg-3">
                       <select class="form-control" name="status" id="status_filter">
                    <option value="all">Semua</option>
                    <option value="0">Belum Di proses</option>
                    <option value="1">Sukses</option>
                    <option value="2">Error</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">Filter</span>
              <button type="submit" class="btn btn-success"><i class="fa fa-cloud-download download_data"></i> Download</button>
                      
                        </div>
                      </div><!-- /.form-group -->
                    </form>
            </div>
    
                                <div class="box-body table-responsive">
                                    <table id="dtb_history" class="table table-bordered table-striped">
                                   <thead>
                                     <tr>
                                           <th><input type="checkbox"  id="bulkDelete"  /> <button id="deleteTriger"><i class="fa fa-trash"></i></button></th>
                          <th>Jenis Import</th>
                          <th>NIPD/NIM</th>
                          <th>NIPD BARU</th>
                          <th>Mulai Semester</th>
                          <th>Jenis Pendaftaran</th>
                          <th>Tgl masuk</th>
                          <th>Prodi</th>
                          <th>Prodi Baru</th>
                          <th>Status</th>
                          
                          <th>Action</th>
                         
                        </tr>
                                      </thead>
                                        <tbody>
                                        </tbody>
                                    </table>
<div class="row">
        <div class="col-lg-12">
            <div class="pull-right">
                <!-- <button type="button" class="btn btn-success" id="down_data_erro">
                <i class="fa fa-file-excel-o"></i> Download data error</button> -->
             
                <button type="button" data-uri="<?=base_admin();?>modul/history_pendidikan/history_pendidikan_action.php" class="btn btn-danger" id="hapus_massal"><i class="fa fa-bitbucket"></i> Hapus Data Massal</button>
            </div>
        </div>
        <!-- /.col-lg-12 -->
    </div>
                                </div><!-- /.box-body -->
                            </div><!-- /.box -->
                        </div>
                   
              </div>

              <!-- /.tab-pane -->
            </div>
            <!-- /.tab-content -->
          </div>
    <div class="modal" id="modal_import_data" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog modal-lg"> <div class="modal-content"><div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&nbsp;</span></button> <h4 class="modal-title">Import Excel </h4> </div> <div class="modal-body" id="isi_import_data"> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div>
                        
<div class="modal modal-danger" id="mass_info" data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog"> <div class="modal-content"><div class="modal-header"> <h4 class="modal-title">Hapus Data Massal</h4> </div> <div class="modal-body" style="height: 140px;"> 
<div class="form-group" style="margin-bottom:32px">
                        <label style="padding-top:6px" for="Jurusan" class="control-label col-lg-3">Semester</label>
                        <div class="col-lg-9">
                          <select name="sem_delete" id="sem_delete"  class="form-control " tabindex="2" required>
<option value="all">Semua Semester</option>
 <?php 
 $jurusan_sem = "";
        if ($_SESSION['level']!=1) {
              $jurusan_sem = "where history_pendidikan.kode_jurusan='".$_SESSION['jurusan']."'";
        }
foreach ($db->fetch_custom("select mulai_smt,nama_semester from history_pendidikan inner join semester on
 mulai_smt=semester.semester $jurusan_sem group by mulai_smt order by mulai_smt desc") as $isi) {
                  echo "<option value='$isi->mulai_smt'>$isi->mulai_smt</option>";
               } ?>
              </select>
                        </div>
<input type="hidden" id="semester_selected">
                      </div><!-- /.form-group -->

<div class="form-group" style="margin-top:39px;margin-bottom:78px">
                        <label for="Semester" class="control-label col-lg-3" style="padding-top:6px">Program Studi</label>
                        <div class="col-lg-9">
                        <select id="jurusan_delete" name="jurusan_delete" data-placeholder="Pilih Program Studi ..." class="form-control" tabindex="2">
                        <option value="all">Semua</option>
              </select>

 </div>
  </div><!-- /.form-group -->
<div class="form-group" style="margin-top:39px">
                        <label for="Kode Matakuliah" class="control-label col-lg-3" style="padding-top:6px">Status</label>
                        <div class="col-lg-9">
                       <select class="form-control" name="status_delete" id="status_delete">
                    <option value="all">Semua</option>
                    <option value="0">Belum Di proses</option>
                    <option value="1">Sukses</option>
                    <option value="2">Error</option>
                  </select> </div>
                      </div><!-- /.form-group -->
<input type="hidden" id="status_selected">
 </div> <div class="modal-footer"> 
 <button type="button" id="delete" class="btn btn-danger">Delete</button> <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div><!-- /.modal -->
        <?php     
      
  foreach ($db->fetch_all("sys_menu") as $isi) {

  //jika url = url dari table menu
  if ($path_url==$isi->url) {
    //check edit permission
  if ($role_act["up_act"]=="Y") {
  $edit = '<a href="'.base_index()."history-pendidikan/edit/'+aData[indek]+'".'" class="btn btn-primary btn-flat"><i class="fa fa-pencil"></i></a>';
  } else {
    $edit ="";
  }
  if ($role_act['del_act']=='Y') {
   $del = "<span data-id='+aData[indek]+' data-uri=".base_admin()."modul/history_pendidikan/history_pendidikan_action.php".' class="btn btn-danger hapus btn-flat"><i class="fa fa-trash"></i></span>';
  } else {
    $del="";
  }
                   } 
  }
  
?>  
                </section><!-- /.content -->
        <script type="text/javascript">
$(document).ready(function(){
$("#download_form").validate();
});
$("#bulkDelete").on('click',function() { // bulk checked
          var status = this.checked;
          $(".deleteRow").each( function() {
            $(this).prop("checked",status);
          });
        });

$('#deleteTriger').on("click", function(event){

              
          if( $('.deleteRow:checked').length > 0 ){
            event.preventDefault();


            var ids = [];
            $('.deleteRow').each(function(){
              if($(this).is(':checked')) {
                ids.push($(this).val());
              }
            });
            var ids_string = ids.toString();



            $('#ucing')
        .modal({ keyboard: false })
        .one('click', '#delete', function (e) {


          $.ajax({
              type: "POST",
              url: "<?=base_admin();?>modul/history_pendidikan/history_pendidikan_action.php?act=del_massal",
              data: {data_ids:ids_string},
              success: function(result) {
                dataTable.draw(false);
              },
              async:false
            });
          $('#ucing').modal('hide');



        });


          }
        });

var dataTable = $("#dtb_history").DataTable({
           "fnCreatedRow": function( nRow, aData, iDataIndex ) {
            var indek = aData.length-1;
            var kur = 0;
     $('td:eq('+indek+')', nRow).html('<?=$edit;?> <?=$del;?>');

       $(nRow).attr('id', 'line_'+aData[indek]);
   },
           'bProcessing': true,
            'bServerSide': true,
        "columnDefs": [ {
              "targets": [0,9],
              "orderable": false,
              "searchable": false

            } ],
            "ajax":{
              url :"<?=base_admin();?>modul/history_pendidikan/history_pendidikan_data.php",
            type: "post",  // method  , by default get
             data: function ( d ) {
             <?php
            if ($_SESSION['level']!=1) {
              ?>
              d.jurusan = <?=$_SESSION['jurusan'];?>;
              <?php
            } else {
              ?>
              d.jurusan = $("#jur_filter").val();
              <?php
            }
            ?>
            d.semester = $("#sem_filter").val();
            d.status_filter = $("#status_filter").val();
            d.filter_jenis_method = $("#filter_jenis_method").val();
            },
          error: function (xhr, error, thrown) {
            console.log(xhr);

            },
      
          },
                     "language": {
            "infoPostFix":"<br><b>Jumlah :</b> <br> Belum diproses(0): <b>_BELUM_</b> <br>Sukses(1): <b>_SUKSES_</b> <br> Error(2): <b> _ANEH_ </b>"
        },

        });

//filter
$('#filter').on('click', function() {
  dataTable.ajax.reload();
});


        $("#import_data").click(function() {
          $.ajax({
              url : "<?=base_admin();?>modul/history_pendidikan/import_data.php",
              type : "GET",
              success: function(data) {
                  $("#isi_import_data").html(data);
                  dataTable.draw();
              }
          });
      $('#modal_import_data').modal({ keyboard: false,backdrop:'static',show:true });
    });

$(document).ready(function() {

$(".table").on('click','.hapus',function(event) {

    event.preventDefault();
    var currentBtn = $(this);

    uri = currentBtn.attr('data-uri');
    id = currentBtn.attr('data-id');


    $('#ucing')
        .modal({ keyboard: false })
        .one('click', '#delete', function (e) {

                $.ajax({
          type: "POST",
          data : 'id='+id,
          url: uri+"?act=delete&id="+id,
          success: function(data){
            console.log(data);
              dataTable.draw(false);
          }
          });
          $('#ucing').modal('hide');

        });



  });

  //filter 
$('#sem_filter').on('change', function() {
      $.ajax({
          url : "<?=base_admin();?>modul/history_pendidikan/jurusan_data.php",
          type : "post",
          data : {semester : $(this).val() },
          success : function(data) {
              $("#jur_filter").html(data);
              $("#jur_filter").trigger('chosen:updated');
          }

      });
});

  //filter 
$('#semester_up').on('change', function() {
      $.ajax({
          url : "<?=base_admin();?>modul/history_pendidikan/jurusan_data.php",
          type : "post",
          data : {semester : $(this).val() },
          success : function(data) {
              $("#jur_up").html(data);
              $("#jur_up").trigger('chosen:updated');
          }

      });
});


    
      //trigger validation onchange
      $('#jenis_method').on('change', function() {
          if ($(this).val()!='') {
              $("#jenis_method").parent().parent().removeClass('has-error');
              $("#error_method").html('');
          } else {
              $("#jenis_method").parent().parent().addClass('has-error');
              $("#error_method").html('<span for="jenis_method" class="help-block">Pilih Metode.</span>');
          }
      });

  //filter delete massal
$('#sem_delete').on('change', function() {

      //$("#jurusan_delete").chosen();
      $.ajax({
          url : "<?=base_admin();?>modul/history_pendidikan/jurusan_data.php",
          type : "post",
          data : {semester : $(this).val() },
          success : function(data) {
              $("#jurusan_delete").html(data);
              $("#jurusan_delete").trigger('chosen:updated');

          }

      });
   
});


$('.up_feeder').on('click', function() {

  if ($('#isi_drop_up').is(":visible")){
    $("#isi_drop_up").hide();
    $("#isi_drop").hide();
    $("hasil_up").hide();
  } else {
    $("#isi_drop_up").show();
    $("#isi_drop").hide();
    $("hasil_up").hide();
  }


});


$('.up_feeder_now').on('click', function() {
    if ($("#jenis_method").val()=="") {
      $("#jenis_method").parent().parent().addClass('has-error');
      $("#error_method").html('<span for="jenis_method" class="help-block">Pilih Metode.</span>');
    } else {
          $("#isi_drop_up").hide();
          $("#isi_drop").hide();

          $("#loadnya").show();
          var start_time = new Date().getTime();
          $("#show_progress").show();
          total_data = parseInt(total());

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


});


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){
    if(percent>10){
      
      $("#progressbar").css("width",percent+"%");
      $("#progressbar").html(percent+"%");
      } else {
        $("#progressbar").css("width",10+"%");
        $("#progressbar").html(10+"%");
      }
    } 

function total() {

          var totaldata;
          var datas = {
            jenis_method : $("#jenis_method").val(),
            sem : $("#semester_up").val(),
            jurusan : $("#jur_up").val()
        }
             $.ajax({
              url: "<?=base_admin();?>modul/history_pendidikan/get_jumlah.php",
              type : "post",
              dataType: 'json',
              'async':false,
              data : datas,
              success: function(data) {
                  totaldata = data.jumlah;
              }
            });

    return totaldata;
}



   
    var counters = 0;
    var jumlah_error=0;
    var jumlah_sukses = 0;
    var persen = 0;
    var progress=11;
    var all_id = "";
    var random_number = Math.floor(1000 + Math.random() * 9000);
    var token = ""; 
window.getData=function(bagi,total_data,start_time)
{
    var start = start_time;
 
    if ((bagi*11)==progress) {
      data = {
          jenis_method : $("#jenis_method").val(),
          sem : $("#semester_up").val(),
          jurusan : $("#jur_up").val(),
          offset : counters,
          id_data : 'end',
          token : token,
          random_number : random_number,
          total_data : total_data
          }
    } else {
      data = {
            jenis_method : $("#jenis_method").val(),
            sem : $("#semester_up").val(),
            jurusan : $("#jur_up").val(),
            offset : counters,
            token : token,
            random_number : random_number,
            total_data : total_data
          }
    }




    $.ajax({
        /* The whisperingforest.org URL is not longer valid, I found a new one that is similar... */
        url: "<?=base_admin();?>modul/history_pendidikan/push_history.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;
            }
         
            $(".current-count").html(progress);
            $(".total-count").html(total_data);
            persen = parseInt(persen);
            proses(persen);

            token = data[index].token;
              
              counters+=11;
              progress+=11;

               if (counters < total_data) {
                  getData(bagi,total_data,start);
                } else {
                 $("#loadnya").hide();
                  var end_time = new Date().getTime();
                  waktu = "Total Waktu Import : "+millisToMinutesAndSeconds(end_time-start);
                  alert('Import data ke Feeder Selesai');
                  $("#isi_informasi").html(data[index].last_notif.concat(waktu));
                  $('#informasi').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>  