
<?php
session_start();
include "../../inc/config.php";
$requestData= $_REQUEST;

function tgl_indo_lahir($date) { // fungsi atau method untuk mengubah tanggal ke format indonesia
	if ($date!="") {
          // variabel BulanIndo merupakan variabel array yang menyimpan nama-nama bulan
	      $BulanIndo = array("Januari", "Februari", "Maret",
	                 "April", "Mei", "Juni",
	                 "Juli", "Agustus", "September",
	                 "Oktober", "November", "Desember");
	    
	      $tgl = substr($date, 0, 2); // memisahkan format tahun menggunakan substring
	      $bulan = substr($date, 3, 2); // memisahkan format bulan menggunakan substring
	      $tahun   = substr($date, 6, 4); // memisahkan format tanggal menggunakan substring
	      
	      $result = $tgl . " " . $BulanIndo[(int)$bulan-1] . " ". $tahun;
	      return($result);
	} else {
    	return '';
    }
}
$requestData= $_REQUEST;

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

$angkatan = "";
$id_prodi = "";
$id_jns_daftar = "";


 if ($_POST['pencarian']!='all' and $_POST['pencarian']!='no_nik') {
 	if ($_POST['pencarian']=='nim') {
 		$search_filter = "nim like '%".$_POST['keyword']."%'";
 	}

 	if ($_POST['pencarian']=='nama') {
 		$search_filter = "lower(nama_mahasiswa) like '%".$_POST['keyword']."%'";
 	}

 	if ($_POST['pencarian']=='nik') {
 		$search_filter = "id_mahasiswa in (select id_pd from peserta_didik where nik='".$_POST['keyword']."')";
 	}

 	if ($_POST['pencarian']=='no_history') {
 		$search_filter = "id_registrasi_mahasiswa is null";
 	}

 } else {
	if ($_POST['jurusan']!='all') {
	  	$id_prodi = "and id_prodi='".$_POST['jurusan']."'";
	}
	if ($_POST['id_jns_daftar']!='all') {
	  	$id_jns_daftar = "and id_registrasi_mahasiswa in (select id_reg_pd from reg_pd where id_jns_daftar='".$_POST['id_jns_daftar']."')";
	}
	if ($_POST['angkatan_awal']!='all') {
	 	$angkatan = "and id_periode BETWEEN '".$_POST['angkatan_awal']."' and '".$_POST['angkatan_akhir']."'";
	}

	$filter_status = "";
	if ($_POST['status_mhs']!='') {
	  $aktif = 0;
	   foreach ($_POST['status_mhs'] as $val) {
	       if ($val!='AKTIF') {
	           $array_status[] = "'$val'";
	       } else {
	          $aktif+=1;
	       }
	   }
	   
	   if (!empty($array_status)) {
	     $implode_status = implode(",", $array_status);
	     if ($aktif>0) {
	        $filter_status = "and (id_registrasi_mahasiswa in(select id_reg_pd from reg_pd where tgl_keluar is null) or id_registrasi_mahasiswa in(select id_reg_pd from reg_pd where id_jns_keluar IN(".$implode_status.") ))";
	     } else {
	        $filter_status = " and id_registrasi_mahasiswa in(select id_reg_pd from reg_pd where id_jns_keluar IN(".$implode_status."))";
	     }
	  } else {
	        $filter_status = " and id_registrasi_mahasiswa in (select id_reg_pd from reg_pd where tgl_keluar is null)";
	  }
	}

	$search_filter = "id_mahasiswa is not null $angkatan $id_prodi $id_jns_daftar $filter_status ";

	if ($_POST['pencarian']=='no_nik') {
 		$search_filter = $search_filter." and id_mahasiswa in (select id_pd from peserta_didik where nik is null)";
 	}
 }

$token = get_token();

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

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

$order_by = "";
$url = get_service_url('rest');

//echo $search_filter;
$data_req_mhs = [
    "act" => "GetCountMahasiswa",
    "token" => $token,
    "filter" => trim($search_filter)
];
$temp_total = json_decode($db->run($data_req_mhs, $url));
$totalData = $temp_total->data;
$totalFiltered = $totalData;
$temp_rec = get_data_services('GetListMahasiswa',trim($search_filter),$order_by, $temp_limit,$temp_offset);
$nik_mhs = array();
$mhs_ids = array();
$implode_id_mhs = "";
$temp_data = array();
		if (!empty($temp_rec)) {
		$temp_rec_for_biodata = $temp_rec;
		
		foreach ($temp_rec_for_biodata as $id_mhs) {
			$mhs_ids[] = "'".$id_mhs->id_mahasiswa."'";
		}
		
		if (!empty($mhs_ids)) {
			$implode_id_mhs = implode(",", $mhs_ids);
		}
		$temp_rec_biodata = get_data_services('GetBiodataMahasiswa',"id_mahasiswa in($implode_id_mhs)","", "","");
		//dump($temp_rec);
		
		foreach ($temp_rec_biodata as $biodata) {
			$nik_mhs[$biodata->id_mahasiswa] = $biodata;
		}

			$temp_data = array();
			$i=0;
			foreach ($temp_rec as $key) {
				$temps = array();
				$temps[] = "<label data-id='$key->id_mahasiswa#$key->id_registrasi_mahasiswa' class='mt-checkbox mt-checkbox-single mt-checkbox-outline data_selected_id'> <input type='checkbox'  class='group-checkable check-selected'> <span></span></label>";
				// <input type='checkbox'  class='deleteRow' value='".$key->id_mahasiswa."'/>";
				if ($key->status_sync=='belum sync') {
					$temps[] = '<span class="btn btn-xs btn-warning"><i class="fa fa-clock-o"></i> Belum Sync</span>';
				} else {
					$temps[] = '<span class="btn btn-xs btn-success"><i class="fa fa-check"></i> Sudah Sync</span>';
				}
				$status = $key->nama_status_mahasiswa;
				if ($key->id_registrasi_mahasiswa=="") {
					$status = "";
				}

				$temps[] = $key->nama_mahasiswa;
				$temps[] = $key->nim;
				if (in_array($key->id_mahasiswa, array_keys($nik_mhs))) {
					$temps[] = $nik_mhs[$key->id_mahasiswa]->nama_ibu_kandung;
				} else {
					$temps[] = "";
				}
				$temps[] = tgl_indo_lahir($key->tanggal_lahir);
				if (in_array($key->id_mahasiswa, array_keys($nik_mhs))) {
					$temps[] = $nik_mhs[$key->id_mahasiswa]->tempat_lahir;
				} else {
					$temps[] = "";
				}
				$temps[] = $key->id_periode;
				$temps[] = $status;
				$temps[] = $key->id_periode_keluar;
				if (in_array($key->id_mahasiswa, array_keys($nik_mhs))) {
					$temps[] = $nik_mhs[$key->id_mahasiswa]->nik;
				} else {
					$temps[] = "";
				}
				$temps[] = $key->nama_program_studi;
			
				$temp_data[] = $temps;
			}

}

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