<div class="callout callout-info">
<h4>Informasi!</h4>
<p>Jika Nilai Huruf sudah di input, maka dianggap sudah dinilai. Anda bisa lihat Mahasiswanya yang Nilai Angka atau huruf nya masih kosong.</p>
</div>
<table id="kelas_krs" class="table table-bordered table-striped">
<thead>
<tr>
<th>No</th>                          
<th>Program Studi</th>
<th>Jumlah KRS</th>
<th>Jumlah NILAI</th>
<th>Nilai Angka Kosong</th>
<th>Nilai Huruf Kosong</th>
<th>Prosentasi Penilaian</th>
</tr>
</thead>
<tbody>
<?php
session_start();
include "../../inc/config.php";
$and_jurusan = "";
if ($_POST['jurusan']!='all') {
  $and_jurusan = "and kode_jurusan='".$_POST['jurusan']."'";
}
$periode = $_POST['semester'];
$jurusan = $db->fetch_custom("select * from jurusan where status='A' $and_jurusan order by nama_jurusan asc");
$loop_jur = $jurusan;
$jumlah_krs = array();
$jumlah_khs = array();
$jumlah_khs_angka = array();

$no=1;
$token = get_token();
foreach ($jurusan as $jur) {
  
  //jumlah mahasiswa aktif
  $data_req_krs = [
    'act' => 'GetCountNilaiPerkuliahanKelas',
      'token' => $token,
      'filter' => "id_semester='$periode' and id_prodi='$jur->id_sms'"
  ];

$data_req_khs = [
    'act' => 'GetCountNilaiPerkuliahanKelas',
      'token' => $token,
      'filter' => "id_semester='$periode' and id_prodi='$jur->id_sms' and nilai_huruf is not null"
  ];

$data_req_khs_angka = [
    'act' => 'GetCountNilaiPerkuliahanKelas',
      'token' => $token,
      'filter' => "id_semester='$periode' and id_prodi='$jur->id_sms' and nilai_angka is null"
  ];

 // dump($data_req_krs);
  $get_jumlah_krs = service_request($data_req_krs);
  //dump($get_jumlah_krs);
  $get_jumlah_khs = service_request($data_req_khs);

  $get_jumlah_khs_angka = service_request($data_req_khs_angka);

  //dump($get_jumlah);
  if ($get_jumlah_krs->data) {
      $jumlah_krs[$jur->id_sms] = $get_jumlah_krs->data;
  } else {
      $jumlah_krs[$jur->id_sms] = 0;
  }
  if ($get_jumlah_khs->data) {
      $jumlah_khs[$jur->id_sms] = $get_jumlah_khs->data;
  } else {
      $jumlah_khs[$jur->id_sms] = 0;
  }
  if ($get_jumlah_khs_angka->data) {
      $jumlah_khs_angka[$jur->id_sms] = $get_jumlah_khs_angka->data;
  } else {
      $jumlah_khs_angka[$jur->id_sms] = 0;
  }

}

$loop_jur = $db->fetch_custom("select * from jurusan where status='A' $and_jurusan order by nama_jurusan asc");
foreach ($loop_jur as $jur) {
$belum_dinilai = 0;
$krs = 0;
$khs = 0;
  //dump($get_jumlah_akm);
  // Add conditional statements to check for division by zero and NaN
  if ($jumlah_krs[$jur->id_sms] != 0) {
    $krs = $jumlah_krs[$jur->id_sms];
    if ($jumlah_khs[$jur->id_sms] != 0) {
      $prosen = ($jumlah_khs[$jur->id_sms]/$jumlah_krs[$jur->id_sms])*100;
      $prosen = round($prosen, 2);
      $khs = $jumlah_khs[$jur->id_sms];
    } else {
      $prosen = 0;
    }
  } else {
    $prosen = "N/A";
  }
if ($prosen==100) {
  $btn = 'badge bg-green';
} elseif ($prosen > 50) {
  $btn = 'badge bg-light-blue';
} else {
  $btn = 'badge bg-red';
}
$belum_dinilai = $jumlah_krs[$jur->id_sms] - $jumlah_khs[$jur->id_sms];
?>

  <tr> 
  <td><?=$no;?></td>
  <td><?=$jur->jenjang.' '.$jur->nama_jurusan;?></td>
  <td><?=$krs;?></td>
  <td><?=$khs;?></td>
  <td><a class='belum-nilai' style="cursor: pointer;" data-toggle="tooltip" data-title="Lihat Data Nilai Angka Kosong" data-nilai='angka' data-sem="<?=$periode;?>" data-id="<?=$jur->id_sms;?>"><?=$jumlah_khs_angka[$jur->id_sms];?></a></td>
  <td><a class='belum-nilai' style="cursor: pointer;" data-toggle="tooltip" data-title="Lihat Data Nilai Huruf Kosong" data-nilai="huruf" data-sem="<?=$periode;?>" data-id="<?=$jur->id_sms;?>"><?=$belum_dinilai;?></a></td>

  <td><span class="<?=$btn;?>"><?=round($prosen,2);?> %</span></td>
  </tr>
  <?php
  $no++;
}
?>
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function() {
  $('#kelas_krs').DataTable({
    "dom": "<'row'<'col-sm-12'B>>" + "<'row'<'col-sm-6'l><'col-sm-6'f>>" +"<'row'<'col-sm-12'tr>>" +"<'row'<'col-sm-5'i><'col-sm-7'p>>",
    buttons: [
      {
        extend: 'collection',
        text: 'Export Data',
        buttons: [ 'print','pdfHtml5', 'csvHtml5', 'copyHtml5', 'excelHtml5' ]
      }
    ]
  });
});

    $("#kelas_krs").on('click','.belum-nilai',function(event) {
        $("#loadnya").show();
        event.preventDefault();
        var currentBtn = $(this);
        
        id = currentBtn.attr('data-id');
        sem_id = currentBtn.attr('data-sem');
        data_nilai = currentBtn.attr('data-nilai');
        $(".modal-title").html("Data Krs belum ada Nilai "+data_nilai);

        $.ajax({
            url : "<?=base_admin();?>modul/rekapitulasi/show_belum_dinilai.php",
            type : "post",
            data : {id_data:id,sem_id:sem_id,data_nilai:data_nilai},
            success: function(data) {
                $("#isi_data").html(data);
                $("#loadnya").hide();
          }
        });

    $('#modal_data').modal({ keyboard: false,backdrop:'static' });

    });
</script>