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

$data_jenis_mk = $db->fetch_custom("select * from jenis_mk");
$dts_jenis_mk = array();
foreach ($data_jenis_mk as $dts) {
	$dts_jenis_mk[$dts->id_jns_mk] = $dts->nm_jns_mk;
}

//$_SESSION['jurusan']
$id_sms = '';
$jns_mk = '';
$id_prodi = '';
$kurikulum = '';
$tampil_data = '';

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


  
  if ($_POST['jns_mk']!='all') {
    	$jns_mk = "and id_jenis_mata_kuliah='".$_POST['jns_mk']."'";
  	}
    if ($_POST['kurikulum']!='all') {
    	$kurikulum = "and id_matkul in(select id_mk from matkul_kurikulum where id_kurikulum_sp='".$_POST['kurikulum']."')";
  	}
    if ($_POST['tampil_data']!='all') {
    	if ($_POST['tampil_data']=='1') {
    		$tampil_data = "and id_matkul not in(select id_mk from matkul_kurikulum)";
    	} elseif ($_POST['tampil_data']=='2') {
    		$tampil_data = "and id_matkul not in(select id_mk from rp_mk)";
    	} elseif ($_POST['tampil_data']=='3') {
    		$tampil_data = "and id_matkul not in(select id_mk from re_mk)";
    	} elseif ($_POST['tampil_data']=='4') {
    		$tampil_data = "and id_matkul in(select id_mk from rp_mk)";
    	} elseif ($_POST['tampil_data']=='5') {
    		$tampil_data = "and id_matkul in(select id_mk from re_mk)";
    	}
  	}
  	$filter_kelas = "$id_sms $jns_mk";
  	$filter_kelas_prodi = "$id_prodi $jns_mk";


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

		}
	
}
	
	if ($sSearch) {

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

$filter_kelas = trim($filter_kelas);
		//and id_matkul not in(select id_kls from komponen_evaluasi)
		$temp_data_kelas = [
			'act' => 'ExportDataMatkulProdi',
		    'token' => $token,
		    'filter' => "id_matkul is not null $filter_kelas $kurikulum $tampil_data",
		    'order' => '',
		    //and id_matkul='8398a57d-b65f-41ca-ad12-9e713c243853'
		    'limit' => $temp_limit,
		    'offset' => $temp_offset

		];
		$temp_rec = json_decode($db->run($temp_data_kelas,$url));

		$has_evaluasi = array();
		$has_rencana = array();
		$has_kurikulum = array();
		if (!empty($temp_rec->data)) {
				foreach ($temp_rec->data as $dt) {
				  $id_matkul[] = $dt->id_matkul;
				  $id_only_kelas[] = $dt->id_matkul;
				}


				$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' => 'GetListRencanaPembelajaran',
				    'token' => $token,
				    'filter' => "id_matkul in($implode_kelas)"
				];
				$check_kelas_rencana_exist = json_decode($db->run($data_req_kelas_komponen,$url));
				if (!empty($check_kelas_rencana_exist->data)) {
					foreach ($check_kelas_rencana_exist->data as $dts) {
						$has_rencana[$dts->id_matkul] = $dts->id_matkul;
					}
				}

				//kurikulum
				$id_mk = "'" . implode("','", $id_matkul) . "'";
				//jumlah pengajar
				$count_kelas = [
					'act' => 'GetMatkulKurikulum',
				    'token' => $token,
				    'filter' => "id_matkul in($id_mk)",
				];
				$temp_recs = json_decode($db->run($count_kelas,$url));
				if (!empty($temp_recs->data)) {
					foreach ($temp_recs->data as $dt) {
						$has_kurikulum[$dt->id_matkul] = array('smt' => $dt->semester,'kurikulum' => $dt->nama_kurikulum);
					}
				}
		}



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

		        $temp_count_mk = [
              'act' => 'GetCountMataKuliah',
                'token' => $token,
                'filter' => "id_matkul is not null $filter_kelas_prodi $kurikulum $tampil_data"
            ];
        $temp_count = json_decode($db->run($temp_count_mk,$url));
        $jumlah = 0;
        //dump($temp_count_mk);
        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_matkul, $has_rencana)) {
					$checkbox = '<label class="mt-checkbox mt-checkbox-single mt-checkbox-outline"> <input type="checkbox" class="group-checkable check-selected" value="'.$key->id_matkul.'#'.$key->id_matkul.'"> <span></span></label>';
				}
				$temps[] = ++$i;
				$temps[] = $key->kode_mata_kuliah;
				$temps[] = $key->nama_mata_kuliah;
				$temps[] = $key->sks_mata_kuliah;
				if (in_array($key->id_jenis_mata_kuliah, array_keys($dts_jenis_mk))) {	
					$temps[] = $dts_jenis_mk[$key->id_jenis_mata_kuliah];
				} else {
					$temps[] = '';
				}

				if (in_array($key->id_matkul, $has_rencana)) {	
					$temps[] = "<span data-toggle='tooltip' data-title='Matakuliah ini sudah ada Rencana Pembelajaran' class='btn btn-sm btn-success'><i class='fa fa-check'></i> Sudah</span>";
				} else {
					$temps[] = "<span data-toggle='tooltip' data-title='Matakuliah ini belum ada Rencana Pembelajaran' class='btn btn-sm btn-warning'><i class='fa fa-times'></i> Belum</span>";
				}

				if (in_array($key->id_matkul, $has_evaluasi)) {	
					$temps[] = "<span data-toggle='tooltip' data-title='Matakuliah ini sudah ada Rencana Evaluasi' class='btn btn-sm btn-success'><i class='fa fa-check'></i> Sudah</span>";
				} else {
					$temps[] = "<span data-toggle='tooltip' data-title='Matakuliah ini belum ada Rencana Evaluasi' class='btn btn-sm btn-warning'><i class='fa fa-times'></i> Belum</span>";
				}

				if (in_array($key->id_matkul, array_keys($has_kurikulum))) {	
					$temps[] = $has_kurikulum[$key->id_matkul]['smt'];
				} else {
					$temps[] = '';
				}

				if (in_array($key->id_matkul, array_keys($has_kurikulum))) {	
					$temps[] = $has_kurikulum[$key->id_matkul]['kurikulum'];
				} else {
					$temps[] = "<span data-id='".$key->id_matkul."' data-toggle='tooltip' data-title='Tambah Matakuliah ke Kurikulum' class='btn btn-sm btn-primary add-kurikulum btn-flat'><i class='fa fa-plus'></i> Tambah</span>";
				}
				if (in_array($key->id_program_studi, array_keys($dts_sms))) {	
					$temps[] = $dts_sms[$key->id_program_studi];
				} else {
					$temps[] = '';
				}
				$button = '';
					$button = "<span data-id='".$key->id_matkul."' data-toggle='tooltip' data-title='Lihat Detail Matakuliah' class='btn btn-sm btn-success detail-matakuliah btn-flat'><i class='fa fa-eye'></i></span> <span data-id='".$key->id_matkul."' data-toggle='tooltip' data-title='Edit Matakuliah' class='btn btn-sm btn-primary edit-matakuliah btn-flat'><i class='fa fa-pencil'></i></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);

