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

$url = get_service_url('rest');
$token = get_token();
$filter = "";
$semester = "";
$id_prodi = "";
$status = "";
if ($_POST['semester']!='') {
	$semester = "and id_semester='".$_POST['semester']."'";
}
if ($_POST['jurusan']!='all') {
	$id_prodi = "and id_prodi='".$_POST['jurusan']."'";
}
if ($_POST['status']!='all') {
	$status = "and id_status_mahasiswa='".$_POST['status']."'";
}
$status_mhs = "";
if ($_POST['status_mhs']!='all') {
	$status_mhs = "and id_registrasi_mahasiswa in(select id_reg_pd from reg_pd where reg_pd.id_jns_keluar='1') and id_semester > (select smt_yudisium from reg_pd where id_registrasi_mahasiswa = id_reg_pd)";
}

$filter_angkatan = " and (angkatan BETWEEN '".$_POST['angkatan_awal']."' and '".$_POST['angkatan_akhir']."')";

		$requestData= $_REQUEST;

		$sSearch = $requestData['search']['value'];
		
		//$Data = $this->input->get('columns');
		//$orders = $requestData['order'];
		//$temp_order = 


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

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

$sSearch = addslashes($sSearch);
$search_akm = "";

		if ($sSearch!='') {
			$search_akm = "and (nim like '%".$sSearch."%' or nama_mahasiswa like E'%".$sSearch."%')";
		}
$order = "";
$filter = "$semester $id_prodi $status $filter_angkatan $search_akm $status_mhs";
$filter = "id_registrasi_mahasiswa is not null ".$filter;

if ($_POST['pencarian']!='all') {
	$filter = "nim='".$_POST['keyword']."' $semester";
	$order = "id_semester desc";
}
		
		$order_by = "id_semester desc";
		$temp_data_akm = [
			'act' => 'GetListPerkuliahanMahasiswa',
		    'token' => $token,
		    'filter' => trim($filter),
		    'order' => $order,
		    'limit' => $temp_limit,
		    'offset' => $temp_offset

		];



		$temp_data = json_decode($db->run($temp_data_akm,$url));



		$count_akm = [
			'act' => 'GetCountPerkuliahanMahasiswa',
		    'token' => $token,
		    'filter' => $filter
		];

		
		$temp_recs = json_decode($db->run($count_akm,$url));

		$totalData = $temp_recs->data;
		$totalFiltered = $totalData;
		

		$temp_datas = array();
		$i=0;
		
		foreach ($temp_data->data as $dt) {
			
						$temps = array();
					$temps[] = ++$i+$temp_offset." <input type='checkbox'  class='deleteRow' value='".$dt->id_semester."_".$dt->id_registrasi_mahasiswa."'/>";
					$temps[] = $dt->nama_semester;
					$temps[] = $dt->nim;
					$temps[] = $dt->nama_mahasiswa;
					$temps[] = $dt->angkatan;
	                $temps[] = $dt->ips;
	                $temps[] = $dt->ipk;
	                $temps[] = $dt->sks_semester;
	                $temps[] = $dt->sks_total;
	                $temps[] = $dt->nama_status_mahasiswa;
	                $temps[] = $dt->nama_program_studi;
	                $temps[] = $dt->id_semester."_". $dt->id_registrasi_mahasiswa;
					
				$temp_datas[] = $temps;
			}
			$temp_output = array(
									'draw' => intval($requestData['draw']),
									'recordsTotal' => intval( $totalData ),
									'recordsFiltered' => intval( $totalFiltered ),
									'data' => $temp_datas
				);

			echo json_encode($temp_output);