<?php
include "../../inc/config.php";
$json_response = array();

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

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

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

$offset = $_POST['offset'];
$token = $_POST['token'];

$jumlah['offset'] = $offset;
$jumlah['token'] = $_POST['token'];


/*if ($_POST['last']=='yes') {
    //echo 'yes';
    $jumlah['last'] = "Yes";
}*/


// open the file "demosaved.csv" for writing
//$file = fopen('demosaved.csv', 'a');

if ($offset==0) {
    $jumlah['token'] = $token;

}

        $temp_rec_data = file_get_contents("http://103.183.74.106/services/get_wilayah.php?offset=$offset");
        $temp_rec = json_decode($temp_rec_data);
                    foreach ($temp_rec as $key) {
                    $data_rec[] = array(
                       'id_wil' => convert_ascii($key->id_wil),
                       'nm_wil' => convert_ascii($key->nm_wil),
                       'id_induk_wilayah' => convert_ascii($key->id_induk_wilayah),
                       'id_level_wil' => convert_ascii($key->id_level_wil)
                       );
                    }

$db->insertMulti('data_wilayah',$data_rec);
echo $db->getErrorMessage();

if ($_POST['last']=='yes') {
  $jumlah_total = $_POST['total_data'];
$msg =  "<div class=\"alert alert-warning \" role=\"alert\">
        <font color=\"#3c763d\">".$jumlah_total." data berhasil di Download</font><br />";
        $msg .= "</div>
        </div>";

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



array_push($json_response, $jumlah);

echo json_encode($json_response);
exit();

?>