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


		$requestData= $_REQUEST;

		$sSearch = $requestData['search']['value'];
	
		$iStart = $requestData['start'];
		$iLength = $requestData['length'];

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


$semester = "";
$jenis = "";
$id_sms = "";
$filter_kelas = "";
$status_sync = "";
$filter_count = "";
 if ($_POST['semester']!='all') {
 	$semester = "and id_semester='".$_POST['semester']."'";
 }

  if ($_POST['jurusan']!='all') {
  	$id_sms = "and id_prodi='".$_POST['jurusan']."'";
 }

  if ($_POST['jenis']!='all') {
	$jenis = "and id_jenis_aktivitas='".$_POST['jenis']."'";
 }

  if ($_POST['status_sync']!='all') {
	$status_sync = "and status_sync='".$_POST['status_sync']."'";
	if ($_POST['status_sync']=='belum sync') {
		$filter_count = "and id_aktivitas in(select id_akt_mhs from akt_mhs where last_sync='1901-01-01 00:00:00')";
	} else {
		$filter_count = "and id_aktivitas in(select id_akt_mhs from akt_mhs where last_sync!='1901-01-01 00:00:00')";
	}
	
 }

$filter_kelas = "$semester $id_sms $jenis";
$filter_kelas = "id_aktivitas is not null ".trim($filter_kelas);

if ($sSearch!='') {
	$filter_kelas .= "and (judul like '%".$sSearch."%' or keterangan like '%".$sSearch."%')";
}



		//$totalData = $temp_total['result'];

		if ($semester!='') {
			$order_by = "tanggal_sk_tugas ASC";
		} else {
			$order_by = "id_semester DESC";
		}


	
		$temp_data_kelas = [
			'act' => 'GetListAktivitasMahasiswa',
		    'token' => $token,
		    'filter' => trim($filter_kelas)." $status_sync",
		    'order' => $order_by,
		    'limit' => $temp_limit,
		    'offset' => $temp_offset

		];


		$temp_rec = json_decode($db->run($temp_data_kelas,$url));
/*		dump($temp_data_kelas);
		dump($temp_rec);*/



		$count_kelas = [
			'act' => 'GetCountAktivitasMahasiswa',
		    'token' => $token,
		    'filter' => trim($filter_kelas)." $filter_count"
		];


		$temp_recs = json_decode($db->run($count_kelas,$url));
		$totalData = $temp_recs->data;
		$totalFiltered = $totalData;



			$temp_data = array();
			$i=0;
			
			if (!empty($temp_rec->data)) {
			foreach ($temp_rec->data as $key) {

				$temps = array();

				$temps[] = ++$i+$temp_offset." <input type='checkbox'  class='deleteRow' value='".$key->id_aktivitas."'/>";
				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>';
				}
				//$jumlah_dosen = count(array_filter(explode(',', $key->nama_dosen)));
				$temps[] = $key->id_semester;
				$temps[] = $key->nama_jenis_aktivitas;
				$temps[] = $key->judul;
				$temps[] = $key->keterangan;
				$temps[] = $key->tanggal_sk_tugas;
				$temps[] = $key->nama_prodi;
				$temps[] = $key->id_aktivitas;
				
				$temp_data[] = $temps;
			}
			}

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

