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

			$semester = "";
			$jenis = "";
			$id_prodi = "";
			$filter_kelas = "";
			$count_nim = "";
			$prodi_akt = "";
			$count_judul = "";
			$pencarian = "";
			$count_id = "";

			  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']!='all') {
			  	$jenis = "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']=='nim') {
			 		$pencarian = "and nim='".$_POST['keyword']."'";
			 		$count_nim = "and nipd='".$_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']."%'";
			 	}
			 }

			 $filter_anggota = "$semester $id_prodi $jenis $pencarian";



			$data_param = [
				'act' => 'GetListAnggotaAktivitasMahasiswa',
			    'token' => $token,
			    'filter' => "id_anggota is not null ".$filter_anggota,
			    'order' => "",
			    'limit' => $temp_limit,
			    'offset' => $temp_offset

			];
			$anggota_data = service_request($data_param);
			//dump($data_param);
			//dump($anggota_data);

			$id_akt_anggota = $anggota_data;

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




		$data_param = [
			'act' => 'GetCountAktivitasMahasiswa',
		    'token' => $token,
		    'filter' => "id_aktivitas in (select id_akt_mhs from anggota_akt_mhs where soft_delete='0' $count_nim) 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($anggota_data->data)) {
				foreach ($anggota_data->data as $key) {

				$temps = array();

				$temps[] = ++$i+$temp_offset." <input type='checkbox'  class='deleteRow' value='".$key->id_anggota."'/>";
				$temps[] = $data_aktivitas[$key->id_aktivitas]->id_semester;
				$temps[] = $data_aktivitas[$key->id_aktivitas]->nama_jenis_aktivitas;
				$temps[] = $key->judul;
				$temps[] = $key->nim;
				$temps[] = $key->nama_mahasiswa;
				$temps[] = $key->nama_jenis_peran;
				$temps[] = $data_aktivitas[$key->id_aktivitas]->keterangan;
				$temps[] = $data_aktivitas[$key->id_aktivitas]->nama_prodi;
				$temps[] = $key->id_anggota;
				
				$temp_data[] = $temps;
				}	
			}

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

