<?php
include "../../inc/config.php";
  $url = get_service_url('url_rest');
  $token = $db->get_token();
switch ($_GET["act"]) {
  case 'edit_action':
$array_key = array(
                'id_registrasi_mahasiswa' => $_POST['id_registrasi_mahasiswa']
              );
              $array_data = array(
                  'id_mahasiswa' => $_POST['id_mahasiswa'],
                  'nim' => $_POST['nim'],
                  'id_jenis_daftar' => $_POST['id_jenis_daftar'],
                  'id_jalur_daftar' => $_POST['id_jalur_daftar'],
                  'id_periode_masuk' => $_POST['id_periode_masuk'],
                  'tanggal_daftar' => $_POST['tgl_mulai'],
                  'id_perguruan_tinggi' => $_POST['id_perguruan_tinggi'],
                  'id_prodi' => $_POST['fakultas_program_studi'],
                  'id_pembiayaan' => $_POST['pembiayaan_awal'],
                  'biaya_masuk' =>$_POST['biaya_masuk']
              );
              if ($_POST['id_jenis_daftar']!=1) {
                  $array_data["id_perguruan_tinggi_asal"] = $_POST["id_perguruan_tinggi_asal"];
                  $array_data["id_prodi_asal"]= $_POST["id_prodi_asal"];
              }


                $data_dic_update = [
                    'act' => 'UpdateRiwayatPendidikanMahasiswa',
                    'key' => $array_key,
                      'token' => $token,
                      'record' => $array_data,

                  ];
               

                $temp_recs = service_request($data_dic_update);

                $value_data = addslashes($temp_recs->error_desc);
                if ($value_data!='') {
                  $error_update_data[] = array(
                    'keterangan' => $value_data,
                    'status_error' => 2
                  );
                  $data_id_error[] = $value->id_akm;
                } else {
                  $array_reponse['status'] = 'good';
                  echo json_encode($array_reponse);
                }
    break;
  case 'massal':
    $config_feeder = $db->fetch_single_row('config_user','id',1);
    $token_nativ = get_token_native();
    $data = array();
    $id_mhs_reg = explode(",", $_POST['data_ids']);
    foreach ($id_mhs_reg as $reg) {
      $exp = explode("#", $reg);
      $id_mhs = $exp[0];
      $id_reg = $exp[1];
      if ($_POST['aksi']=='history') {
       $data_param = array(
            'act' => 'GetListMahasiswa',
            'filter' => "id_mahasiswa='".$id_mhs."'"
        );

        $check_nim_exist = $db->get_data_service($data_param);
        if (!empty($check_nim_exist)) {
            if ($check_nim_exist->nim!='') {
              $id_reg_pd = $check_nim_exist->id_registrasi_mahasiswa;
              //hapus nilai
              $data_dic = [
                    'act' => 'GetDetailNilaiPerkuliahanKelas',
                      'token' => $token,
                      'filter' => "id_registrasi_mahasiswa='".$id_reg_pd."'",
                      'order' => '',
                      'limit' => '',
                      'offset' => ''
                  ];
              $temp_nilai = json_decode($db->run($data_dic,$url));
              if(!empty($temp_nilai->data)) {
                  foreach ($temp_nilai->data as $dt_nilai) {
                    $data_hapus = [
                            'act' => 'DeletePesertaKelasKuliah',
                            'token' => $token,
                            'key' => array(
                              'id_kelas_kuliah' => $dt_nilai->id_kelas_kuliah,
                              'id_registrasi_mahasiswa' => $id_reg_pd
                            )
                        ];
                    $temp_hapus = json_decode($db->run($data_hapus,$url));
                    dump($temp_hapus);
                  }

              }

              //hapus nilai Transfer
              $data_dic = [
                    'act' => 'GetNilaiTransferPendidikanMahasiswa',
                      'token' => $token,
                      'filter' => "id_registrasi_mahasiswa='".$id_reg_pd."'",
                      'order' => '',
                      'limit' => '',
                      'offset' => ''
                  ];
              $temp_nilaitrf = json_decode($db->run($data_dic,$url));
              if(!empty($temp_nilaitrf->data)) {
                foreach ($temp_nilaitrf->data as $dt_nilaitrf) {
                  $data_hapus = [
                          'act' => 'DeleteNilaiTransferPendidikanMahasiswa',
                          'token' => $token,
                          'key' => array(
                            'id_transfer' => $dt_nilaitrf->id_transfer
                          )
                      ];
                  $temp_hapus = json_decode($db->run($data_hapus,$url));
                  dump($temp_hapus);
                }

              }
              //hapus akm
              $data_dic = [
                    'act' => 'GetAktivitasKuliahMahasiswa',
                      'token' => $token,
                      'filter' => "id_registrasi_mahasiswa='".$id_reg_pd."'",
                      'order' => '',
                      'limit' => '',
                      'offset' => ''
                  ];
              $temp_akm = json_decode($db->run($data_dic,$url));
              if(!empty($temp_akm->data)) {
                foreach ($temp_akm->data as $temp_akm) {
                  $data_hapus = [
                          'act' => 'DeletePerkuliahanMahasiswa',
                          'token' => $token,
                          'key' => array(
                            'id_registrasi_mahasiswa' => $temp_akm->id_registrasi_mahasiswa,
                            'id_semester' => $temp_akm->id_semester
                          )
                      ];
                  $temp_hapus = json_decode($db->run($data_hapus,$url));
                  dump($temp_hapus);
                }
              }
              //hapus Anggota Aktivitas
              $data_dic = [
                    'act' => 'GetListAnggotaAktivitasMahasiswa',
                      'token' => $token,
                      'filter' => "id_registrasi_mahasiswa='".$id_reg_pd."'",
                      'order' => '',
                      'limit' => '',
                      'offset' => ''
                  ];
              $temp_anggota = json_decode($db->run($data_dic,$url));
              if(!empty($temp_anggota->data)) {
                foreach ($temp_anggota->data as $dt_anggota) {
                  $data_hapus = [
                          'act' => 'DeleteAnggotaAktivitasKampusMerdeka',
                          'token' => $token,
                          'key' => array(
                            'id_anggota' => $dt_anggota->id_anggota
                          )
                      ];
                  $temp_hapus = json_decode($db->run($data_hapus,$url));
                  dump($temp_hapus);

                  $data_hapus = [
                          'act' => 'DeleteAnggotaAktivitasMahasiswa',
                          'token' => $token,
                          'key' => array(
                            'id_anggota' => $dt_anggota->id_anggota
                          )
                      ];
                  $temp_hapus = json_decode($db->run($data_hapus,$url));
                  dump($temp_hapus);
                }
              }
              
              //DeletePrestasiMahasiswa
              $data_dic = [
                    'act' => 'GetListPrestasiMahasiswa',
                      'token' => $token,
                      'filter' => "id_mahasiswa='".$id_mhs."'",
                      'order' => '',
                      'limit' => '',
                      'offset' => ''
                  ];
              $temp_prestasi = json_decode($db->run($data_dic,$url));
              if(!empty($temp_prestasi->data)) {
                foreach ($temp_prestasi->data as $dt_prestasi) {
                  $data_hapus = [
                          'act' => 'DeletePrestasiMahasiswa',
                          'token' => $token,
                          'key' => array(
                            'id_prestasi' => $dt_prestasi->id_prestasi
                          )
                      ];
                  $temp_hapus = json_decode($db->run($data_hapus,$url));
                  dump($temp_hapus);
                }
              }
              //delete riwayat
              $data_hapus = [
                      'act' => 'DeleteRiwayatPendidikanMahasiswa',
                      'token' => $token,
                      'key' => array(
                        'id_registrasi_mahasiswa' => $id_reg_pd
                      )
                  ];
              $temp_hapus = json_decode($db->run($data_hapus,$url));
              dump($temp_hapus);
            }
        }
      } else {
        $data_param = array(
            'act' => 'GetListMahasiswa',
            'filter' => "id_mahasiswa='".$id_mhs."'"
        );

        $check_nim_exist = $db->get_data_service($data_param);
        if (!empty($check_nim_exist)) {
            if ($check_nim_exist->nim=='') {
            $data_hapus = [
                    'act' => 'DeleteBiodataMahasiswa',
                    'token' => $token,
                    'key' => array('id_mahasiswa' => $id_mhs)
                ];
            $temp_hapus = json_decode($db->run($data_hapus,$url));
            dump($temp_hapus);
              
            } else {
              $id_reg_pd = $check_nim_exist->id_registrasi_mahasiswa;
              //hapus nilai
              $data_dic = [
                    'act' => 'GetDetailNilaiPerkuliahanKelas',
                      'token' => $token,
                      'filter' => "id_registrasi_mahasiswa='".$id_reg_pd."'",
                      'order' => '',
                      'limit' => '',
                      'offset' => ''
                  ];
              $temp_nilai = json_decode($db->run($data_dic,$url));
              if(!empty($temp_nilai->data)) {
                  foreach ($temp_nilai->data as $dt_nilai) {
                    $data_hapus = [
                            'act' => 'DeletePesertaKelasKuliah',
                            'token' => $token,
                            'key' => array(
                              'id_kelas_kuliah' => $dt_nilai->id_kelas_kuliah,
                              'id_registrasi_mahasiswa' => $id_reg_pd
                            )
                        ];
                    $temp_hapus = json_decode($db->run($data_hapus,$url));
                    dump($temp_hapus);
                  }

              }

              //hapus nilai Transfer
              $data_dic = [
                    'act' => 'GetNilaiTransferPendidikanMahasiswa',
                      'token' => $token,
                      'filter' => "id_registrasi_mahasiswa='".$id_reg_pd."'",
                      'order' => '',
                      'limit' => '',
                      'offset' => ''
                  ];
              $temp_nilaitrf = json_decode($db->run($data_dic,$url));
              if(!empty($temp_nilaitrf->data)) {
                foreach ($temp_nilaitrf->data as $dt_nilaitrf) {
                  $data_hapus = [
                          'act' => 'DeleteNilaiTransferPendidikanMahasiswa',
                          'token' => $token,
                          'key' => array(
                            'id_transfer' => $dt_nilaitrf->id_transfer
                          )
                      ];
                  $temp_hapus = json_decode($db->run($data_hapus,$url));
                  dump($temp_hapus);
                }

              }
              //hapus akm
              $data_dic = [
                    'act' => 'GetAktivitasKuliahMahasiswa',
                      'token' => $token,
                      'filter' => "id_registrasi_mahasiswa='".$id_reg_pd."'",
                      'order' => '',
                      'limit' => '',
                      'offset' => ''
                  ];
              $temp_akm = json_decode($db->run($data_dic,$url));
              if(!empty($temp_akm->data)) {
                foreach ($temp_akm->data as $temp_akm) {
                  $data_hapus = [
                          'act' => 'DeletePerkuliahanMahasiswa',
                          'token' => $token,
                          'key' => array(
                            'id_registrasi_mahasiswa' => $temp_akm->id_registrasi_mahasiswa,
                            'id_semester' => $temp_akm->id_semester
                          )
                      ];
                  $temp_hapus = json_decode($db->run($data_hapus,$url));
                  dump($temp_hapus);
                }
              }
              //hapus Anggota Aktivitas
              $data_dic = [
                    'act' => 'GetListAnggotaAktivitasMahasiswa',
                      'token' => $token,
                      'filter' => "id_registrasi_mahasiswa='".$id_reg_pd."'",
                      'order' => '',
                      'limit' => '',
                      'offset' => ''
                  ];
              $temp_anggota = json_decode($db->run($data_dic,$url));
              if(!empty($temp_anggota->data)) {
                foreach ($temp_anggota->data as $dt_anggota) {
                  $data_hapus = [
                          'act' => 'DeleteAnggotaAktivitasKampusMerdeka',
                          'token' => $token,
                          'key' => array(
                            'id_anggota' => $dt_anggota->id_anggota
                          )
                      ];
                  $temp_hapus = json_decode($db->run($data_hapus,$url));
                  dump($temp_hapus);

                  $data_hapus = [
                          'act' => 'DeleteAnggotaAktivitasMahasiswa',
                          'token' => $token,
                          'key' => array(
                            'id_anggota' => $dt_anggota->id_anggota
                          )
                      ];
                  $temp_hapus = json_decode($db->run($data_hapus,$url));
                  dump($temp_hapus);
                }
              }
              
              //DeletePrestasiMahasiswa
              $data_dic = [
                    'act' => 'GetListPrestasiMahasiswa',
                      'token' => $token,
                      'filter' => "id_mahasiswa='".$id_mhs."'",
                      'order' => '',
                      'limit' => '',
                      'offset' => ''
                  ];
              $temp_prestasi = json_decode($db->run($data_dic,$url));
              if(!empty($temp_prestasi->data)) {
                foreach ($temp_prestasi->data as $dt_prestasi) {
                  $data_hapus = [
                          'act' => 'DeletePrestasiMahasiswa',
                          'token' => $token,
                          'key' => array(
                            'id_prestasi' => $dt_prestasi->id_prestasi
                          )
                      ];
                  $temp_hapus = json_decode($db->run($data_hapus,$url));
                  dump($temp_hapus);
                }
              }
              //delete riwayat
              $data_hapus = [
                      'act' => 'DeleteRiwayatPendidikanMahasiswa',
                      'token' => $token,
                      'key' => array(
                        'id_registrasi_mahasiswa' => $id_reg_pd
                      )
                  ];
              $temp_hapus = json_decode($db->run($data_hapus,$url));
              dump($temp_hapus);
              //delete biodata
              $data_hapus = [
                      'act' => 'DeleteBiodataMahasiswa',
                      'token' => $token,
                      'key' => array(
                        'id_mahasiswa' => $id_mhs
                      )
                  ];
              $temp_hapus = json_decode($db->run($data_hapus,$url));
              dump($temp_hapus);

            }

        }
      }
    }
    //dump($id_mhs_reg);
    break;
  case "delete":
        $data_param = array(
            'act' => 'GetListMahasiswa',
            'filter' => "id_mahasiswa='".$_GET['id']."'"
        );

        $check_nim_exist = $db->get_data_service($data_param);
        if (!empty($check_nim_exist)) {
            if ($check_nim_exist->nim=='') {
            $data_hapus = [
                    'act' => 'DeleteBiodataMahasiswa',
                    'token' => $token,
                    'key' => array('id_mahasiswa' => $_GET['id'])
                ];
            $temp_hapus = json_decode($db->run($data_hapus,$url));
            dump($temp_hapus);
              
            } else {
              $id_reg_pd = $check_nim_exist->id_registrasi_mahasiswa;
              //hapus nilai
              $data_dic = [
                    'act' => 'GetDetailNilaiPerkuliahanKelas',
                      'token' => $token,
                      'filter' => "id_registrasi_mahasiswa='".$id_reg_pd."'",
                      'order' => '',
                      'limit' => '',
                      'offset' => ''
                  ];
              $temp_nilai = json_decode($db->run($data_dic,$url));
              if(!empty($temp_nilai->data)) {
                  foreach ($temp_nilai->data as $dt_nilai) {
                    $data_hapus = [
                            'act' => 'DeletePesertaKelasKuliah',
                            'token' => $token,
                            'key' => array(
                              'id_kelas_kuliah' => $dt_nilai->id_kelas_kuliah,
                              'id_registrasi_mahasiswa' => $id_reg_pd
                            )
                        ];
                    $temp_hapus = json_decode($db->run($data_hapus,$url));
                    dump($temp_hapus);
                  }

              }

              //hapus nilai Transfer
              $data_dic = [
                    'act' => 'GetNilaiTransferPendidikanMahasiswa',
                      'token' => $token,
                      'filter' => "id_registrasi_mahasiswa='".$id_reg_pd."'",
                      'order' => '',
                      'limit' => '',
                      'offset' => ''
                  ];
              $temp_nilaitrf = json_decode($db->run($data_dic,$url));
              if(!empty($temp_nilaitrf->data)) {
                foreach ($temp_nilaitrf->data as $dt_nilaitrf) {
                  $data_hapus = [
                          'act' => 'DeleteNilaiTransferPendidikanMahasiswa',
                          'token' => $token,
                          'key' => array(
                            'id_transfer' => $dt_nilaitrf->id_transfer
                          )
                      ];
                  $temp_hapus = json_decode($db->run($data_hapus,$url));
                  dump($temp_hapus);
                }

              }
              //hapus akm
              $data_dic = [
                    'act' => 'GetAktivitasKuliahMahasiswa',
                      'token' => $token,
                      'filter' => "id_registrasi_mahasiswa='".$id_reg_pd."'",
                      'order' => '',
                      'limit' => '',
                      'offset' => ''
                  ];
              $temp_akm = json_decode($db->run($data_dic,$url));
              if(!empty($temp_akm->data)) {
                foreach ($temp_akm->data as $temp_akm) {
                  $data_hapus = [
                          'act' => 'DeletePerkuliahanMahasiswa',
                          'token' => $token,
                          'key' => array(
                            'id_registrasi_mahasiswa' => $temp_akm->id_registrasi_mahasiswa,
                            'id_semester' => $temp_akm->id_semester
                          )
                      ];
                  $temp_hapus = json_decode($db->run($data_hapus,$url));
                  dump($temp_hapus);
                }
              }
              //hapus Anggota Aktivitas
              $data_dic = [
                    'act' => 'GetListAnggotaAktivitasMahasiswa',
                      'token' => $token,
                      'filter' => "id_registrasi_mahasiswa='".$id_reg_pd."'",
                      'order' => '',
                      'limit' => '',
                      'offset' => ''
                  ];
              $temp_anggota = json_decode($db->run($data_dic,$url));
              if(!empty($temp_anggota->data)) {
                foreach ($temp_anggota->data as $dt_anggota) {
                  $data_hapus = [
                          'act' => 'DeleteAnggotaAktivitasKampusMerdeka',
                          'token' => $token,
                          'key' => array(
                            'id_anggota' => $dt_anggota->id_anggota
                          )
                      ];
                  $temp_hapus = json_decode($db->run($data_hapus,$url));
                  dump($temp_hapus);

                  $data_hapus = [
                          'act' => 'DeleteAnggotaAktivitasMahasiswa',
                          'token' => $token,
                          'key' => array(
                            'id_anggota' => $dt_anggota->id_anggota
                          )
                      ];
                  $temp_hapus = json_decode($db->run($data_hapus,$url));
                  dump($temp_hapus);
                }
              }
              
              //DeletePrestasiMahasiswa
              $data_dic = [
                    'act' => 'GetListPrestasiMahasiswa',
                      'token' => $token,
                      'filter' => "id_mahasiswa='".$_GET['id']."'",
                      'order' => '',
                      'limit' => '',
                      'offset' => ''
                  ];
              $temp_prestasi = json_decode($db->run($data_dic,$url));
              if(!empty($temp_prestasi->data)) {
                foreach ($temp_prestasi->data as $dt_prestasi) {
                  $data_hapus = [
                          'act' => 'DeletePrestasiMahasiswa',
                          'token' => $token,
                          'key' => array(
                            'id_prestasi' => $dt_prestasi->id_prestasi
                          )
                      ];
                  $temp_hapus = json_decode($db->run($data_hapus,$url));
                  dump($temp_hapus);
                }
              }
              //delete riwayat
              $data_hapus = [
                      'act' => 'DeleteRiwayatPendidikanMahasiswa',
                      'token' => $token,
                      'key' => array(
                        'id_registrasi_mahasiswa' => $id_reg_pd
                      )
                  ];
              $temp_hapus = json_decode($db->run($data_hapus,$url));
              dump($temp_hapus);
              //delete biodata
              $data_hapus = [
                      'act' => 'DeleteBiodataMahasiswa',
                      'token' => $token,
                      'key' => array(
                        'id_mahasiswa' => $_GET['id']
                      )
                  ];
              $temp_hapus = json_decode($db->run($data_hapus,$url));
              dump($temp_hapus);

            }

        }

  break;
}