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

switch ($_GET["act"]) {
  case 'delete_aktivitas':
    if ($_GET['jenis']=='anggota') {
      $key['id_anggota'] = $_GET['id'];
       $data_param = array(
              'act' => 'DeleteAnggotaAktivitasMahasiswa',
              'token' => $token,
              'key' => $key
          );
        $temp_recs = json_decode($db->run($data_param,$url));
        if ($temp_recs->error_code==0) {
          echo "good";
        } else {
          $temp_recs->error_desc;
        }
    } elseif ($_GET['jenis']=='pembimbing') {
      $key['id_bimbing_mahasiswa'] = $_GET['id'];
       $data_param = array(
              'act' => 'DeleteBimbingMahasiswa',
              'token' => $token,
              'key' => $key
          );
        $temp_recs = json_decode($db->run($data_param,$url));
        if ($temp_recs->error_code==0) {
          echo "good";
        } else {
          $temp_recs->error_desc;
        }
    } elseif ($_GET['jenis']=='penguji') {
      $key['id_uji'] = $_GET['id'];
       $data_param = array(
              'act' => 'DeleteUjiMahasiswa',
              'token' => $token,
              'key' => $key
          );
        $temp_recs = json_decode($db->run($data_param,$url));
        if ($temp_recs->error_code==0) {
          echo "good";
        } else {
          $temp_recs->error_desc;
        }
    }
    break;
  case 'delete_all':
  $id_sms = "";
  $jenis = "";
  if ($_POST['jurusan']!='all') {
       $id_sms = "and id_prodi='".$_POST['jurusan']."'";
  } 
  if ($_POST['jenis']!='all') {
      $jenis = "and id_jenis_aktivitas='".$_POST['jenis']."'";
  }

   
  $filter_semester = "id_semester='".$_POST['sem']."' $id_sms $jenis";

  $data_param = array(
            'act' => 'GetListAktivitasMahasiswa',
            'token' => $token,
            'filter' => $filter_semester,

        );

$temp_recs = json_decode($db->run($data_param,$url));

  if ($temp_recs->data) {
    
 
      foreach($temp_recs->data as $id) {
        $id_kls = $id->id_aktivitas;

     $data_param = array(
            'act' => 'GetListAnggotaAktivitasMahasiswa',
            'token' => $token,
            'filter' => "id_aktivitas='$id_kls'",

        );

$temp_recs = json_decode($db->run($data_param,$url));
print_r($temp_recs);


    if (!empty($temp_recs->data)) {
       foreach ($temp_recs->data as $reg) {
        $hapus_anggota[] = array(
        'id_ang_akt_mhs' => $reg->id_anggota,
        );
       }
       $temp_res = $proxy->DeleteRecordset($token, 'anggota_aktivitas_mahasiswa', json_encode($hapus_anggota));
        print_r($temp_res);
    }


  //hapus pembimbing
  $data_param = array(
            'act' => 'GetListBimbingMahasiswa',
            'token' => $token,
            'filter' => "id_aktivitas='$id_kls'",

        );

$temp_recs = json_decode($db->run($data_param,$url));
print_r($temp_recs);


    if (!empty($temp_recs->data)) {
       foreach ($temp_recs->data as $reg) {
        $hapus_anggota[] = array(
        'id_bimb_mhs' => $reg->id_bimbing_mahasiswa,
        );
       }
       $temp_res = $proxy->DeleteRecordset($token, 'bimbing_mahasiswa', json_encode($hapus_anggota));
        print_r($temp_res);
    }


  //hapus penguji
  $data_param = array(
            'act' => 'GetListUjiMahasiswa',
            'token' => $token,
            'filter' => "id_aktivitas='$id_kls'",

        );

$temp_recs = json_decode($db->run($data_param,$url));
print_r($temp_recs);


    if (!empty($temp_recs->data)) {
       foreach ($temp_recs->data as $reg) {
        $hapus_anggota[] = array(
        'id_uji_mhs' => $reg->id_uji,
        );
       }
       $temp_res = $proxy->DeleteRecordset($token, 'uji_mahasiswa', json_encode($hapus_anggota));
        print_r($temp_res);
    }
//hapus aktivitas
  $data_param = array(
            'act' => 'DeleteAktivitasMahasiswa',
            'token' => $token,
            'key' => array('id_aktivitas' => $id_kls),

        );

$temp_recs = json_decode($db->run($data_param,$url));
print_r($temp_recs);

  

      }
 }

    break;
  case "delete":
  $ids = $_GET["id"];
  //hapus anggota aktivitas
    if ($_GET['jenis']=='bimbing') {
      $table = "DeleteBimbingMahasiswa";
      $hapus_dosen = array(
        'id_bimbing_mahasiswa' => $ids,
        );
    } else {
      $table = "DeleteUjiMahasiswa";
        $hapus_dosen = array(
        'id_uji' => $ids,
        );
    }

          $data_param = array(
                    'act' => $table,
                    'token' => $token,
                    'key' => $hapus_dosen
                );
          $hapus_data_pembimbing = service_request($data_param);
          dump($hapus_data_pembimbing);
  break;

  case 'del_massal':
     $data_ids = $_REQUEST['data_ids'];
    $data_id_array = explode(",", $data_ids);
    if(!empty($data_id_array)) {

      foreach($data_id_array as $ids) {

  //hapus anggota aktivitas
    if ($_REQUEST['jenis']=='bimbing') {
      $table = "DeleteBimbingMahasiswa";
      $hapus_dosen = array(
        'id_bimbing_mahasiswa' => $ids,
        );
    } else {
      $table = "DeleteUjiMahasiswa";
        $hapus_dosen = array(
        'id_uji' => $ids,
        );
    }

          $data_param = array(
                    'act' => $table,
                    'token' => $token,
                    'key' => $hapus_dosen
                );
          $hapus_data_pembimbing = service_request($data_param);
          dump($hapus_data_pembimbing);

    }

  }
    break;

}