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

$json_response = array();
$msg = "";
$jur_filter = "";

if ($_POST['total_data']<1) {
    $msg =  "<div class=\"alert alert-warning \" role=\"alert\">
        <font color=\"#3c763d\">Tidak ada data berhasil di Upload</font><br />
        </div>";

    $jumlah['last_notif'] = $msg;
    array_push($json_response, $jumlah);

    echo json_encode($json_response);
    exit();
}

$kode_pt = $db->fetch_single_row('config_user','id',1);

$offset = $_POST['offset'];

$jumlah['offset'] = $offset;


function http_build_query_for_curl( $arrays, &$new = array(), $prefix = null ) {

    if ( is_object( $arrays ) ) {
        $arrays = get_object_vars( $arrays );
    }

    foreach ( $arrays AS $key => $value ) {
        $k = isset( $prefix ) ? $prefix . '[' . $key . ']' : $key;
        if ( is_array( $value ) OR is_object( $value )  ) {
            http_build_query_for_curl( $value, $new, $k );
        } else {
            $new[$k] = $value;
        }
    }
}


   
    $prodi = trim($_POST['jurusan']);
    if ($prodi!='all') {
      $jur_filter = "and jurusan.kode_jurusan='$prodi'";
      } 




$data_rec = array();

		     
   
        $order_by = "nipd ASC";

        if ($_POST['total_data']<=15) {
            $limit = $_POST['total_data'];
        } else {
            $limit = 15;
        }


        $temp_rec = $db->fetch_custom("select * from mhs inner join jurusan on mhs.kode_jurusan=jurusan.kode_jurusan where mulai_smt='".$_POST['angkatan']."' $jur_filter limit $offset,$limit");

                    foreach ($temp_rec as $key) {

                    	
                    $data_rec[] = array(
                          'nipd' => $key->nipd,
                          'nm_pd' => $key->nm_pd,
                          'tmpt_lahir' => $key->tmpt_lahir,
                          'tgl_lahir' => $key->tgl_lahir,
                          'jk' => $key->jk,
                          'nik' => $key->nik,
                          'id_agama' => $key->id_agama,
                          'nisn' => $key->nisn,
                          'id_jalur_masuk' => $key->id_jalur_masuk,
                          'npwp' => $key->npwp,
                          'kewarganegaraan' => $key->kewarganegaraan,
                          'id_jns_daftar' => $key->id_jns_daftar,
                          'tgl_masuk_sp' => $key->tgl_masuk_sp,
                          'mulai_smt' => $key->mulai_smt,
                          'jln' => $key->jln,
                          'rt' => $key->rt,
                          'rw' => $key->rw,
                          'nm_dsn' => $key->nm_dsn,
                          'ds_kel' => $key->ds_kel,
                          'id_wil' => $key->id_wil,
                          'kode_pos' => $key->kode_pos,
                          'id_jns_tinggal' => $key->id_jns_tinggal,
                          'id_alat_transport' => $key->id_alat_transport,
                          'no_tel_rmh' => $key->no_tel_rmh,
                          'no_hp' => $key->no_hp,
                          'email' => $key->email,
                          'a_terima_kps' => $key->a_terima_kps,
                          'no_kps' => $key->no_kps,
                          'nik_ayah' => $key->nik_ayah,
                          'nm_ayah' => $key->nm_ayah,
                          'tgl_lahir_ayah' => $key->tgl_lahir_ayah,
                          'id_jenjang_pendidikan_ayah' => $key->id_jenjang_pendidikan_ayah,
                          'id_pekerjaan_ayah' => $key->id_pekerjaan_ayah,
                          'id_penghasilan_ayah' => $key->id_penghasilan_ayah,
                          'nik_ibu' => $key->nik_ibu,
                          'nm_ibu_kandung' => $key->nm_ibu_kandung,
                          'tgl_lahir_ibu' => $key->tgl_lahir_ibu,
                          'id_jenjang_pendidikan_ibu' => $key->id_jenjang_pendidikan_ibu,
                          'id_pekerjaan_ibu' => $key->id_pekerjaan_ibu,
                          'id_penghasilan_ibu' => $key->id_penghasilan_ibu,
                          'nm_wali' => $key->nm_wali,
                          'tgl_lahir_wali' => $key->tgl_lahir_wali,
                          'id_jenjang_pendidikan_wali' => $key->id_jenjang_pendidikan_wali,
                          'id_pekerjaan_wali' => $key->id_pekerjaan_wali,
                          'id_penghasilan_wali' => $key->id_penghasilan_wali,
                          'id_pembiayaan'=> $key->id_pembiayaan,
                          'biaya_masuk_kuliah'=> $key->biaya_masuk_kuliah,
                          'sks_diakui'=> $key->sks_diakui,
                          'kode_pt_asal'=> $key->kode_pt_asal,
                          'kode_prodi_asal'=>$key->kode_prodi_asal,
                          'kode_jurusan' => $key->kode_jurusan,
                          'kode_pt' => $kode_pt->id_sp,
                          'id_sms' => $key->id_sms,
                          'nama_jurusan' => $key->jenjang.' '.$key->nama_jurusan,
                          'password' => $key->password
                        );

                    }

http_build_query_for_curl( $data_rec, $post );

//print_r($post);


//extract data from the post
//set POST variables
$url = $db->fetch_single_row("url_data_mhs","id",1)->url_mhs;
$url = $url."/push.php";
//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POSTFIELDS, $post);

//execute post
$result = curl_exec($ch);

//print_r($result);


//close connection
curl_close($ch);


if ($_POST['last']=='yes') {
    //echo "<pre>";

$msg =  "<div class=\"alert alert-warning \" role=\"alert\">
        <font color=\"#3c763d\">".$_POST['total_data']." data berhasil di Upload</font><br />";
        $msg .= "</div>
        </div>";

        $jumlah['last_notif'] = $msg;
}



array_push($json_response, $jumlah);

echo json_encode($json_response);
exit();
?>