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


$config = $db->fetch_single_row('config_user','id',1);

if ($config->live=='Y') {
	$url = 'http://'.$config->url.':'.$config->port.'/ws/live2.php'; // gunakan live
	$url_soap = 'http://'.$config->url.':'.$config->port.'/ws/live.php?wsdl'; // gunakan 
} else {
	$url = 'http://'.$config->url.':'.$config->port.'/ws/sandbox2.php'; // gunakan sandbox
	$url_soap = 'http://'.$config->url.':'.$config->port.'/ws/sandbox.php?wsdl'; // gunakan 
}

$client = new nusoap_client($url_soap, true);
$proxy = $client->getProxy();

$data = array(
  'act' => 'GetToken',
  'username' => $config->username,
  'password' => $config->password
);
$result = json_decode($db->run($data,$url));
$token = $result->data->token;



$filter = "";
$semester = "";
$semester_count = "";
$prodi = "";
$keyword = "";
$status = "";
$id_sms = "";
$filter_count = "";
if (isset($_POST['semester'])) {

	if ($_POST['semester']!='all') {
    	$semester = "id_periode='".$_POST['semester']."'";
    	$semester_count = "id_semester='".$_POST['semester']."'";
    	$filter = "$semester";
		$filter_count = "$semester_count";
  	}

	if ($_POST['jurusan']!='all') {
		$jur_name = $db->fetch_single_row("jurusan","id_sms",$_POST['jurusan']);
		$nama_prodi = "$jur_name->jenjang $jur_name->nama_jurusan";
		$prodi = "nama_program_studi like '%".$nama_prodi."%'";
		if ($filter!="") {
			$filter = "$filter and $prodi";
			$filter_count = "$filter_count and $prodi";
		} else {
			$filter = "$prodi";
			$filter_count = "$prodi";
		}
		
	}
	if ($_POST['search_by']!='all') {
		//if searh by nim
		if ($_POST['search_by']==1) {
			$keyword = "nim like '%".$_POST['keyword']."%' or nama_mahasiswa like '%".$_POST['keyword']."%'";
			$temp_data_akm = [
				'act' => 'GetListMahasiswa',
			    'token' => $token,
			    'filter' => $keyword,
			    'order' => "",
			    'limit' => 10,
			    'offset' => 0
			];
			//print_r($temp_data_akm);
			$temp_data = json_decode($db->run($temp_data_akm,$url));
			if (!empty($temp_data->data)) {
				foreach ($temp_data->data as $dt) {
					$nim_in[] = $dt->id_registrasi_mahasiswa;
				}
				$nim_in = "'" . implode("','", $nim_in) . "'";
				$keyword = "id_registrasi_mahasiswa in($nim_in)";
			}

		} 
		//if searh by kodemk
		elseif ($_POST['search_by']==2) {
			$keyword = "kode_mata_kuliah like '%".$_POST['keyword']."%'";
		}
		//if searh by namamk
		elseif ($_POST['search_by']==3) {
			$keyword = "nama_mata_kuliah like '%".$_POST['keyword']."%'";
		}
		//if searh by namakelas
		elseif ($_POST['search_by']==4) {
			$keyword = "nama_kelas_kuliah like '%".$_POST['keyword']."%'";
		}
		if ($filter!="") {
			$filter = "$filter and $keyword";
			$filter_count = "$filter_count and $keyword";
		} else {
			$filter = "$keyword";
			$filter_count = "$keyword";
		}
	}

} else {
	if (isset($_POST['id_sms_user'])) {
		$prodi = "id_prodi='".$_POST['id_sms_user']."'";
		$filter = "$prodi";
		$filter_count = "$prodi";
	} 
}



$filter = trim($filter);
$filter_count = trim($filter_count);


		$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;

		
		$order_by = "id_periode desc";

				$temp_data_akm = [
			'act' => 'GetKRSMahasiswa',
		    'token' => $token,
		    'filter' => $filter,
		    'order' => $order_by,
		    'limit' => $temp_limit,
		    'offset' => $temp_offset

		];

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


                    $temp_data = $proxy->GetRecordset($token,'nilai',$filter_krs,$order_by, $limit,$offset);

                    if ($temp_data['result']) {
                        foreach ($temp_data['result'] as $krs) {
                   
                
                           $data_rec[] = array(
                            'id_kls' => $krs['id_kls'],
                            'id_reg_pd' => $krs['id_reg_pd'],
                            'nipd' => convert_ascii($krs['nipd']),
                            'nm_pd' => convert_ascii($krs['nm_pd']),
                            'kode_mk' => $kelas_attribute[$krs['id_kls']]['kode_mk'], 
                            //convert_ascii($key['kode_mk']),
                            'nama_mk' => $kelas_attribute[$krs['id_kls']]['nama_mk'], 
                            //convert_ascii($key['fk__id_mk']),
                            'id_smt' => convert_ascii($krs['id_smt']),
                            'nama_kelas' => $kelas_attribute[$krs['id_kls']]['nm_kls'], 
                            //convert_ascii($key['nm_kls']),
                            'nilai_huruf' => convert_ascii($krs['nilai_huruf']),
                            'nilai_indeks' => convert_ascii($krs['nilai_indeks']),
                            'nilai_angka' => convert_ascii($krs['nilai_angka']),
                            'kode_prodi' => $kode_prodi
                          );

                        }
                    }
                    

		
		
		if ($filter_count=="") {
			$temp_total = $proxy->GetCountRecordset($token,"nilai","");
			$totalData = $temp_total['result'];
			$totalFiltered = $totalData;
		} else {
			$array_count = array(
			  'act' => 'GetCountNilaiPerkuliahanKelas',
			  'token' => $token,
			  'filter' => $filter_count
			);
			$temp_count = json_decode($db->run($array_count,$url));
			$totalData = $temp_count->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_kelas."$".$dt->id_registrasi_mahasiswa."'/>";
					$temps[] = $dt->id_periode;
					$temps[] = $dt->nim;
					$temps[] = $dt->nama_mahasiswa;
					$temps[] = $dt->kode_mata_kuliah;
	                $temps[] = $dt->nama_mata_kuliah;
	                $temps[] = $dt->nama_kelas_kuliah;
	                $temps[] = $dt->nama_program_studi;
	                $temps[] = $dt->id_kelas."$". $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);