<?php
session_start();
include "../../inc/config.php";

$periode = $_POST['semester'];
$tahun_ajar = substr($periode, 0, 4);
$token = get_token();

// 1. Ambil data jurusan
$and_jurusan = ($_POST['jurusan'] != 'all') ? "and kode_jurusan='" . $_POST['jurusan'] . "'" : "";
$jurusan = $db->fetch_custom("select * from jurusan where status='A' $and_jurusan order by nama_jurusan asc");

// 2. Ambil data penugasan seluruh dosen (Hanya 1x call API di luar loop)
$data_penugasan_all = [
    'act' => 'GetListPenugasanSemuaDosen',
    'token' => $token,
    'filter' => "id_tahun_ajaran='$tahun_ajar'"
];
$temp_all = service_request($data_penugasan_all);
$all_penugasan = [];
if (!empty($temp_all->data)) {
    foreach ($temp_all->data as $item) {
        $all_penugasan[$item->id_dosen] = $item;
    }
}
?>
<table id="rasio_table" class="table table-bordered table-striped">
    <thead>
        <tr>
            <th>No</th>
            <th>Program Studi</th>
            <th>Dosen Penghitung Rasio</th>
            <th>Mahasiswa</th>
            <th>Rasio (1 : N)</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $no = 1;
        foreach ($jurusan as $jur):
            // Reset counters
            $jml_dosen_homebase = 0;
            $jml_dosen_nonhomebase = 0;

            // API: Mahasiswa Aktif (AKM)
            $res_akm = service_request([
                'act' => 'GetCountPerkuliahanMahasiswa',
                'token' => $token,
                'filter' => "id_prodi='$jur->id_sms' and id_semester='$periode'"
            ]);
            $jml_mhs = $res_akm->data ?? 0;

            // API: Dosen Homebase Prodi ini
            $res_homebase = service_request([
                'act' => 'GetListPenugasanDosen',
                'token' => $token,
                'filter' => "id_prodi='$jur->id_sms' and id_tahun_ajaran='$tahun_ajar' and a_sp_homebase='1'"
            ]);
            $data_homebase = [];
            if (!empty($res_homebase->data)) {
                foreach ($res_homebase->data as $h) { $data_homebase[$h->id_dosen] = true; }
            }

            // API: Dosen yang mengajar di prodi ini pada semester ini
            $res_pengajar = service_request([
                'act' => 'GetDosenPengajarKelasKuliah',
                'token' => $token,
                'filter' => "id_prodi='$jur->id_sms' and id_semester='$periode'"
            ]);

            if (!empty($res_pengajar->data)) {
                $id_pengajar_unique = array_unique(array_column($res_pengajar->data, 'id_dosen'));
                
                foreach ($id_pengajar_unique as $id_dosen) {
                    if (isset($data_homebase[$id_dosen])) {
                        $jml_dosen_homebase++;
                    } elseif (isset($all_penugasan[$id_dosen])) {
                        $jml_dosen_nonhomebase++;
                    }
                }
            }

            $total_dosen = $jml_dosen_homebase + $jml_dosen_nonhomebase;
            $rasio = ($total_dosen > 0) ? round($jml_mhs / $total_dosen, 2) : 0;
        ?>
            <tr>
                <td><?= $no++; ?></td>
                <td><?= $jur->jenjang . " " . $jur->nama_jurusan; ?></td>
                <td class="text-center"><?= $total_dosen; ?></td>
                <td class="text-center"><?= $jml_mhs; ?></td>
                <td><strong>1 : <?= $rasio; ?></strong></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<script type="text/javascript">
$(document).ready(function() {
  $('#rasio_table').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' ]
      }
    ]
  });
});

</script>