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

$token = get_token();
$url = $db->get_service_url('rest');


$requestData= $_REQUEST;

$sSearch = $requestData['search']['value'];

$iStart = $requestData['start'];
$iLength = $requestData['length'];

$temp_limit = $iLength;
$temp_offset = $iStart?$iStart : 0;

$id_sms_old = "";
$semester = "";
$kode_mk = "";
$id_sms = "";
$id_kelas = array();
$array_jml_mhs = array();
$array_jml_pengajar = array();

$semester = "";
$kode_mk = "";
$id_prodi = "";
if ($_POST['semester']!='all') {
	$semester = "and id_kelas_kuliah in(select id_kls from kelas_kuliah where id_smt='".$_POST['semester']."')";
}

if ($_POST['kode_mk']!='all') {
	$kode_mk = "and id_kelas_kuliah in(select id_kls from kelas_kuliah where id_mk='".$_POST['kode_mk']."')";
}

if ($_POST['jurusan']!='all') {
  	$id_prodi = "and id_kelas_kuliah in(select id_kls from kelas_kuliah where id_sms='".$_POST['jurusan']."')";
}

$search_filter = "";

if ($sSearch!='') {
	$search_filter = "and (nidn like '%".$sSearch."%' or lower(nama_dosen) like '%".strtolower($sSearch)."%')";
}

$filter_kelas = "id_aktivitas_mengajar is not null $semester $id_prodi $kode_mk $search_filter";


				//jumlah pengajar
				$count_kelas = [
					'act' => 'GetDosenPengajarKelasKuliah',
				    'token' => $token,
				    'filter' => $filter_kelas,
				    'order' => '',
				    'limit' => $temp_limit,
				    'offset' => $temp_offset
				];
				$temp_recs = json_decode($db->run($count_kelas,$url));

				$datas_dosen = $temp_recs;

				$data_check_kelas= $temp_recs;
				if (!empty($data_check_kelas->data)) {
					foreach ($data_check_kelas->data as $dt_kls) {
						$id_kls[] = "'$dt_kls->id_kelas_kuliah'";
					}
					$kelas_id = implode(",", $id_kls);
					$temp_data_kelas = [
						'act' => 'GetDetailKelasKuliah',
					    'token' => $token,
					    'filter' => "id_kelas_kuliah in($kelas_id)"

					];
					$temp_rec_kelas = json_decode($db->run($temp_data_kelas,$url));
					foreach ($temp_rec_kelas->data as $dt_kelas) {
						$data_kelas[$dt_kelas->id_kelas_kuliah] = $dt_kelas;
					}
				}

		$temp_count_mk = [
              'act' => 'GetCountDosenPengajarKelasKuliah',
                'token' => $token,
                'filter' => $filter_kelas
            ];
        $temp_count = json_decode($db->run($temp_count_mk,$url));
        $jumlah = 0;
       // dump($temp_count);
        if(!empty($temp_count->data)) {
          $jumlah = $temp_count->data;
        }

		//print_r($count_kelas);

		$totalData = $jumlah;
		$totalFiltered = $totalData;


			$temp_data = array();
			$i=0;
				//dump($datas_dosen);
				if (!empty($datas_dosen->data)) {
					foreach ($datas_dosen->data as $key) {

					$temps = array();

					$temps[] = ++$i+$temp_offset." <input type='checkbox'  class='deleteRow' value='".$key->id_aktivitas_mengajar."'/>";
					$temps[] = $_POST['semester'];
					$temps[] = $data_kelas[$key->id_kelas_kuliah]->kode_mata_kuliah;
					$temps[] = $data_kelas[$key->id_kelas_kuliah]->nama_mata_kuliah;
					$temps[] = $data_kelas[$key->id_kelas_kuliah]->nama_kelas_kuliah;
					$temps[] = $key->nama_dosen;
					$temps[] = $key->sks_substansi_total;
					$temps[] = $key->rencana_minggu_pertemuan;
					$temps[] = $key->realisasi_minggu_pertemuan;
					$temps[] = $data_kelas[$key->id_kelas_kuliah]->nama_program_studi;
					$temps[] = $key->id_aktivitas_mengajar;
					
					$temp_data[] = $temps;
				}
				}

			$temp_output = array(
									'draw' => intval($requestData['draw']),
									'recordsTotal' => intval( $totalData ),
									'recordsFiltered' => intval( $totalFiltered ),
									'data' => $temp_data
				);
			echo json_encode($temp_output);

