
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1>
                        Manage Salin Evaluasi Kelas Matkul
                    </h1>
                        <ol class="breadcrumb">
                        <li><a href="<?=base_index();?>"><i class="fa fa-dashboard"></i> Home</a></li>
                        <li><a href="<?=base_index();?>salin-evaluasi-matkul">Salin Evaluasi Matkul</a></li>
                        <li class="active">Salin Evaluasi Matkul 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">

<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">
               <div class="form-group">
                        <label for="Semester" class="control-label col-lg-2">Semester</label>
                        <div class="col-lg-5" >
                        <select id="sem_filter" name="sem_filter" data-placeholder="Pilih Semester ..." class="form-control chzn-select" tabindex="2" style="z-index: 1" required="">
                        <option value="">Pilih Semester</option>
              <?php 
              $url_rest = get_service_url('rest');
              $temp_data_semester = [
                'act' => 'GetSemester',
                  'token' => get_token(),
                  'filter' => "a_periode_aktif='1' and right(id_semester,1) in('1','2')",
                  'order' => 'id_semester desc'
              ];

            $temp_rec = json_decode($db->run($temp_data_semester,$url_rest));
            dump($temp_rec);

               $semester = $db->fetch_custom('select *,left(semester,4) as smt from semester order by semester desc');
                    foreach ($semester as $isi) {
                      if ($temp_rec->data[0]->id_semester==$isi->semester) {
                          echo "<option value='".$isi->semester."' selected>".$isi->nama_semester."</option>";
                          $smt_selected = $isi->semester;    
                        } else {
                          echo "<option value='".$isi->semester."'>".$isi->nama_semester."</option>";    
                        }
                      }
                       ?>
              </select>
              <div id="error_sem"></div>

 </div>
                      </div><!-- /.form-group -->
<div class="form-group">
                                        <label for="Semester" class="control-label col-lg-2">Program Studi</label>
                                        <div class="col-lg-5">
                                        <select id="jurusan" name="jurusan" data-placeholder="Pilih Semester ..." class="form-control chzn-select" tabindex="2">
                               <?php
        $jurusan_sms = "";
        $jur_selected = 0;
        if ($_SESSION['level']!=1) {
              $jurusan_sms = "where id_sms='".$_SESSION['id_sms']."'";
              $jur_selected = 1;
        } else {
          echo '<option value="all">Semua</option>';
        }
        
                                foreach ($db->fetch_custom("select * from jurusan $jurusan_sms") as $isi) {
                                  echo "<option value='$isi->id_sms'>$isi->jenjang $isi->nama_jurusan</option>";
                               } ?>
                              </select>
 <div id="error_prodi"></div>
                 </div>
                                      </div><!-- /.form-group -->
<div class="form-group">
                        <label for="Kode Matakuliah" class="control-label col-lg-2">Matakuliah</label>
                        <div class="col-lg-5">
                      <select class="form-control  chzn-select" name="matkul" id="matkul">
<?php
if ($jur_selected==1) {

    $temp_data_kelas = [
      'act' => 'GetDetailKelasKuliah',
        'token' => get_token(),
        'filter' => "id_semester='$smt_selected' and id_prodi='".$_SESSION['id_sms']."'",
        'order' => "id_semester DESC",
        'limit' => "",
        'offset' => ""

    ];

    $kelas = service_request($temp_data_kelas);

   // dump($temp_data_kelas);



foreach ($kelas->data as $data_kelas) {
    $array_kode_mk[$data_kelas->kode_mata_kuliah] = $data_kelas->nama_mata_kuliah;
}
$kode_mk_group = array_unique($array_kode_mk);

echo '<option value="all">Semua</option>';
foreach ($kode_mk_group as $isi => $data) {
echo "<option value='".$isi."'>".$data."</option>";
}

} else {
  echo '<option value="all">Semua</option>';
}
?>
                  </select> </div><img id="img_loader" style="display:none" src="<?=base_admin();?>assets/dist/img/ajax-loader.gif">
                      </div><!-- /.form-group -->
                      
                      <input type="hidden" name="ids" class="ids">
                      <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 btn-flat"><i class="fa fa-refresh"></i> Filter</span>
                        <!--   <button type="submit" class="btn btn-success"><i class="fa fa-copy"></i> Salin Massal Evaluasi</button> -->
                        </div>
                      </div><!-- /.form-group -->
                    </form>
            </div>
            <!-- /.box-body -->
          </div>

<div class="alert alert-info alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-info"></i> Informasi!</h4>
- Data kelas dibawah muncul jika matakuliah sudah ada Rencana Evaluasi
- Data kelas yang muncul hanya yang belum punya komponen evaluasi
</div>

<div class="alert alert-success success_data" style="display:none">
<h4><i class="icon fa fa-check"></i> Berhasil!</h4>
Rencana Evaluasi Matakuliah berhasil di Salin ke Kelas Kuliah
</div>

