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


$json_response = array();
$msg = "";
$values = "";
/**
 * [trimmer trim for import excel
 *
 * @param  [type] $excel column value
 * @return [type]  trimmed value
 */
function trimmer($value)
{
    $result = preg_replace('/[^[:print:]]/', '', filter_var($value, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH));
    return addslashes(trim($result));
}

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)->id_sp;

$offset = $_POST['offset'];

$jumlah['offset'] = $offset;


$data_rec = array();
        $url = $db->fetch_single_row("url_data_mhs","id",1)->url_mhs;
        $data = file_get_contents($url."/stream_data.php?offset=$offset&kode_pt=$kode_pt");

        $datas = json_decode($data);

                    foreach ($datas as $key) {

                        $check = $db->check_exist('update_nik',array('nim' => $key->nipd));
                        if ($check==true) {
                            $update_mhs[] = array(
                              'nik' => trimmer($key->nik),
                              'nisn' => trimmer($key->nisn),
                              'jln' => trimmer($key->jln),
                              'id_wil' => trimmer($key->id_wil),
                              'no_hp' => trimmer($key->no_hp),   
                              'email' => trimmer($key->email)
                            );
                            $nipd[] = $key->nipd;
                      } else {
                           $insert_mhs[] = array(
                              'nim' => trimmer($key->nipd),
                              'nama' => trimmer($key->nm_pd),
                              'nik' => trimmer($key->nik),
                              'nisn' => trimmer($key->nisn),
                              'jln' => trimmer($key->jln),
                              'id_wil' => trimmer($key->id_wil),
                              'no_hp' => trimmer($key->no_hp),   
                              'email' => trimmer($key->email),   
                              'kode_jurusan' => trimmer($key->kode_jurusan)
                            );
                      }      
}

                if (!empty($update_mhs)) {
                    updateMulti('update_nik',$update_mhs,'nim',$nipd);
                }

                if (!empty($insert_mhs)) {
                  $db->insertMulti('update_nik',$insert_mhs);
                  echo $db->getErrorMessage();
                }

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

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

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



array_push($json_response, $jumlah);

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