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

$url_rest = $db->get_service_url('rest');

  # MENDAPATKAN TOKEN
  $token = $db->get_token();



switch ($_GET["act"]) {
  case 'delete_all':

   $prodi = $_POST['jurusan'];
    //get id npsn
    $id_sp = $config->id_sp;

    $filter_sms = "id_sp='".$id_sp."' and trim(kode_prodi)='".trim($prodi)."'";
    $temp_sms = $proxy->GetRecord($token,'sms',$filter_sms);
    if ($temp_sms['result']) {
      $id_sms = $temp_sms['result']['id_sms'];
    }
 
    $jenis_keluar = $_POST['jenis_keluar'];
    $tahun_lulus = $_POST['tahun_lulus'];
    if ($jenis_keluar=='all') {
        $jenis_keluar = "";
    } else {
        $jenis_keluar = "and p.id_jns_keluar='".$jenis_keluar."'";
    }

  $filter_semester = "p.id_sms='".$id_sms."' and EXTRACT(YEAR FROM tgl_keluar)='".$_POST['tahun_lulus']."' $jenis_keluar";

  
  $all_kelas = $proxy->GetRecordset($token,'mahasiswa_pt',$filter_semester,'','','');

  if (!empty($all_kelas['result'])) {
      
      foreach($all_kelas['result'] as $id) {

        $filter_mk = "id_reg_pd='".$id['id_reg_pd']."'";
        $order_by = "";
        $temp_data = $proxy->GetRecordset($token,'dosen_pembimbing',$filter_mk,$order_by, "","");

        if (!empty($temp_data['result'])) {
            foreach ($temp_data['result'] as $value) {
                  $temp_keys[] = array(
                        'id_sdm' => $value['id_sdm'],
                        'id_reg_pd' => $id['id_reg_pd']
                     );
            }
       
        }


        $temp_key = array(
            'id_reg_pd' => $id['id_reg_pd']
        );

        $temp_data = array(
            'id_jns_keluar' => '',
            'tgl_keluar' => '',
            'judul_skripsi' => '',
            'jalur_skripsi' => '',
            'bln_awal_bimbingan' => '',
            'bln_akhir_bimbingan' => '',
            'sk_yudisium' => '',
            'tgl_sk_yudisium' => '',
            'ipk' => '',
            'no_seri_ijazah' => ''
        );
         $array_mhs[] = array('key'=>$temp_key,'data'=>$temp_data);

      }

         $temp_result = $proxy->DeleteRecordset($token, 'dosen_pembimbing', json_encode($temp_keys));
         print_r($temp_result);

       
        $temp_result = $proxy->UpdateRecordset($token, 'mahasiswa_pt', json_encode($array_mhs));
        print_r($temp_result);

  }
    break;
  case "delete":

/*                $filter_mk = "id_reg_pd='".$_GET['id']."'";
                $order_by = "";
                $temp_data = $proxy->GetRecordset($token,'dosen_pembimbing',$filter_mk,$order_by, "","");

                if (!empty($temp_data['result'])) {
                    foreach ($temp_data['result'] as $value) {
                          $temp_key[] = array(
                                'id_sdm' => $value['id_sdm'],
                                'id_reg_pd' => $_GET['id']
                             );
                    }
                 $temp_result = $proxy->DeleteRecordset($token, 'dosen_pembimbing', json_encode($temp_key));
                 print_r($temp_result);
                }*/
                
                $data_param = array(
                            'act' => 'DeleteMahasiswaLulusDO',
                            'token' => $token,
                            'key' => array('id_registrasi_mahasiswa' => $_GET['id'])
                          );

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


/*                 $temp_keys = array(
                                'id_reg_pd' => $_GET['id']
                             );

                 
                $temp_data = array(
                    'id_jns_keluar' => '',
                    'tgl_keluar' => '',
                    'judul_skripsi' => '',
                    'jalur_skripsi' => '',
                    'bln_awal_bimbingan' => '',
                    'bln_akhir_bimbingan' => '',
                    'sk_yudisium' => '',
                    'tgl_sk_yudisium' => '',
                    'ipk' => '',
                    'no_seri_ijazah' => ''
                );

                
                $array_mhs[] = array('key'=>$temp_keys,'data'=>$temp_data);
               
                $temp_result = $proxy->UpdateRecordset($token, 'mahasiswa_pt', json_encode($array_mhs));
                print_r($temp_result);*/

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

/*        $filter_mk = "id_reg_pd='".$id."'";
        $order_by = "";
        $temp_data = $proxy->GetRecordset($token,'dosen_pembimbing',$filter_mk,$order_by, "","");

        if (!empty($temp_data['result'])) {
            foreach ($temp_data['result'] as $value) {
                  $temp_keys[] = array(
                        'id_sdm' => $value['id_sdm'],
                        'id_reg_pd' => $id
                     );
            }
       
        }*/


/*        $temp_key = array(
            'id_reg_pd' => $id
        );

        $temp_data = array(
            'id_jns_keluar' => '',
            'tgl_keluar' => '',
            'judul_skripsi' => '',
            'jalur_skripsi' => '',
            'bln_awal_bimbingan' => '',
            'bln_akhir_bimbingan' => '',
            'sk_yudisium' => '',
            'tgl_sk_yudisium' => '',
            'ipk' => '',
            'no_seri_ijazah' => ''
        );
         $array_mhs[] = array('key'=>$temp_key,'data'=>$temp_data);
*/
        $data_param = array(
                    'act' => 'DeleteMahasiswaLulusDO',
                    'token' => $token,
                    'key' => array('id_registrasi_mahasiswa' => $id)
                  );

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

  
/*          $temp_result = $proxy->DeleteRecordset($token, 'dosen_pembimbing', json_encode($temp_keys));
         print_r($temp_result);*/

       
       // $temp_result = $proxy->UpdateRecordset($token, 'mahasiswa_pt', json_encode($array_mhs));
       // print_r($temp_result);

    }

    break;

}