<div class="alert alert-danger error_data" style="display:none">
<h4><i class="icon fa fa-warning"></i> Error!</h4>
 <span class="isi_warning"></span>
</div>
            <div class="row" id="show_progress" style="display: none">
   <div class="col-md-11">
    Mohon Tunggu Sedang Menyalin Rencana Evaluasi <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 Akm</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 class="row" id="aksi_top_krs" style="display: none">
                                   
                                    <div class="col-sm-4" style="margin-bottom: 10px;">
                                      
                                      <div class="input-group input-group-sm">
                                          <span class="input-group-btn">
                                            <button type="button" class="btn btn-danger btn-flat selected-data">Terpilih</button>
                                          </span>
                                       <select class="form-control col-lg-3" name="aksi_krs" id="aksi_krs">
                                      <option value="0">Salin data Evaluasi ke Kelas</option>
                                    </select>
                                          <span class="input-group-btn">
                                            <button  class="btn btn-success btn-flat" id="salinTriger" data-toggle="tooltip" data-title="Klik untuk Salin Data">Submit</button>
                                          </span>
                                    </div>
                                    </div>
                                  </div>

                                    <table id="dtb_salin_evaluasi_matkul" class="table table-bordered table-striped" width="100%">
                                   <thead>
                                     <tr>

<th style="padding-right:0;width: 4%"><label class="mt-checkbox mt-checkbox-single mt-checkbox-outline "> <input type="checkbox" class="group-checkable bulk-check"> <span></span></label></th>
                          <th>Semester</th>
                          <th>Kode Matakuliah</th>
                          <th>Nama Matakuliah</th>
                          <th>Nama Kelas</th>
                          <th>Prodi</th>
                          <th>Action</th>
                         
                        </tr>
                                      </thead>
                                        <tbody>
                                        </tbody>
                                    </table>
                                                       <div class="row">
        <div class="col-lg-12">
        </div>
        <!-- /.col-lg-12 -->
    </div>
                                </div><!-- /.box-body -->
                            </div><!-- /.box -->
                        </div>
                    </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: 100px"> 
<div class="form-group" style="margin-bottom:38px">
                        <label style="padding-top:6px" for="Jurusan" class="control-label col-lg-2">Semester</label>
                        <div class="col-lg-10">
                          <select  name="sem_delete" id="sem_delete"  class="form-control " tabindex="2" required>
                          <?php 
               $semester = $db->fetch_custom('select *,left(semester,4) as smt from semester order by semester desc');
                    foreach ($semester as $isi) {
                      if ($isi->smt <= date('Y')) {
                  echo "<option value='".$isi->semester."'>".$isi->nama_semester."</option>";      
                      }
                  } ?>

              </select>
                        </div>
<input type="hidden" id="semester_selected">
                      </div><!-- /.form-group -->
<div class="form-group">
                                        <label for="Semester" class="control-label col-lg-2">Prodi</label>
                                        <div class="col-lg-10">
                                        <select id="jurusan_delete" name="jurusan_delete" data-placeholder="Pilih Semester ..." class="form-control" tabindex="2">
                                        
                                  <?php
 $jurusan_sms = "";
        if ($_SESSION['level']!=1) {
              $jurusan_sms = "where id_sms='".$_SESSION['id_sms']."'";
        } else {
          echo '<option value="all">Semua</option>';
        }
        
                                foreach ($db->fetch_custom("select * from jurusan $jurusan_sms") as $isi) {
                                  echo "<option value='$isi->id_sms'>$isi->jenjang $isi->nama_jurusan</option>";
                               } ?>
                              </select>
 <div id="error_prodi"></div>
                 </div>
                                      </div><!-- /.form-group -->
 </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 -->
                </section><!-- /.content -->
<link href="<?=base_admin();?>assets/plugins/jquery-confirm/jquery-confirm.min.css" rel="stylesheet" type="text/css" />
<script src="<?=base_admin();?>assets/plugins/jquery-confirm/jquery-confirm.min.js" type="text/javascript"></script>

        <script type="text/javascript">

