<?php
session_start();
include "../../inc/config.php";
session_check();
switch ($_GET["act"]) {
  case 'save_sks':
    //dump($_POST);
    $array_reponse = array();
    $token = get_token();
    //check periode is aktif
  $req_periode = [
    'act' => 'GetPeriode',
      'token' => $token,
      'filter' => "id_prodi='".$_POST['id_prodi']."' and periode_pelaporan='".$_POST['id_semester']."'"
  ];

  $get_periode = service_request($req_periode);
  $jml_total = 0;
  foreach ($_POST['sks'] as $key => $value) {
    $jml_total+=(float)$value;
    $sks_dosen[$key] = $value;
  }

  $sks_total = (float)$_POST['sks_total'];

 //dump($jml_total);
  if (empty($get_periode->data)) {
    $array_reponse['status'] = 'bad';
    $array_reponse['error_desc'] = 'Tidak bisa merubah data, karena diluar periode aktif';
  } elseif ($jml_total!==$sks_total) {
    $array_reponse['status'] = 'bad';
    $array_reponse['error_desc'] = 'Jumlah Total SKS dosen harus '.$sks_total.' SKS';
  } else {

    $data_req_dosen = [
      'act' => 'GetDosenPengajarKelasKuliah',
        'token' => $token,
        'filter' => "id_kelas_kuliah='".$_POST['id_kelas']."'"
    ];

    $get_dosen = service_request($data_req_dosen);

    $iterate = 0;
    $sukses = 0;
    foreach ($get_dosen->data as $dos) {
      $array_data = array(
        'id_registrasi_dosen' => $dos->id_registrasi_dosen,
        'id_kelas_kuliah' => $dos->id_kelas_kuliah,
        'rencana_minggu_pertemuan' => $_POST['rencana'][$dos->id_aktivitas_mengajar],
        'sks_substansi_total' => $sks_dosen[$dos->id_aktivitas_mengajar],
        'id_jenis_evaluasi' => $dos->id_jenis_evaluasi
      );
      $data_dic_update = [
          'act' => 'UpdateDosenPengajarKelasKuliah',
          'key' => array('id_aktivitas_mengajar' => $dos->id_aktivitas_mengajar),
            'token' => $token,
            'record' => $array_data,

        ];
      $temp_recs = service_request($data_dic_update);
      if ($temp_recs->error_desc=="") {
        $sukses++;
      }
        $error_desc[] = $temp_recs->error_desc;
         $iterate++;
      //dump($data_dic_update);
      //dump($temp_recs);
    }
    if ($sukses==$iterate) {
      $array_reponse['status'] = 'good';
    } else {
      $array_reponse['status'] = 'good';
      $array_reponse['error_desc'] = implode("<br>", $error_desc);
    }

  }


    echo json_encode($array_reponse);
    break;
  case "in":
  
  
  
  $data = array("nm_agama"=>$_POST["nm_agama"],);
  
  
  
   
    $in = $db->insert("agama",$data);
    
    if ($in=true) {
      echo "good";
    } else {
      return false;
    }
    break;
  case "delete":
    
    
    
    $db->delete("agama","id_agama",$_GET["id"]);
    break;
  case "up":
   $data = array("nm_agama"=>$_POST["nm_agama"],);
   
   
   

    
    $up = $db->update("agama",$data,"id_agama",$_POST["id"]);
    if ($up=true) {
      echo "good";
    } else {
      return false; 
    }
    break;
  default:
    # code...
    break;
}

?>