<?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();

$data_sms = $db->fetch_custom("select * from jurusan");
foreach ($data_sms as $dts) {
	$dts_sms[$dts->id_sms] = $dts->jenjang.' '.$dts->nama_jurusan;
}

if (isset($_POST['semester'])) {
  
  if ($_POST['semester']=='all') {
    	$semester = '';
    	$semester_old = '';
    	if ($_POST['jurusan']=='all') {
		$id_sms = '';
    	if ($_SESSION['level']!=1) {
			$id_sms = "id_prodi='".$_SESSION['id_sms']."'";
		}

	  } else {
	    $id_sms = "id_prodi='".$_POST['jurusan']."'";
	  }
  } else {
    $semester = "id_semester='".$_POST['semester']."'";
    $semester_old = "p.id_smt='".$_POST['semester']."'";
    	if ($_POST['jurusan']=='all') {
    		$id_sms = '';
    		if ($_SESSION['level']!=1) {
			$id_sms = "id_prodi='".$_SESSION['id_sms']."'";
		}
	  } else {
	    $id_sms = "and id_prodi='".$_POST['jurusan']."'";
	  }

  }

  
  if ($_POST['kode_mk']=='all') {
    $kode_mk = '';
  } else {
    $kode_mk = "and id_matkul='".$_POST['kode_mk']."'";
  }

  	$filter_kelas = "$semester $id_sms $kode_mk";


} else {
		if (isset($_POST['id_sms_user'])) {
			$filter_kelas = "id_prodi='".$_SESSION['id_sms']."'";
		} else {
			$filter_kelas = "";

		}
	
}
	
	if ($sSearch) {

	if ($filter_kelas!="") {
		$filter_kelas .= "and kode_mata_kuliah like '%".$sSearch."%'";
	} else {
		$filter_kelas .= "kode_mata_kuliah like '%".$sSearch."%'";
	
	}
}

$filter_kelas = trim($filter_kelas);



		//$totalData = $temp_total['result'];

		if ($semester!='') {
			$order_by = "nama_kelas_kuliah ASC";
		} else {
			$order_by = "id_semester DESC";
		}


		
		$temp_data_kelas = [
			'act' => 'GetDetailKelasKuliah',
		    'token' => $token,
		    'filter' => $filter_kelas,
		    'order' => '',
		    'limit' => $temp_limit,
		    'offset' => $temp_offset

		];

		$temp_rec = json_decode($db->run($temp_data_kelas,$url));
		//print_r($temp_rec);

	/*	dump($temp_data_kelas);
		exit();*/

		$dosen_name = array();
		$jml_siswa = array();
		if ($temp_rec->data) {
					foreach ($temp_rec->data as $dt) {
				  $id_kelas[] = $dt->id_kelas_kuliah;
				}


				$in_id_kelas = "'" . implode("','", $id_kelas) . "'";
				//jumlah pengajar
				$count_kelas = [
					'act' => 'ExportDataMengajarDosen',
				    'token' => $token,
				    'filter' => "id_kelas_kuliah in($in_id_kelas)",
				];
				$temp_recs = json_decode($db->run($count_kelas,$url));
				if ($temp_recs->data) {
					foreach ($temp_recs->data as $count) {
				    	$dosen_name[$count->id_kelas_kuliah] = '- '.$count->nama_dosen."<br>";
					}
				}

				//jumlah mahasiswa kelas
				$count_kelas_mhs = [
					'act' => 'GetKRSMahasiswa',
				    'token' => $token,
				    'filter' => "id_kelas in($in_id_kelas)",
				];
			
				$temp_recs_mhs = json_decode($db->run($count_kelas_mhs,$url));
			    foreach ($temp_recs_mhs->data as $key => $dt) {
			      $data[$dt->id_kelas][] = $dt;
			      $jml_siswa[$dt->id_kelas] = count($data[$dt->id_kelas]);
			    }
		}




		$temp_data_kelas_count = [
			'act' => 'GetDetailKelasKuliah',
		    'token' => $token,
		    'filter' => $filter_kelas
		];

		$temp_rec_count = json_decode($db->run($temp_data_kelas_count,$url));

        $jumlah = 0;
       // dump($temp_count);
        if(!empty($temp_rec_count->data)) {
          $jumlah = count($temp_rec_count->data);
        }
        

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


			$temp_data = array();
			$i=0;
							foreach ($temp_rec->data as $key) {

				$temps = array();

				$temps[] = ++$i+$temp_offset." <input type='checkbox'  class='deleteRow' value='".$key->id_kelas_kuliah."'/>";
				$temps[] = $key->id_semester;
				$temps[] = $key->kode_mata_kuliah;
				$temps[] = $key->nama_mata_kuliah;
				$temps[] = $key->nama_kelas_kuliah;
				if (in_array($key->id_kelas_kuliah, array_keys($dosen_name))) {
					$temps[] = $dosen_name[$key->id_kelas_kuliah];
				} else {
					$temps[] = '';
				}
				if (in_array($key->id_kelas_kuliah, array_keys($jml_siswa))) {
					$temps[] = $jml_siswa[$key->id_kelas_kuliah];
				} else {
					$temps[] = 0;
				}
				if (in_array($key->id_prodi, array_keys($dts_sms))) {	
					$temps[] = $dts_sms[$key->id_prodi];
				} else {
					$temps[] = '';
				}
				$temps[] = $key->id_kelas_kuliah;
				
				$temp_data[] = $temps;
			}
			$temp_output = array(
									'draw' => intval($requestData['draw']),
									'recordsTotal' => intval( $totalData ),
									'recordsFiltered' => intval( $totalFiltered ),
									'data' => $temp_data
				);
			echo json_encode($temp_output);

