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

$id_sms_old = "";
$semester = "";
$kode_mk = "";
$id_sms = "";
$id_kelas = array();
$array_jml_mhs = array();
$array_jml_pengajar = array();

$data_sms = $db->fetch_custom("select * from jurusan");
foreach ($data_sms as $dts) {
	$dts_sms[$dts->id_sms] = $dts->jenjang.' '.$dts->nama_jurusan;
}

if (isset($_POST['semester'])) {
  
  if ($_POST['semester']=='all') {
    	$semester = '';
    	if ($_POST['jurusan']=='all') {
		$id_sms = '';
    	if ($_SESSION['level']!=1) {
			$id_sms = "id_prodi='".$_SESSION['id_sms']."'";
		}

	  } else {
	    $id_sms = "id_prodi='".$_POST['jurusan']."'";
	  }
  } else {
    	$semester = "id_semester='".$_POST['semester']."'";
    	if ($_POST['jurusan']=='all') {
    			$id_sms = '';
    		if ($_SESSION['level']!=1) {
				$id_sms = "id_prodi='".$_SESSION['id_sms']."'";
			}
	  	} else {
	    	$id_sms = "and id_prodi='".$_POST['jurusan']."'";
	  	}

  }

  
  if ($_POST['kode_mk']=='all') {
    $kode_mk = '';
  } else {
    $kode_mk = "and id_matkul='".$_POST['kode_mk']."'";
  }

  	$filter_kelas = "$semester $id_sms $kode_mk";


} else {
		if (isset($_POST['id_sms_user'])) {
			$filter_kelas = "id_prodi='".$_SESSION['id_sms']."'";
		} else {
			$filter_kelas = "";

		}
	
}
	
	if ($sSearch) {

	if ($filter_kelas!="") {
		$filter_kelas .= "and kode_mata_kuliah like '%".$sSearch."%'";
	} else {
		$filter_kelas .= "kode_mata_kuliah like '%".$sSearch."%'";
	
	}
}

$filter_kelas = trim($filter_kelas);



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

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


		
		$temp_data_kelas = [
			'act' => 'GetDetailKelasKuliah',
		    'token' => $token,
		    'filter' => $filter_kelas." and id_kelas_kuliah not in(select id_kls from komponen_evaluasi) and id_matkul in(select id_mk from re_mk)",
		    'order' => '',
		    //and id_matkul='8398a57d-b65f-41ca-ad12-9e713c243853'
		    'limit' => $temp_limit,
		    'offset' => $temp_offset

		];
		//dump($temp_data_kelas);
		$temp_rec = json_decode($db->run($temp_data_kelas,$url));
		//dump($temp_data_kelas);
		$has_evaluasi = array();
		$has_komponen = array();
		/*if (!empty($temp_rec->data)) {
				foreach ($temp_rec->data as $dt) {
				  $id_matkul[] = $dt->id_matkul;
				  $id_only_kelas[] = $dt->id_kelas_kuliah;
				}


				$in_id_kelas_mk = "'" . implode("','", $id_matkul) . "'";
				//jumlah pengajar
				$count_kelas = [
					'act' => 'GetListRencanaEvaluasi',
				    'token' => $token,
				    'filter' => "id_matkul in($in_id_kelas_mk)",
				];
				$temp_recs = json_decode($db->run($count_kelas,$url));
				if (!empty($temp_recs->data)) {
					foreach ($temp_recs->data as $dt) {
						$has_evaluasi[$dt->id_matkul] = $dt->id_matkul;
					}
				}

				$implode_kelas = "'" . implode("','", $id_only_kelas) . "'";
				$data_req_kelas_komponen = [
					'act' => 'GetListKomponenEvaluasiKelas',
				    'token' => $token,
				    'filter' => "id_kelas_kuliah in($implode_kelas)"
				];
				$check_kelas_komponen_exist = json_decode($db->run($data_req_kelas_komponen,$url));
				if (!empty($check_kelas_komponen_exist->data)) {
					foreach ($check_kelas_komponen_exist->data as $dts) {
						$has_komponen[$dts->id_kelas_kuliah] = $dts->id_kelas_kuliah;
					}
				}
		}*/



		$dosen_name = array();
		$jml_siswa = array();

		        $temp_count_mk = [
              'act' => 'GetCountKelasKuliah',
                'token' => $token,
                'filter' => $filter_kelas." and id_kelas_kuliah not in(select id_kls from komponen_evaluasi) and id_matkul in(select id_mk from re_mk)"
            ];
        $temp_count = json_decode($db->run($temp_count_mk,$url));
        $jumlah = 0;
       // dump($temp_count);
        if(!empty($temp_count->data)) {
          $jumlah = $temp_count->data;
        }

		//print_r($count_kelas);

		$totalData = $jumlah;
		$totalFiltered = $totalData;


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

				$temps = array();
				$checkbox = "";
				//if (in_array($key->id_matkul,$has_evaluasi)  && !in_array($key->id_kelas_kuliah, $has_komponen)) {
					$checkbox = '<label class="mt-checkbox mt-checkbox-single mt-checkbox-outline"> <input type="checkbox" class="group-checkable check-selected" value="'.$key->id_kelas_kuliah.'#'.$key->id_matkul.'"> <span></span></label>';
				//}
				$temps[] = $checkbox;
				$temps[] = $key->id_semester;
				$temps[] = $key->kode_mata_kuliah;
				$temps[] = $key->nama_mata_kuliah;
				$temps[] = $key->nama_kelas_kuliah;
				if (in_array($key->id_prodi, array_keys($dts_sms))) {	
					$temps[] = $dts_sms[$key->id_prodi];
				} else {
					$temps[] = '';
				}
				/*if (in_array($key->id_kelas_kuliah, $has_komponen)) {	
					$temps[] = "<span data-toggle='tooltip' data-title='Kelas ini sudah ada Komponen Evaluasi' class='btn btn-sm btn-success'><i class='fa fa-check'></i> Sudah</span>";
				} else {
					$temps[] = "<span data-toggle='tooltip' data-title='Kelas ini belum ada Komponen Evaluasi' class='btn btn-sm btn-warning'><i class='fa fa-times'></i> Belum</span>";
				}*/
				$button = '';
			//if (in_array($key->id_matkul,$has_evaluasi) && !in_array($key->id_kelas_kuliah, $has_komponen)) {
					$button = "<span data-id='".$key->id_matkul."' data-kelas='".$key->id_kelas_kuliah."' data-toggle='tooltip' data-title='Salin Evaluasi Matakuliah Ke kelas' class='btn btn-sm btn-primary salin-evaluasi btn-flat'><i class='fa fa-copy'></i> Salin Evaluasi</span>";
			//	}
				$temps[] = $button;
				
				$temp_data[] = $temps;
			}
			$temp_output = array(
									'draw' => intval($requestData['draw']),
									'recordsTotal' => intval( $totalData ),
									'recordsFiltered' => intval( $totalFiltered ),
									'data' => $temp_data
				);
			echo json_encode($temp_output);

