<?php
session_start();
include "../../../inc/config.php";
$url = get_service_url('rest');
$token = get_token();
$requestData= $_REQUEST;

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

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

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


	



		$jenis_peran = "";
		$semester = "";
		$jenis = "";
		$id_prodi = "";
		$pencarian = "";
		$jenis_akt = "";
		$count_nidn = "";
		$count_judul = "";
		$count_id = "";
		$prodi_akt = "";

			  if ($_POST['semester']!='all') {
			  	$semester = "and id_aktivitas in (select id_akt_mhs from akt_mhs where id_smt='".$_POST['semester']."')";
			  	
			 }

			 if ($_POST['jurusan']!='all') {
			  	$id_prodi = "and id_aktivitas in (select id_akt_mhs from akt_mhs where id_sms='".$_POST['jurusan']."')";
			  	$prodi_akt = "and id_prodi='".$_POST['jurusan']."'";
			}

			  if ($_POST['jenis_peran']!='all') {
			  	$jenis_peran = "and id_aktivitas in (select id_akt_mhs from akt_mhs where id_jns_akt_mhs='".$_POST['jenis_peran']."')";
			 }

			  if ($_POST['jenis']!='all') {
			  	$jenis_akt = "and id_aktivitas in (select id_akt_mhs from akt_mhs where id_jns_akt_mhs='".$_POST['jenis']."')";
			 }

			 if ($_POST['pencarian']!='all') {
			 	if ($_POST['pencarian']=='judul') {
			 		$pencarian = "and id_aktivitas in (select id_akt_mhs from akt_mhs where judul_akt_mhs like '%".$_POST['keyword']."%')";
			 		$count_judul = "and judul like '%".$_POST['keyword']."%'";
			 	}

			 	if ($_POST['pencarian']=='nidn') {
			 		$pencarian = "and nidn='".$_POST['keyword']."'";
			 		$count_nidn = "and nidn='".$_POST['keyword']."'";
			 	}
			 	if ($_POST['pencarian']=='id') {
			 		$pencarian = "and id_aktivitas in (select id_akt_mhs from akt_mhs where ket_akt like '%".$_POST['keyword']."%')";
			 		$count_id = "and keterangan like '%".$_POST['keyword']."%'";
			 	}
			 }

			  if ($_POST['jenis_peran']=='bimbing') {
			  	$jenis = "Pembimbing ";
			  	$table = "GetMahasiswaBimbinganDosen";
			    $filter_where = "id_bimbing_mahasiswa is not null $semester $id_prodi $jenis_akt $pencarian";
			    $count_table = "bimbing_mhs";
			  } else {
			  	$table = "GetListUjiMahasiswa";
			  	$jenis = "Penguji ";
			  	$filter_where = "id_uji is not null $semester $id_prodi $jenis_akt $pencarian";
			  	$count_table = "uji_mhs";
			  }

			$data_param = [
				'act' => $table,
			    'token' => $token,
			    'filter' => trim($filter_where),
			    'order' => "",
			    'limit' => $temp_limit,
			    'offset' => $temp_offset

			];
			$data_uji_bimbing = service_request($data_param);

			$id_akt_anggota = $data_uji_bimbing;

			if (!empty($id_akt_anggota->data)) {
				foreach ($id_akt_anggota->data as $dt) {
					$id_aktivitas[$dt->id_aktivitas] = "'".$dt->id_aktivitas."'";
				}

				$akt_concat_where = implode(",", $id_aktivitas);
				$temp_data_akt = [
					'act' => 'GetListAktivitasMahasiswa',
				    'token' => $token,
				    'filter' => "id_aktivitas in($akt_concat_where)",
				    'order' => "",
				    'limit' => "",
				    'offset' => ""

				];
				$aktivitas_data = service_request($temp_data_akt);

				foreach ($aktivitas_data->data as $akt_data) {
					$data_aktivitas[$akt_data->id_aktivitas] = $akt_data;
				}
			}


/*			dump($data_param);

			dump($data_uji_bimbing);*/

		$data_param = [
			'act' => 'GetCountAktivitasMahasiswa',
		    'token' => $token,
		    'filter' => "id_aktivitas in (select id_akt_mhs from $count_table inner join sdm on $count_table.id_sdm=sdm.id_sdm where $count_table.soft_delete='0' $count_nidn) and id_semester='".$_POST['semester']."' $count_judul $count_id $prodi_akt"

		];
		$jml_anggota = service_request($data_param);

/*		dump($data_param);
		dump($jml_anggota);*/
		$totalData = $jml_anggota->data;
		$totalFiltered = $totalData;

			$temp_data = array();
			$i=0;

			if (!empty($data_uji_bimbing->data)) {
				foreach ($data_uji_bimbing->data as $key) {

					

				if ($_POST['jenis_peran']=='bimbing') {
					$jenis_peran = 'bimbing';
					$id = $key->id_bimbing_mahasiswa;
					$urutan = $key->pembimbing_ke;
				} else {
					$id = $key->id_uji;
					$urutan = $key->penguji_ke;
					$jenis_peran = 'uji';
				}

				$temps = array();

				$temps[] = ++$i+$temp_offset." <input type='checkbox'  class='deleteRow' value='".$id."'/>";
				$temps[] = $data_aktivitas[$key->id_aktivitas]->id_semester;
				$temps[] = $data_aktivitas[$key->id_aktivitas]->nama_jenis_aktivitas;
				$temps[] = $key->judul;
				$temps[] = $key->nidn;
				$temps[] = $key->nama_dosen;
				$temps[] = "<span data-jenis='$jenis_peran' class='jenis'>$jenis$urutan</span>";
				$temps[] = $data_aktivitas[$key->id_aktivitas]->keterangan;
				$temps[] = $data_aktivitas[$key->id_aktivitas]->nama_prodi;
				$temps[] = $id;
				
				$temp_data[] = $temps;
				}	
			}

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

