<?php
session_start();

include "../../inc/config.php";

$token = get_token();
$url = $db->get_service_url('rest');

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":
    $id_akt = $_GET["id"];
    //hapus anggota aktivitas
    $data_param = array(
              'act' => 'GetListAnggotaAktivitasMahasiswa',
              'token' => $token,
              'filter' => "id_aktivitas='$id_akt'",
          );
    $data_anggota = service_request($data_param);

    if (!empty($data_anggota->data)) {
       foreach ($data_anggota->data as $agt) {
          $data_param = array(
                    'act' => 'DeleteAnggotaAktivitasMahasiswa',
                    'token' => $token,
                    'key' => array('id_anggota' => $agt->id_anggota)
                );
          $hapus_data_anggota = service_request($data_param);
          dump($hapus_data_anggota);
       }
    }

    //delete pembimbing
    $data_param = array(
              'act' => 'GetListBimbingMahasiswa',
              'token' => $token,
              'filter' => "id_aktivitas='$id_akt'",
          );
    $data_pembimbing = service_request($data_param);

    if (!empty($data_pembimbing->data)) {
       foreach ($data_pembimbing->data as $pembimbing) {
          $data_param = array(
                    'act' => 'DeleteBimbingMahasiswa',
                    'token' => $token,
                    'key' => array('id_bimbing_mahasiswa' => $pembimbing->id_bimbing_mahasiswa)
                );
          $hapus_data_pembimbing = service_request($data_param);
          dump($hapus_data_pembimbing);
       }
    }

      //hapus penguji
    $data_param = array(
              'act' => 'GetListUjiMahasiswa',
              'token' => $token,
              'filter' => "id_aktivitas='$id_akt'",
          );
    $data_penguji = service_request($data_param);

    if (!empty($data_penguji->data)) {
       foreach ($data_penguji->data as $penguji) {
          $data_param = array(
                    'act' => 'DeleteUjiMahasiswa',
                    'token' => $token,
                    'key' => array('id_uji' => $penguji->id_uji)
                );
          $hapus_data_penguji = service_request($data_param);
          dump($hapus_data_penguji);
       }
    }

        //hapus aktivitas
        $data_param = array(
            'act' => 'DeleteAktivitasMahasiswa',
            'token' => $token,
            'key' => array('id_aktivitas' => $id_akt),
        );
        $hapus_aktivitas = service_request($data_param);
        dump($hapus_aktivitas);

  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 $id_akt) {

    //hapus anggota aktivitas
    $data_param = array(
              'act' => 'GetListAnggotaAktivitasMahasiswa',
              'token' => $token,
              'filter' => "id_aktivitas='$id_akt'",
          );
    $data_anggota = service_request($data_param);

    if (!empty($data_anggota->data)) {
       foreach ($data_anggota->data as $agt) {
          $data_param = array(
                    'act' => 'DeleteAnggotaAktivitasMahasiswa',
                    'token' => $token,
                    'key' => array('id_anggota' => $agt->id_anggota)
                );
          $hapus_data_anggota = service_request($data_param);
          dump($hapus_data_anggota);
       }
    }

    //delete pembimbing
    $data_param = array(
              'act' => 'GetListBimbingMahasiswa',
              'token' => $token,
              'filter' => "id_aktivitas='$id_akt'",
          );
    $data_pembimbing = service_request($data_param);

    if (!empty($data_pembimbing->data)) {
       foreach ($data_pembimbing->data as $pembimbing) {
          $data_param = array(
                    'act' => 'DeleteBimbingMahasiswa',
                    'token' => $token,
                    'key' => array('id_bimbing_mahasiswa' => $pembimbing->id_bimbing_mahasiswa)
                );
          $hapus_data_pembimbing = service_request($data_param);
          dump($hapus_data_pembimbing);
       }
    }

      //hapus penguji
    $data_param = array(
              'act' => 'GetListUjiMahasiswa',
              'token' => $token,
              'filter' => "id_aktivitas='$id_akt'",
          );
    $data_penguji = service_request($data_param);

    if (!empty($data_penguji->data)) {
       foreach ($data_penguji->data as $penguji) {
          $data_param = array(
                    'act' => 'DeleteUjiMahasiswa',
                    'token' => $token,
                    'key' => array('id_uji' => $penguji->id_uji)
                );
          $hapus_data_penguji = service_request($data_param);
          dump($hapus_data_penguji);
       }
    }

        //hapus aktivitas
        $data_param = array(
            'act' => 'DeleteAktivitasMahasiswa',
            'token' => $token,
            'key' => array('id_aktivitas' => $id_akt),
        );
        $hapus_aktivitas = service_request($data_param);
        dump($hapus_aktivitas);

      }

    }
    break;

}