$(document).ready(function() {




  $("#jurusan").change(function(){
             $("#matkul").html('');
        $("#matkul").chosen();
        $("#loadnya").show();
        $("#img_loader").show();
    
      $.ajax({
          url : "<?=base_admin();?>modul/salin_evaluasi_matkul/matakuliah.php",
          type : "post",
          data : {semester : $("#sem_filter").val(),jurusan :  $(this).val() },
          success : function(data) {
              $("#loadnya").hide();
              $("#img_loader").hide();
              $("#matkul").html(data);
              $("#matkul").trigger('chosen:updated');
          }

      });
     
  });

  $("#sem_filter").change(function(){
             $("#matkul").html('');
        $("#matkul").chosen();
        $("#loadnya").show();
        $("#img_loader").show();
    
      $.ajax({
          url : "<?=base_admin();?>modul/salin_evaluasi_matkul/matakuliah.php",
          type : "post",
          data : {semester : $(this).val(),jurusan :  $("#jurusan").val() },
          success : function(data) {
              $("#loadnya").hide();
              $("#img_loader").hide();
              $("#matkul").html(data);
              $("#matkul").trigger('chosen:updated');
          }

      });
     
  });
  
var dataTable;

dataTable = $("#dtb_salin_evaluasi_matkul").DataTable({
           'bProcessing': true,
            'bServerSide': true,
             // "searching": false,
               "ordering": false,
                 "columnDefs": [ {
              "targets": [0,-1],
              "orderable": false,
              "searchable": false

            } ],
            "ajax":{
              url :"<?=base_admin();?>modul/salin_evaluasi_matkul/salin_evaluasi_matkul_data.php",
            type: "post",  // method  , by default get
            data: function ( d ) {
            <?php
            if ($_SESSION['level']!=1) {
              ?>
             
              d.id_sms_user = "<?=$_SESSION['id_sms'];?>";
             
              <?php
            }
            ?>
              d.jurusan = $("#jurusan").val();
              d.semester = $("#sem_filter").val();
              d.kode_mk = $("#matkul").val();
             },
          error: function (xhr, error, thrown) {
            console.log(xhr);
              $(".dtb_akm-error").html("");
              $("#dtb_akm").append('<tbody class="dtb_akm-error"><tr><th colspan="9">No data found in the server</th></tr></tbody>');
              $("#dtb_akm_processing").css("display","none");

            }
          },
          "language": {
             searchPlaceholder: "Pencarian dengan Kode Matakuliah",
          "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 = $("#dtb_salin_evaluasi_matkul").DataTable({
     destroy: true,
           'bProcessing': true,
            'bServerSide': true,
              //"searching": false,
               "ordering": false,
                 "columnDefs": [ {
              "targets": [0,-1],
              "orderable": false,
              "searchable": false

            } ],
            "ajax":{
              url :"<?=base_admin();?>modul/salin_evaluasi_matkul/salin_evaluasi_matkul_data.php",
            type: "post",  // method  , by default get
            data: function ( d ) {
                    d.jurusan = $("#jurusan").val();
                    d.semester = $("#sem_filter").val();
                    d.kode_mk = $("#matkul").val();
                  },
          error: function (xhr, error, thrown) {
            console.log(xhr);
              $(".dtb_akm-error").html("");
              $("#dtb_akm").append('<tbody class="dtb_akm-error"><tr><th colspan="9">No data found in the server</th></tr></tbody>');
              $("#dtb_akm_processing").css("display","none");

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

        });

      });


$('#salinTriger').on("click", function(event){
          if( $('.check-selected:checked').length > 0 ){
            event.preventDefault();


            var ids = [];
            $('.check-selected').each(function(){
              if($(this).is(':checked')) {
                ids.push($(this).val());
              }
            });
            var ids_string = ids.toString();
            $('.ids').val(ids_string);
            $.confirm({
                title: 'Konfirmasi',
                content: 'Apakah Yakin akan Salin Evaluasi Matakuliah ke Kelas ini ?',
                 theme: 'modern',
                buttons: {
                      ok: {
                        text: "Ya Salin !",
                        btnClass: 'btn-primary',
                        keys: ['enter'],
                        action: function(){
                              $("#show_progress").show();
                              $(".batal").show();
                              var start_time = new Date().getTime();
                              var totaldata;
                              var datas = {
                              ids_string : $(".ids").val(),
                            }
                           
                                 $.ajax({
                                  url: "<?=base_admin();?>modul/salin_evaluasi_matkul/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/4);
                                        
                                        getData(bagi,total_data,start_time,0);

                                  }
                                });
                        }
                    },
                    cancel: {
                         text: "Batal"
                    }
                }
            });
          }
        });

$("#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/salin_evaluasi_matkul/salin_evaluasi_matkul_action.php?act=delete_download",
            success: function(data){
              console.log(data);
              dataTable.draw(false);
              $('#informasi_download').modal('hide');
            }
        });
  });

 $(".table").on('click','.salin-evaluasi',function(event) {
    var currentBtn = $(this);
    id_matkul = currentBtn.attr('data-id');
    id_kelas = currentBtn.attr('data-kelas');

    $.confirm({
        title: 'Konfirmasi',
        content: 'Apakah Yakin akan Salin Evaluasi Matakuliah ke Kelas ini ?',
         theme: 'modern',
        buttons: {
              ok: {
            text: "Ya Salin!",
            btnClass: 'btn-primary',
            keys: ['enter'],
            action: function(){
                 $.ajax({
              url: "<?=base_admin();?>modul/salin_evaluasi_matkul/salin_evaluasi_matkul_action.php?act=salin_single",
              dataType: "json",
              type : "post",
              error: function(data ) {
                  $("#loadnya").hide();
                  console.log(data);
                  $(".isi_warning").html(data.responseText);
                  $(".error_data").focus()
                  $(".error_data").fadeIn();
                },
              data : {id_matkul:id_matkul,id_kelas:id_kelas},
              success: function(responseText) {
                
                $.each(responseText, function(index) {
                  if(responseText[index].status=="good") {
                    $(".error_data").hide();
                    $(".success_data").fadeIn(1000);
                    $('html, body').animate({
                            scrollTop: $('.fa-info').offset().top
                        }, 500); // Adjust the duration as needed
                    $(".success_data").fadeOut(2000, function() {
                        dataTable.draw(false);
                    });
                  } else {
                      $("#loadnya").hide();
                      $(".isi_warning").html(responseText[index].error_message);
                      $(".error_data").focus()
                      $(".error_data").fadeIn();
                  }
                });
              }

              });
            }
        },
            /*confirm: {
               text: "ok!",
                btnClass: "btn-primary"
           
            },*/
            cancel: {
                 text: "Batal"
            }
        }
    });

   });


});



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+"%");
}


    var random_number = Math.floor(1000 + Math.random() * 9000);
    var counters = 0;
    var persen = 0;
    var progress=4;
    var all_id = "";
    var error_msg = "";
    var token = "";
    var last = "";

    var data_recs = [];

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

     
 
    if ((bagi*4)==progress) {
      data = {
          ids_string : $(".ids").val(),
          id_data : all_id,
          error_msg : error_msg,
          total_data : total_data,
          offset : counters,
          token : token,
          random_number : random_number,
          last : 'yes'
          }
    } else {
      data = {
            ids_string : $(".ids").val(),
            total_data : total_data,
            offset : counters,
            token : token,
            random_number : random_number,
            last : 'no'
          }
    }


    $.ajax({
        /* The whisperingforest.org URL is not longer valid, I found a new one that is similar... */
        url: "<?=base_admin();?>modul/salin_evaluasi_matkul/salin_masal.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;
            }

           //console.log(data[index].limit);

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

              console.log(total_data);
               if (counters < total_data) {
                 var newOffset = offset + progress;
                    getData(bagi, total_data, start, newOffset);
                  //getData(bagi,total_data,start);
                } else {
                  var end_time = new Date().getTime();
                  waktu = "Total Waktu Generate : "+millisToMinutesAndSeconds(end_time-start);
                  alert('Proses salin data selesai');
                  $("#show_progress").hide();
                  $(".bulk-check").prop("checked",false);
                  $("#aksi_top_krs").hide();
                  $("#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');
                   $('html, body').animate({
                    scrollTop: $('.fa-info').offset().top
                  }, 1000); // Adjust the duration as needed
                   resetVariables();
                } 
              });

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

    });
}

