
<table id="table_lewat_studi" class="table table-bordered table-striped">
<thead>
<tr>
<th>No</th>                          
<th>NIM</th>
<th>NAMA</th>
<th>Angkatan</th>
<th>Lama Studi (Semester)</th>
<th>Program Studi</th>
</tr>
</thead>
<tbody>
<?php
session_start();
include "../../inc/config.php";
$and_jurusan = "";
if ($_POST['jurusan']!='all') {
  $and_jurusan = "and kode_jurusan='".$_POST['jurusan']."'";
}
$and_jenjang = "";
if ($_POST['jenjang']!='all') {
  $and_jenjang = "and id_jenj_didik='".$_POST['jenjang']."'";
}
$jurusan = $db->fetch_custom("select * from jurusan where status='A' $and_jenjang $and_jurusan order by nama_jurusan asc");
$loop_jur = $jurusan;
foreach ($jurusan as $jur) {
  $id_sms[] = "'".$jur->id_sms."'";
  $where_sms = $jur->id_sms;
}
$implode_sms = implode(",", $id_sms);
$no=1;
$token = get_token();

//check periode terakhir aktif
  //jumlah mahasiswa aktif
  $data_req_periode = [
    'act' => 'GetPeriode',
      'token' => $token,
      'filter' => "id_prodi='$where_sms'",
      'order' => 'periode_pelaporan desc',
      'limit' => 1,
      'offset' => 0
       
  ];
  $get_periode = service_request($data_req_periode);


function countSemesters($semesterCode1, $semesterCode2) {
    // Extract year and semester numbers from the semester codes.
    $year1 = (int) substr($semesterCode1, 0, 4);
    $semester1 = (int) substr($semesterCode1, 4, 1);

    $year2 = (int) substr($semesterCode2, 0, 4);
    $semester2 = (int) substr($semesterCode2, 4, 1);

    // Calculate the number of semesters between the two codes.
    $numSemesters = 0;

    while ($year1 < $year2 || ($year1 == $year2 && $semester1 <= $semester2)) {
        $numSemesters++;
        $semester1++;
        if ($semester1 > 2) {
            $semester1 = 1;
            $year1++;
        }
    }

    return $numSemesters;
}


function subtractSemesters($semesterCode, $numSemesters) {
    // Extract year and semester numbers from the semester code.
    $year = (int) substr($semesterCode, 0, 4);
    $semester = (int) substr($semesterCode, 4, 1);

    // Calculate the new semester and year.
    $semester -= $numSemesters;

    while ($semester < 1) {
        $semester += 2;
 //
        $year--;
    }
    // Format the result into the new semester code.
    $newSemesterCode = sprintf("%d%d", $year, $semester);
    return $newSemesterCode;

}
// Example usage:
$sem_akhir = $_POST['sem_akhir'];
$masa_studi_awal =  $_POST['masa_studi_awal']-1;
$masa_studi_akhir =  $_POST['masa_studi_akhir']-1;



$newSemesterCode = subtractSemesters($sem_akhir, $masa_studi_awal);
$newSemesterCode2 = subtractSemesters($sem_akhir, $masa_studi_akhir);



$numSemesters = countSemesters($newSemesterCode, $sem_akhir);
//echo "Number of semesters between $newSemesterCode and $sem_akhir is $numSemesters.";



$current_tahun = substr($get_periode->data[0]->periode_pelaporan, 0,4);
$max_studi = 7; //max 7 tahun, jadi tahun ke 8 baru dicek lewat
$tahun_awal_masuk_dicek = $current_tahun - $max_studi;


/*if ($_POST['masa_studi']=='all') {
	$and_masa_studi = "left(id_periode_masuk,4) < '$newSemesterCode'";
	$judul = "Mahasiswa Masa Studi yang Lebih dari 7 Tahun";
} else {
	$and_masa_studi = "left(id_periode_masuk,4) = '$tahun_awal_masuk_dicek'";
	$judul = "Mahasiswa Masa Studi injury time (sudah 7 Tahun)";
}*/
//echo "id_periode_masuk >='$newSemesterCode2' and id_periode_masuk <= '$newSemesterCode'";
$filter_lulus = "id_jenis_keluar is null and id_prodi in($implode_sms)";
$and_masa_studi = "$filter_lulus and id_periode_masuk >='$newSemesterCode2' and id_periode_masuk <= '$newSemesterCode'";


if ($masa_studi_awal > 14) {
  $and_masa_studi = "$filter_lulus and id_periode_masuk <= '$newSemesterCode'";
}

  $judul = "Mahasiswa Masa Studi antara $masa_studi_awal s/d $masa_studi_akhir Semester";
  $data_req = [
    'act' => 'GetListRiwayatPendidikanMahasiswa',
      'token' => $token,
      'filter' => $and_masa_studi
  ];
 // dump($data_req);
$get_mahasiswa = service_request($data_req);
foreach ($get_mahasiswa->data as $mhs_data) {

	$lama_studi = $current_tahun - substr($mhs_data->id_periode_masuk,0,4);
?>

  <tr> 
  <td><?=$no;?></td>
  <td><?=$mhs_data->nim;?></td>
  <td><?=$mhs_data->nama_mahasiswa;?></td>
  <td><?=$mhs_data->id_periode_masuk;?></td>
  <td><?=countSemesters($mhs_data->id_periode_masuk,$sem_akhir);?></td>
  <td><?=$mhs_data->nama_program_studi;?></td>
  </tr>
  <?php
  $no++;
}
?>
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function() {
  $('#table_lewat_studi').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>>",
    "lengthMenu": [10, 25, 50, 100,200,300,500,1000],
    buttons: [
      {
        extend: 'collection',
        text: 'Export Data',
               buttons: [
          {
            extend: 'print',
            title: '<?=$judul;?>'
          },
          {
            extend: 'pdfHtml5',
            title: '<?=$judul;?>'
          },
          {
            extend: 'csvHtml5',
            title: '<?=$judul;?>'
          },
          {
            extend: 'copyHtml5',
            title: '<?=$judul;?>'
          },
          {
            extend: 'excelHtml5',
            title: '<?=$judul;?>'
          }
        ]
      }
    ]
  });
});

</script>