function resetVariables() {
  random_number = Math.floor(1000 + Math.random() * 9000);
  counters = 0;
  persen = 0;
  progress = 4;
  all_id = "";
  error_msg = "";
  token = "";
  last = "";
  data_recs = [];
}
$(".bulk-check").on('click',function() { // bulk checked
          var status = this.checked;
          if (status) {
            select_deselect('select');
          } else {
            select_deselect('unselect');
          }
          
          $(".check-selected").each( function() {
            $(this).prop("checked",status);
          });
        });

  function init_selected() {
      var selected = check_selected();
      var btn_hide = $('#aksi_top_krs');
      if (selected.length > 0) {
          btn_hide.show()
      } else {
          btn_hide.hide()
      }
  }


    function check_selected() {
      var table_select = $('#dtb_salin_evaluasi_matkul tbody tr.selected');
      var array_data_delete = [];
      table_select.each(function() {
          var check_data = $(this).find('.check-selected').val();
          if (typeof check_data != 'undefined') {
              array_data_delete.push(check_data)
          }
      });
      $('.selected-data').text(array_data_delete.length + ' Data Terpilih');
      return array_data_delete
  }
  

  function select_deselect(type) {
      if (type == 'select') {
          $('#dtb_salin_evaluasi_matkul tbody tr').addClass('DTTT_selected selected')
      } else {
          $('#dtb_salin_evaluasi_matkul tbody tr').removeClass('DTTT_selected selected')
      }
    init_selected()
  }
  $(document).on('click', '#dtb_salin_evaluasi_matkul tbody tr .check-selected', function(event) {
      var btn = $(this).find('button');
      if (btn.length == 0) {
          $(this).parents('tr').toggleClass('DTTT_selected selected');
          var selected = check_selected();
         // console.log(selected);
          init_selected();

      }
  });

</script>  
            