<?php
session_start();
include "../../inc/config.php";
$url_rest = get_service_url('rest');
$json_response = [];
$jumlah = [];
$success_insert = [];
$success_insert_id = [];
$error_insert = [];
$error_insert_id = [];
$insert_biodata = [];
$all_errors = array();
$all_id = array();

//filter va
$sem = "";
//config feeder
$config_feeder = $db->fetch_single_row('config_user','id',1);
$id_sp = $config_feeder->id_sp;
$user_id = $config_feeder->id_user;


if ($_POST["total_data"] > 0) {
    $url_rest = get_service_url("rest");
    $offset = $_POST["offset"];
    $jumlah["offset"] = $offset;
    $token = $_POST["token"];
    $jumlah["token"] = $_POST["token"];

    if ($offset == 0) {
        $token = get_token();
        $jumlah["token"] = $token;
    }

    if ($_POST["sem"] != "all") {
        $sem = "and mulai_smt='" . $_POST["sem"] . "'";
    }
    if ($_POST["jurusan"] != "all") {
        $query_jurusan =
            "and trim(jurusan.kode_jurusan)='" . trim($_POST["jurusan"]) . "'";
    } else {
        if ($_SESSION["level"] != 1) {
            $query_jurusan =
                "and trim(jurusan.kode_jurusan)='" . $_SESSION["jurusan"] . "'";
            if ($_SESSION['jurusan']=='0') {
                $query_jurusan = '';
            }
        } else {
            $query_jurusan = "";
        }
    }

    $query_not_in = "and unik_id not in(" . $_POST["random_number"] . ")";
    $datas = $db->fetch_custom(
        "select mhs.*,id_sms,year(tgl_masuk_sp) as tahun_masuk,year(tgl_lahir) as tahun_lahir from mhs inner join jurusan on mhs.kode_jurusan=jurusan.kode_jurusan where mhs.id is not null $sem $query_jurusan and status_error!=1 $query_not_in limit 16"
    );

    $array_check_nim = array();
    $peserta_data = array();
    $biodatas = array();
     $mhs_data = array();
    $check_array_kode_pt = array();
    $peserta_data_nik = array();
    $peserta_data_nomom = array();
    $check_array_prodi_asal = array();
    foreach ($datas as $dt_id) {
        $nama = str_replace("'", "''", $dt_id->nm_pd);
        $nama_ibu = str_replace("'", "''", $dt_id->nm_ibu_kandung);
        $nim_concat[convert_ascii($dt_id->nipd).$dt_id->id_sms] = "('".convert_ascii($dt_id->nipd)."','".$dt_id->id_sms."')";
        $nim_like[$dt_id->nipd] = "nim like '%$dt_id->nipd%'";
        $nim_array[$dt_id->nipd] = "'".$dt_id->nipd."'";
        $sms_and[$dt_id->id_sms] = "('".$dt_id->id_sms."')";
        $peserta_data[convert_ascii($dt_id->nipd)] = "('".convert_ascii(strtoupper($nama))."','".tgl_indo_angka($dt_id->tgl_lahir)."','".convert_ascii(strtolower($nama_ibu))."')";
        $peserta_data_nomom[convert_ascii($dt_id->nipd)] = "('".convert_ascii(strtoupper($nama))."','".tgl_indo_angka($dt_id->tgl_lahir)."')";
        //if (strlen($dt_id->nik)==16) {
            $peserta_data_nik[convert_ascii($dt_id->nik)] =  "('".convert_ascii($dt_id->nik)."')";
       // }
        
        $biodatas[] = $dt_id;
        if ($dt_id->kode_pt_asal!='') {
            $check_array_kode_pt[convert_ascii($dt_id->kode_pt_asal)] = "('".convert_ascii($dt_id->kode_pt_asal)."')";
        }
        if ($dt_id->kode_pt_asal!='' && $dt_id->kode_prodi_asal!='') {
            $check_array_prodi_asal[convert_ascii($dt_id->kode_pt_asal).convert_ascii($dt_id->kode_prodi_asal)] = "('".convert_ascii($dt_id->kode_pt_asal)."','".$dt_id->kode_prodi_asal."')";
        }

    }

    $nim_concat_where = implode(",", $nim_concat);
    $implode_nim_like = implode(" or ",$nim_like);
    $implode_prodi = implode(",",$sms_and);
    //$filter_mhs = "(regexp_replace(trim(nim), '[^[:alnum:]\s.]+', ''), id_prodi) in ($nim_concat_where)";
    $filter_mhs = "(trim(nim),id_prodi) in ($nim_concat_where)";
        $data_req_mhs = [
            'act' => 'GetListMahasiswa',
            'token' => $token,
            'filter' => $filter_mhs,
            //'filter' => "id_registrasi_mahasiswa is not null and ($implode_nim_like) and id_prodi in($implode_prodi)",
            'order' => "",
            'limit' => "",
            'offset' => ""

        ];

    //dump($data_req_mhs);


    $nim_data = array();
    $check_nim_prodi = array();
    $check_nim_exist = json_decode($db->run($data_req_mhs,$url_rest));
    if ($check_nim_exist) {
        foreach ($check_nim_exist->data as $nim_mhs) {
            $nim_data[convert_ascii($nim_mhs->nim)] = $nim_mhs;
            $check_nim_prodi[convert_ascii($nim_mhs->nim).$nim_mhs->id_prodi] = $nim_mhs;
        }
    }



    //if insert dengan biodata sudah ada
    if ($_POST['method']=='insert_history_nik') {
        $biodata_concat_where =  implode ( ",", $peserta_data_nik); 
        $filter = "(trim(nik)) IN ($biodata_concat_where)";
        $data_req_mhs = [
            'act' => 'GetBiodataMahasiswa',
            'token' => $token,
            'filter' => $filter,
            'order' => "",
            'limit' => "",
            'offset' => ""

        ];
        $check_biodata_exist = json_decode($db->run($data_req_mhs,$url_rest));
      /* dump($data_req_mhs);
        dump($check_biodata_exist);
        exit();*/
        $mhs_data_nik = array();
        $id_mahasiswa_where = array();

        if (!empty($check_biodata_exist->data)) {
            foreach ($check_biodata_exist->data as $bio_mhs) {

                $mhs_data_nik[convert_ascii($bio_mhs->nik)] = $bio_mhs->id_mahasiswa;
                $id_mahasiswa_where[] = "('".$bio_mhs->id_mahasiswa."')";
            }
        }
    } elseif ($_POST['method']=='in') {
        $biodata_concat_where = implode ( ",", $peserta_data );
        $filter = "(trim(upper(nama_mahasiswa)),tanggal_lahir,trim(lower(nama_ibu_kandung))) IN ($biodata_concat_where)";
        $data_req_mhs = [
            'act' => 'GetBiodataMahasiswa',
            'token' => $token,
            'filter' => $filter,
            'order' => "",
            'limit' => "",
            'offset' => ""

        ];
        $check_biodata_exist = json_decode($db->run($data_req_mhs,$url_rest));
        /*dump($data_req_mhs);
        dump($check_biodata_exist);
        exit();*/
        $mhs_data = array();
        $id_mahasiswa_where = array();

        if (!empty($check_biodata_exist->data)) {
            foreach ($check_biodata_exist->data as $bio_mhs) {
                $mhs_data[convert_ascii($bio_mhs->nama_mahasiswa).convert_ascii($bio_mhs->tanggal_lahir).convert_ascii($bio_mhs->nama_ibu_kandung)] = $bio_mhs->id_mahasiswa;
                $id_mahasiswa_where[] = "('".$bio_mhs->id_mahasiswa."')";
            }
        }
    } elseif ($_POST['method']=='insert_history') {
        $biodata_concat_where = implode ( ",", $peserta_data );
        $filter = "(trim(upper(nama_mahasiswa)),tanggal_lahir,trim(lower(nama_ibu_kandung))) IN ($biodata_concat_where)";
        $data_req_mhs = [
            'act' => 'GetBiodataMahasiswa',
            'token' => $token,
            'filter' => $filter,
            'order' => "",
            'limit' => "",
            'offset' => ""

        ];
        $check_biodata_exist = json_decode($db->run($data_req_mhs,$url_rest));
/*        dump($data_req_mhs);
        dump($check_biodata_exist);
        exit();*/
       
        $id_mahasiswa_where = array();

        if (!empty($check_biodata_exist->data)) {
            foreach ($check_biodata_exist->data as $bio_mhs) {
                $mhs_data[convert_ascii($bio_mhs->nama_mahasiswa).convert_ascii($bio_mhs->tanggal_lahir).convert_ascii($bio_mhs->nama_ibu_kandung)] = $bio_mhs->id_mahasiswa;
                $id_mahasiswa_where[] = "('".$bio_mhs->id_mahasiswa."')";
            }
        }

    }/* elseif ($_POST['method']=='up') {

    }
*/
    


    $id_used_array = array();
    if (!empty($id_mahasiswa_where)) {
        //check if id_mhs used
        $id_concat_where = implode(",", $id_mahasiswa_where);
            $data_req_mhs = [
                'act' => 'GetListMahasiswa',
                'token' => $token,
                'filter' => "(id_mahasiswa) in ($id_concat_where)",
                'order' => "",
                'limit' => "",
                'offset' => ""

            ];
        $check_id_exist = json_decode($db->run($data_req_mhs,$url_rest));
        if ($check_id_exist) {
            foreach ($check_id_exist->data as $id_mhs_used) {
                $id_used_array[] = $id_mhs_used->id_mahasiswa;
            }
        }
    }

    $id_pt_array = array();
    if (!empty($check_array_kode_pt)) {
        //check if id_mhs used
        $concat_where = implode(",", $check_array_kode_pt);
            $data_req_mhs = [
                'act' => 'GetAllPT',
                'token' => $token,
                'filter' => "(kode_perguruan_tinggi) in ($concat_where)",
                'order' => "",
                'limit' => "",
                'offset' => ""

            ];
        $check_exist = json_decode($db->run($data_req_mhs,$url_rest));
        if ($check_exist) {
            foreach ($check_exist->data as $id_pt) {
                $id_pt_array[convert_ascii($id_pt->kode_perguruan_tinggi)] = array(
                    'id_sp' => $id_pt->id_perguruan_tinggi,
                    'nm_lemb' => $id_pt->nama_perguruan_tinggi
                );
            }
        }
    }
    $id_prodi_array = array();
    if (!empty($check_array_prodi_asal) and !empty($id_pt_array)) { 
        $concat_where = implode(",", $check_array_prodi_asal);
            $data_req_prodi = [
                'act' => 'GetAllProdi',
                'token' => $token,
                'filter' => "(kode_perguruan_tinggi,kode_program_studi) in ($concat_where)",
                'order' => "",
                'limit' => "",
                'offset' => ""
            ];

        
        $check_exist = json_decode($db->run($data_req_prodi,$url_rest));
        if ($check_exist) {
            foreach ($check_exist->data as $id_pt_prodi) {
                $id_prodi_array[convert_ascii($id_pt_prodi->kode_perguruan_tinggi).convert_ascii($id_pt_prodi->kode_program_studi)] = array(
                    'id_sms' => $id_pt_prodi->id_prodi,
                    'nm_lemb' => $id_pt_prodi->nama_program_studi
                );
            }
        }
    }


function getPtAsal($kode_pt) {
    global $db;
    $array_data_pt = array();
    if ($kode_pt!="") {
        $array_data_pt = array();
        $data_pt = $db->fetch_custom_single("select * id_sp,nm_lemb from satuan_pendidikan where trim(npsn)='".trim($kode_pt)."'");
        if ($data_pt) {
            $array_data_pt = array(
                'error_pt' => '',
                'id_sp' => $data_pt->id_sp,
                'nama_pt' => $data_pt->nm_lemb
            );
        } else {
            $array_data_pt = array(
                'error_pt' => 'Salah Kode PT Asal Perguruan Tinggi',
                'id_sp' => $data_pt->id_sp,
                'nama_pt' => $data_pt->nm_lemb
            );
        }
    } else {
            $array_data_pt = array(
                'error_pt' => 'Kode PT Asal tidak Boleh Kosong',
                'id_sp' => '',
                'nama_pt' => ''
            );
    }

    return $array_data_pt;
}

function getProdiAsal($id_sp,$kode_prodi) {
    global $db;
    if ($kode_pt!="") {
        $array_data_pt = array();
        $data_pt = $db->fetch_custom_single("select * id_sms,nm_lemb from sms where id_sp='$id_sp' and trim(kode_prodi)='".trim($kode_pt)."'");
        if ($data_pt) {
            $array_data_pt = array(
                'error_prodi' => '',
                'id_sms' => $data_pt->id_sms,
                'nama_prodi' => $data_pt->nm_lemb
            );
        } else {
            $array_data_pt = array(
                'error_prodi' => 'Kode Prodi Tidak Ditemukan',
                'id_sms' => $data_pt->id_sms,
                'nama_prodi' => $data_pt->nm_lemb
            );
        }
    } else {
            $array_data_pt = array(
                'error_prodi' => 'Kode Prodi Asal tidak Boleh Kosong',
                'id_sms' => '',
                'nama_prodi' => ''
            );
    }

    return $array_data_pt;
}
function insertRegPdArray($biodata,$additional_data)
{
    global $id_sp;
    global $id_pt_array;
    global $id_prodi_array;
    $biodatas = [
        "id_mahasiswa" => $additional_data['id_mahasiswa'],
        "nim" => $biodata->nipd,
        "id_jenis_daftar" => $biodata->id_jns_daftar,
        "id_jalur_daftar" => $biodata->id_jalur_masuk,
        "id_periode_masuk" => $biodata->mulai_smt,
        "tanggal_daftar" => $biodata->tgl_masuk_sp,
        "id_perguruan_tinggi" => $id_sp,
        "id_prodi" => $biodata->id_sms, 
        "id_pembiayaan" => $biodata->id_pembiayaan,
        "biaya_masuk"  => $biodata->biaya_masuk_kuliah
    ];
    if ($biodata->id_jns_daftar!='1') {
        $biodatas['sks_diakui'] = $biodata->sks_diakui;
        if (in_array(convert_ascii($biodata->kode_pt_asal), array_keys($id_pt_array))) {
            $biodatas["id_perguruan_tinggi_asal"] = $id_pt_array[convert_ascii($biodata->kode_pt_asal)]['id_sp'];
        }
        if (in_array(convert_ascii($biodata->kode_pt_asal).convert_ascii($biodata->kode_prodi_asal), array_keys($id_prodi_array))) {
           $biodatas["id_prodi_asal"] = $id_prodi_array[convert_ascii($biodata->kode_pt_asal).convert_ascii($biodata->kode_prodi_asal)]['id_sms'];
        }
    }
/*    if ($biodata->id_jns_daftar!='1') {
        $array_pindah = array(
            "sks_diakui" => $biodata->sks_diakui,
            "id_perguruan_tinggi_asal" => $id_pt_array[convert_ascii($biodata->kode_pt_asal)]['id_sp']
        );
        if (in_array(convert_ascii($biodata->kode_pt_asal).convert_ascii($biodata->kode_prodi_asal), array_keys($id_prodi_array))) {
            $array_pindah["id_prodi_asal"] = $id_prodi_array[convert_ascii($biodata->kode_pt_asal).convert_ascii($biodata->kode_prodi_asal)]['id_sms'];
        }
        $biodatas = array_merge($biodatas,$array_pindah);
    }*/
    return $biodatas;
}

function insertRegPdNativeArray($biodata,$additional_data)
{
    global $id_sp;
    global $user_id;
    global $id_pt_array;
    global $id_prodi_array;
    $biodatas = [
        "nipd" => $biodata->nipd,
        "jenis_daftar" => array('id_jns_daftar' => $biodata->id_jns_daftar),
        "jalur_masuk" => array('id_jalur_masuk' => $biodata->id_jalur_masuk),
        "pembiayaan" => array('id_pembiayaan' => $biodata->id_pembiayaan),
        "periode_pendaftaran" => $biodata->mulai_smt,
        "tgl_masuk" => $biodata->tgl_masuk_sp,
        "id_perguruan_tinggi" => $id_sp,
        "prodi" => array('id_sms' => $biodata->id_sms),
        "user" => array('id_sp' => $id_sp,'userid' => $user_id),
        "detail" => array('id_pd' => $additional_data['id_mahasiswa']),
        "biaya_masuk_kuliah"  => $biodata->biaya_masuk_kuliah
    ];
    if ($biodata->id_jns_daftar!='1') {
        $biodatas['sks_diakui'] = $biodata->sks_diakui;
        if (in_array(convert_ascii($biodata->kode_pt_asal), array_keys($id_pt_array))) {
            $biodatas["asal_perguruan_tinggi"] = array('id_sp' => $id_pt_array[convert_ascii($biodata->kode_pt_asal)]['id_sp'],'nm_lemb' => $id_pt_array[convert_ascii($biodata->kode_pt_asal)]['nm_lemb']);
        }
        if (in_array(convert_ascii($biodata->kode_pt_asal).convert_ascii($biodata->kode_prodi_asal), array_keys($id_prodi_array))) {
           $biodatas["asal_program_studi"] = array('id_sms' => $id_prodi_array[convert_ascii($biodata->kode_pt_asal).convert_ascii($biodata->kode_prodi_asal)]['id_sms'],'nm_lemb' => $id_prodi_array[convert_ascii($biodata->kode_pt_asal).convert_ascii($biodata->kode_prodi_asal)]['nm_lemb']);
        }
    }
/*    if ($biodata->id_jns_daftar!='1') {
        $array_pindah = array(
            "sks_diakui" => $biodata->sks_diakui,
            "asal_perguruan_tinggi" => array('id_sp' => $id_pt_array[convert_ascii($biodata->kode_pt_asal)]['id_sp'],'nm_lemb' => $id_pt_array[convert_ascii($biodata->kode_pt_asal)]['nm_lemb'])
        );

        if (in_array(convert_ascii($biodata->kode_pt_asal).convert_ascii($biodata->kode_prodi_asal), array_keys($id_prodi_array))) {
            $array_pindah["asal_program_studi"] = array('id_sms' => $id_prodi_array[convert_ascii($biodata->kode_pt_asal).convert_ascii($biodata->kode_prodi_asal)]['id_sms'],'nm_lemb' => $id_prodi_array[convert_ascii($biodata->kode_pt_asal).convert_ascii($biodata->kode_prodi_asal)]['nm_lemb']);
        }
        $biodatas = array_merge($biodatas,$array_pindah);
    }*/
    return $biodatas;
}

function insertRegPdNative($data) {
    global $config_feeder;
    global $token;
    $return_data = array();
    $url = $config_feeder->url.':'.$config_feeder->port.'/ws/history_pendidikan/add';
    $insert_reg = service_native($data,$url);
    return $insert_reg;
}

function updateRegPdArray($biodata,$additional_data)
{
    global $id_sp;
    global $id_pt_array;
    global $id_prodi_array;
    $biodatas = [
        "id_mahasiswa" => $additional_data['id_mahasiswa'],
        "nim" => $biodata->nipd,
       "id_jenis_daftar" => $biodata->id_jns_daftar,
        "id_jalur_daftar" => $biodata->id_jalur_masuk,
        "id_periode_masuk" => $biodata->mulai_smt,
        "id_perguruan_tinggi" => $id_sp,
        "id_prodi" => $biodata->id_sms, 
         "tanggal_daftar" => $biodata->tgl_masuk_sp,
        "id_bidang_minat" => null,
        "id_pembiayaan" => $biodata->id_pembiayaan,
        "biaya_masuk"  => $biodata->biaya_masuk_kuliah
    ];

/*    if ($biodata->id_jns_daftar=='1') {
        $additional_data = array(
            'id_mahasiswa' => $id_mahasiswa
        );
        $data_insert_reg = insertRegPdArray($biodata,$additional_data);
    } else {
        $additional_data = array(
            'id_mahasiswa' => $id_mahasiswa,
            "sks_diakui" => $biodata->sks_diakui,
            "id_perguruan_tinggi_asal" => $id_pt_array[convert_ascii($biodata->kode_pt_asal)]['id_sp']
        );
        if (in_array(convert_ascii($biodata->kode_pt_asal).convert_ascii($biodata->kode_prodi_asal, array_keys($id_prodi_array))) {
            $additional_data["id_prodi_asal"] = $id_prodi_array[convert_ascii($biodata->kode_pt_asal).convert_ascii($biodata->kode_prodi_asal)]['id_sms'];
        }
        $data_insert_reg = insertRegPdArray($biodata,$additional_data);
    }
*/
    if ($biodata->id_jns_daftar!='1') {
        $biodatas['sks_diakui'] = $biodata->sks_diakui;
        if (in_array(convert_ascii($biodata->kode_pt_asal), array_keys($id_pt_array))) {
            $biodatas["id_perguruan_tinggi_asal"] = $id_pt_array[convert_ascii($biodata->kode_pt_asal)]['id_sp'];
        }
        if (in_array(convert_ascii($biodata->kode_pt_asal).convert_ascii($biodata->kode_prodi_asal), array_keys($id_prodi_array))) {
           $biodatas["id_prodi_asal"] = $id_prodi_array[convert_ascii($biodata->kode_pt_asal).convert_ascii($biodata->kode_prodi_asal)]['id_sms'];
        }
    }
    return $biodatas;
}

function insertRegPd($data) {
    global $db;
    global $url_rest;
    global $token;
    $return_data = array();
    $data_dic = [
        "act" => "InsertRiwayatPendidikanMahasiswa",
        "token" => $token,
        "record" => $data
    ];

    $temp_recs_biodata = json_decode($db->run($data_dic, $url_rest));
    return $temp_recs_biodata;
}

function insertBiodataArray($biodata)
{
    $biodatas = [
        "nama_mahasiswa" => $biodata->nm_pd,
        "jenis_kelamin" => $biodata->jk,
        "tempat_lahir" => $biodata->tmpt_lahir,
        "tanggal_lahir" => $biodata->tgl_lahir,
        "id_agama" => $biodata->id_agama,
        "nik" => $biodata->nik,
        "nisn" => $biodata->nisn,
        "kewarganegaraan" => $biodata->kewarganegaraan,
        "jalan" => $biodata->jln,
        "dusun" => $biodata->nm_dsn,
        "rt" => $biodata->rt,
        "rw" => $biodata->rw,
        "kelurahan" => $biodata->ds_kel,
        "kode_pos" => $biodata->kode_pos,
        "id_wilayah" => $biodata->id_wil,
        "id_jenis_tinggal" => $biodata->id_jns_tinggal,
        "id_alat_transportasi" => $biodata->id_alat_transport,
        "telepon" => $biodata->no_tel_rmh,
        "handphone" => $biodata->no_hp,
        "email" => $biodata->email,
        "penerima_kps" => $biodata->a_terima_kps,
        "nomor_kps" => $biodata->no_kps,
        "nik_ayah" => $biodata->nik_ayah,
        "nama_ayah" => $biodata->nm_ayah,
        "tanggal_lahir_ayah" => $biodata->tgl_lahir_ayah,
        "id_pendidikan_ayah" => $biodata->id_jenjang_pendidikan_ayah,
        "id_pekerjaan_ayah" => $biodata->id_pekerjaan_ayah,
        "id_penghasilan_ayah" => $biodata->id_penghasilan_ayah,
        "nik_ibu" => $biodata->nik_ibu,
        "nama_ibu_kandung" => $biodata->nm_ibu_kandung,
        "id_pendidikan_ibu" => $biodata->id_jenjang_pendidikan_ibu,
        "id_pekerjaan_ibu" => $biodata->id_pekerjaan_ibu,
        "id_penghasilan_ibu" => $biodata->id_penghasilan_ibu,
        "npwp" => $biodata->npwp,
        "nama_wali" => $biodata->nm_wali,
        "tanggal_lahir_wali" => $biodata->tgl_lahir_wali,
        "id_pendidikan_wali" => $biodata->id_jenjang_pendidikan_wali,
        "id_pekerjaan_wali" => $biodata->id_pekerjaan_wali,
        "id_penghasilan_wali" => $biodata->id_penghasilan_wali,
        "id_kebutuhan_khusus_mahasiswa" => 0,
        "id_kebutuhan_khusus_ayah" => 0,
        "id_kebutuhan_khusus_ibu" => 0
    ];
    return $biodatas;
}

function insertBiodata($data) {
    global $db;
    global $url_rest;
    global $token;
    $return_data = array();
    $data_dic = [
        "act" => "InsertBiodataMahasiswa",
        "token" => $token,
        "record" => $data
    ];
    $temp_recs_biodata = json_decode($db->run($data_dic, $url_rest));
    return $temp_recs_biodata;
}

function responseCheckBiodata($data) {
    //dump($data);
    $return_array = array();
    $return_array['data'] = array();
    $return_array['is_error'] = 1;
        if (isset($data->code)) {
        $value_data = addslashes($data->message);
/*        if ($value_data!='') {*/
            $return_array['data'] = array(
                'keterangan' => $value_data,
                'status_error' => 2
            );
/*        } else {
          // dump('yep');
            if ($data->data->id_mahasiswa=="") {
                $return_array['is_error'] = 1;
                $return_array['data'] = array(
                    'keterangan' => 'Gagal di Import',
                    'status_error' => 2
                );
            } else {
                $return_array['is_error'] = 0;
                $return_array['id_mahasiswa'] = $data->data->id_mahasiswa;
                $return_array['data'] = array(
                    'keterangan' => '',
                    'status_error' => 1
                );
            }

        }*/
    } elseif ($data->error_desc!="") {
        $return_array['is_error'] = 1;
        $return_array['data'] = array(
            'keterangan' => addslashes($data->error_desc),
            'status_error' => 2
        );
    } else {
        $return_array['is_error'] = 0;
        $return_array['id_mahasiswa'] = $data->data->id_mahasiswa;
        $return_array['data'] = array(
            'keterangan' => '',
            'status_error' => 1
        );
       /* $return_array['data'] = array(
                'keterangan' => $data,
                'status_error' => 2
            );*/
    }

    //dump($return_array);

    return $return_array;
}

function check_id_valid($table_name,$id_name) {
    global $db;
    $jenis = $db->fetch_custom_single("SELECT group_concat($id_name) as id FROM $table_name");
    return explode(",",$jenis->id); 
}

function validasiMhs($biodata)
{
    //dump($biodata);
    //check if nim is exist
    global $url_rest;
    global $token;
    global $db;
    global $id_pt_array;
    global $id_prodi_array;
/*    echo $biodata->kode_pt_asal.$biodata->kode_prodi_asal;
    dump($id_prodi_array);*/
    //jenis daftar
    $jenis_daftar = check_id_valid('jenis_pendaftaran','id_jns_daftar');
    $jalur_masuk = check_id_valid('jalur_masuk','id');
    $jenjang_pendidikan = check_id_valid('jenjang_pendidikan','id_jenj_didik');
    $pekerjaan = check_id_valid('pekerjaan','id_pekerjaan');
    $penghasilan = check_id_valid('penghasilan','id_penghasilan');
    $transportasi = check_id_valid('data_transportasi','id_alat_transport');
    $agama = check_id_valid('agama','id_agama');
    $jenis_pembiayaan = array(1,2,3);
    $error_validasi = array();
    if ($biodata->nisn=="") {
         $error_validasi[] = "- NISN tidak boleh kosong";
    }
    if (!validateDate($biodata->tgl_lahir)) {
         $error_validasi[] = "- Format Tanggal Lahir harus Tahun-Bulan-Tanggal";
    }
    if (strlen($biodata->nisn)!=10) {
        $error_validasi[] = "- Nisn harus 10 digit";
    }
    if (!in_array($biodata->jk, array('L','P'))) {
        $error_validasi[] = "- Id Jenis Kelamin salah, Pastikan Sesuai Template";
    }  
    if (!in_array($biodata->id_agama, $agama)) {
        $error_validasi[] = "- Id Agama salah, Pastikan Sesuai Template";
    }
    if (!validateDate($biodata->tgl_masuk_sp)) {
         $error_validasi[] = "- Format Tanggal Masuk Kuliah harus Tahun-Bulan-Tanggal";
    }
    if (strlen($biodata->mulai_smt)!=5) {
        $error_validasi[] = "- Mulai Semester/Angkatan harus 5 digit";
    }
    
    if (!in_array($biodata->id_jns_daftar, $jenis_daftar)) {
        $error_validasi[] = "- Id Jenis Pendaftaran Salah, Pastikan Sesuai Template";
    }
    if (!in_array($biodata->id_jalur_masuk, $jalur_masuk)) {
        $error_validasi[] = "- Id Jalur Pendaftaran Salah, Pastikan Sesuai Template";
    }
    if (!in_array($biodata->id_pembiayaan, $jenis_pembiayaan)) {
        $error_validasi[] = "- Id Jenis Pembiayaan Salah, Pastikan Sesuai Template";
    }
    if ($biodata->biaya_masuk_kuliah=="") {
        $error_validasi[] = "- Biaya Masuk Kuliah Wajib diisi";
    }

    if ($biodata->tmpt_lahir=="") {
        $error_validasi[] = "- Tempat Lahir Wajib diisi";
    }
    if ($biodata->tmpt_lahir!="" && strlen($biodata->tmpt_lahir)>32) {
        $error_validasi[] = "- Panjang Karakter Tempat Lahir tidak boleh lebih dari 32 Karakter";
    }

    if ($biodata->kode_pos!="") {
        if (strlen($biodata->kode_pos)>5) {
           $error_validasi[] = "Kode POS tidak boleh lebih dari 5 karakter";
        }
        if (strlen($biodata->kode_pos)<5) {
           $error_validasi[] = "Kode POS harus berupa 5 digit angka, boleh kosong jika belum ada";
        }
    }
    if ($biodata->kewarganegaraan=='ID' && strlen($biodata->nik)!=16) {
        $error_validasi[] = "- NIK harus 16 Digit";
    }
    if ($biodata->nik_ayah!='' && strlen($biodata->nik_ayah)!=16) {
        $error_validasi[] = "- NIK Ayah harus angka 16 Digit, boleh kosongkan jika belum ada";
    } 
    if ($biodata->nik_ibu!='' && strlen($biodata->nik_ibu)!=16) {
        $error_validasi[] = "- NIK Ibu harus angka 16 Digit, boleh kosongkan jika belum ada";
    }
    if ($biodata->tgl_lahir_ayah!='' && !validateDate($biodata->tgl_lahir_ayah)) {
        $error_validasi[] = "- Format Tanggal Lahir Ayah harus Tahun-Bulan-Tanggal, boleh kosongkan jika belum ada";
    }
    if ($biodata->tgl_lahir_ibu!='' && !validateDate($biodata->tgl_lahir_ibu)) {
        $error_validasi[] = "- Format Tanggal Lahir Ibu harus Tahun-Bulan-Tanggal, boleh kosongkan jika belum ada";
    }
    if (!validateDate($biodata->tgl_masuk_sp)) {
         $error_validasi[] = "- Format Tanggal Masuk Kuliah harus Tahun-Bulan-Tanggal";
    }
    
    if ($biodata->id_jenjang_pendidikan_ayah!='' && !in_array($biodata->id_jenjang_pendidikan_ayah, $jenjang_pendidikan)) {
        $error_validasi[] = "- Id Pendidikan Ayah Salah, Pastikan Sesuai Template";
    }
    if ($biodata->id_jenjang_pendidikan_ibu!='' && !in_array($biodata->id_jenjang_pendidikan_ibu, $jenjang_pendidikan)) {
        $error_validasi[] = "- Id Pendidikan IBU Salah, Pastikan Sesuai Template, boleh kosongkan jika belum ada";
    }

    if ($biodata->id_jenjang_pendidikan_wali!='' && !in_array($biodata->id_jenjang_pendidikan_wali, $jenjang_pendidikan)) {
        $error_validasi[] = "- Id Pendidikan Wali Salah, Pastikan Sesuai Template, boleh kosongkan jika belum ada";
    }
    if ($biodata->id_pekerjaan_ayah!='' && !in_array($biodata->id_pekerjaan_ayah, $pekerjaan)) {
        $error_validasi[] = "- Id Pekerjaan Ayah Salah, Pastikan Sesuai Template, boleh kosongkan jika belum ada";
    }
    if ($biodata->id_pekerjaan_ibu!='' && !in_array($biodata->id_pekerjaan_ibu, $pekerjaan)) {
        $error_validasi[] = "- Id Pekerjaan IBU Salah, Pastikan Sesuai Template, boleh kosongkan jika belum ada";
    }

    if ($biodata->id_pekerjaan_wali!='' && !in_array($biodata->id_pekerjaan_wali, $pekerjaan)) {
        $error_validasi[] = "- Id Pekerjaan Wali Salah, Pastikan Sesuai Template, boleh kosongkan jika belum ada";
    }
    if ($biodata->id_penghasilan_ayah!='' && !in_array($biodata->id_penghasilan_ayah, $penghasilan)) {
        $error_validasi[] = "- Id Penghasilan Ayah Salah, Pastikan Sesuai Template, boleh kosongkan jika belum ada";
    }
    if ($biodata->id_penghasilan_ibu!='' && !in_array($biodata->id_penghasilan_ibu, $penghasilan)) {
        $error_validasi[] = "- Id Penghasilan Ibu Salah, Pastikan Sesuai Template, boleh kosongkan jika belum ada";
    }

    if ($biodata->id_penghasilan_wali!='' && !in_array($biodata->id_penghasilan_wali, $penghasilan)) {
        $error_validasi[] = "- Id Penghasilan Wali Salah, Pastikan Sesuai Template, boleh kosongkan jika belum ada";
    }

    if ($biodata->tgl_lahir_wali!='' && !validateDate($biodata->tgl_lahir_wali)) {
        $error_validasi[] = "- Format Tanggal Lahir Wali harus Tahun-Bulan-Tanggal, boleh kosongkan jika belum ada";
    }
    
    if ($biodata->id_alat_transport!='' && !in_array($biodata->id_alat_transport, $transportasi)) {
        $error_validasi[] = "- Id Alat Transportasi Salah, Pastikan Sesuai Template, boleh kosongkan jika belum ada";
    } elseif ($biodata->npwp!='' && strlen($biodata->npwp)>15) {
        $error_validasi[] = "- Digit NPWP tidak boleh lebih dari 15 Angka, cukup isikan angka nya saja";
    }
    /* elseif ($biodata->kewarganegaraan=='Id' && (substr($biodata->nik, 8, 2) < 1) || (substr($biodata->nik, 8, 2) > 12)) {
         $error_validasi = "NIK tidak Valid";
    }*/  /*elseif ($biodata->id_jns_daftar!='1') {
        if ($biodata->kode_pt_asal=="") {
            $error_validasi = "Selain Peserta didik baru, Kode PT Asal Tidak Boleh Kosong";
        } elseif ($biodata->kode_prodi_asal=="") {
            $error_validasi ='Selain Peserta didik baru, Kode Prodi Asal Tidak Boleh Kosong';
        } elseif (!in_array(convert_ascii($biodata->kode_pt_asal), array_keys($id_pt_array))) {
            $error_validasi = 'Kode PT tidak ditemukan di Feeder';
        }/* elseif (!in_array(convert_ascii($biodata->kode_pt_asal).convert_ascii($biodata->kode_prodi_asal), array_keys($id_prodi_array))) {
            $error_validasi = 'Kode Prodi Asal tidak ditemukan di Feeder';
        }
    }*/
    $return_validasi = "";
    if (!empty($error_validasi)) {
        $return_validasi = implode("\n", $error_validasi); 
    }

    return $return_validasi;
}

function checkBiodataExist($biodata)
{
    //check if nim is exist
    global $url_rest;
    global $token;
    global $db;
    $array_data = [];
    $data_req_mhs = [
        "act" => "GetBiodataMahasiswa",
        "token" => $token,
        "filter" => "nama_mahasiswa='" . trim(strtoupper($biodata->nm_pd)) . "' and tanggal_lahir='".tgl_indo_angka($biodata->tgl_lahir)."' and nama_ibu_kandung='".$biodata->nm_ibu_kandung."'",
        "order" => "",
        "limit" => "",
        "offset" => "",
    ];
    $check_biodata_exist = json_decode($db->run($data_req_mhs, $url_rest));
/*    dump($data_req_mhs);
    dump($check_biodata_exist);*/
    if (!empty($check_biodata_exist->data)) {
        $array_data = $check_biodata_exist->data[0];
    }

    return $array_data;
}

function checkNimExist($biodata)
{
    //check if nim is exist
    global $url_rest;
    global $token;
    global $db;
    $array_data = [];
    $data_req_mhs = [
        "act" => "GetListMahasiswa",
        "token" => $token,
        //"filter" => "regexp_replace(trim(nim), '[^[:alnum:]. ]+', '') = '".trim($biodata->nipd)."'",
         "filter" => "trim(nim) = '".trim($biodata->nipd)."'",
        "order" => "",
        "limit" => "",
        "offset" => "",
    ];

    $check_nim_exist = json_decode($db->run($data_req_mhs, $url_rest));
/*    dump($data_req_mhs);
    dump($check_nim_exist);*/
    if (!empty($check_nim_exist->data)) {
        $array_data = $check_nim_exist->data[0];
    }

    return $array_data;
}
function responseCheckHistory($data) {
    $return_array = array();
    $return_array['data'] = array();
    $return_array['is_error'] = 1;
    //dump($data);
        if (is_object($data)) {
        if (isset($data->message)) {
                $return_array['is_error'] = 1;
                $return_array['data'] = array(
                    'keterangan' => $data->message,
                    'status_error' => 2
                );
        } else {
        $value_data = addslashes($data->error_desc);
        if ($value_data!='') {
             $return_array['is_error'] = 1;
            $return_array['data'] = array(
                'keterangan' => $value_data,
                'status_error' => 2
            );
        } else {
            //dump($data);
            if ($data->data->id_registrasi_mahasiswa=="") {
                $return_array['is_error'] = 1;
                $return_array['data'] = array(
                    'keterangan' => 'Gagal di Import',
                    'status_error' => 2
                );
            } else {
                $return_array['is_error'] = 0;
                $return_array['data'] = array(
                    'keterangan' => '',
                    'status_error' => 1
                );
            }

        }
        }
        
    } else {
        $return_array['data'] = array(
                'keterangan' => $data,
                'status_error' => 2
            );
    }

    //dump($return_array);

    return $return_array;
}
function responseCheck($data) {
    $return_array = array();
    $return_array['data'] = array();
    $return_array['is_error'] = 1;
        if (is_object($data)) {
        $value_data = addslashes($data->error_desc);
        if ($value_data!='') {
            $return_array['data'] = array(
                'keterangan' => $value_data,
                'status_error' => 2
            );
        } else {
            //dump($data);
            if ($data->data->id_registrasi_mahasiswa=="") {
                $return_array['is_error'] = 1;
                $return_array['data'] = array(
                    'keterangan' => 'Gagal di Import',
                    'status_error' => 2
                );
            } else {
                $return_array['is_error'] = 0;
                $return_array['data'] = array(
                    'keterangan' => '',
                    'status_error' => 1
                );
            }

        }
    } else {
        $return_array['data'] = array(
                'keterangan' => $data,
                'status_error' => 2
            );
    }

    //dump($return_array);

    return $return_array;
}

function responseCheckNativeHistory($data) {
    
    $return_array = array();
    $return_array['data'] = array();
    $return_array['is_error'] = 1;
    if (isset($data->message)) {
        if ($data->code=='200') {
            $return_array['is_error'] = 0;
            $return_array['data'] = array(
                'keterangan' => '',
                'status_error' => 1
            );
        } else {
            $return_array['data'] = array(
                'keterangan' => $data->message,
                'status_error' => 2
            );
        }
    } else {
            $return_array['data'] = array(
                'keterangan' => 'Error',
                'status_error' => 2
            );
    }

    
//dump($data);
    return $return_array;
}
   $indexer = 0;
 foreach ($biodatas as $biodata) {
    $data_insert_reg = array();
    $has_biodata = 0;
    $validasi_mhs = validasiMhs($biodata);
    $nim = convert_ascii($biodata->nipd);
        if ($_POST['method']=='up') {
            //check if nim is exist
            if (!in_array($biodata->nipd.$biodata->id_sms, array_keys($check_nim_prodi))) {
                $error_insert[] = [
                    "keterangan" => 'NIM ini tidak ada di Feeder',
                    "status_error" => 2,
                ];
                $error_insert_id[] = $biodata->id;
            } else {
                 $error_history = "";

            $additional_data = array(
                'id_mahasiswa' => $check_nim_prodi[$biodata->nipd.$biodata->id_sms]->id_mahasiswa
            );
            $array_update = updateRegPdArray($biodata,$additional_data);
            $param = array(
                'act' => 'UpdateRiwayatPendidikanMahasiswa',
                'token' => get_token(),
                'key' => array('id_registrasi_mahasiswa' => $check_nim_prodi[$biodata->nipd.$biodata->id_sms]->id_registrasi_mahasiswa),
                'record' => $array_update
            );
            $update = service_request($param);
            if ($update->error_desc!="") {
                $error_history = $update->error_desc;
            }


          /*  dump($param);

            dump($update);
*/
            if ($error_history=="") {
                //update biodata
                $array_update_biodata = insertBiodataArray($biodata);
                $param = array(
                    'act' => 'UpdateBiodataMahasiswa',
                    'token' => get_token(),
                    'key' => array('id_mahasiswa' => $check_nim_prodi[$biodata->nipd.$biodata->id_sms]->id_mahasiswa),
                    'record' => $array_update_biodata
                );
                $update_biodata = service_request($param);

                if ($update_biodata->error_desc!="") {
                     $error_insert[] =  array(
                        'keterangan' => $update_biodata->error_desc,
                        'status_error' => 2
                    ); 
                    $error_insert_id[] = $biodata->id;
                } else {
                    $success_insert[] = array(
                        'keterangan' => '',
                        'status_error' => 1
                    );
                    $success_insert_id[] = $biodata->id;
                }

            } else {
                     $error_insert[] =  array(
                        'keterangan' => $error_history,
                        'status_error' => 2
                    ); 
                    $error_insert_id[] = $biodata->id;
            }

            $data_insert_reg = array();
            }
    } elseif ($validasi_mhs != "") {
        $error_insert[] = [
            "keterangan" => $validasi_mhs,
            "status_error" => 2,
        ];
        $error_insert_id[] = $biodata->id;
    } elseif ($_POST['method']=='insert_history_nik') {
        if (in_array(convert_ascii($biodata->nik), array_keys($mhs_data_nik))) {
           $id_mahasiswa = $mhs_data_nik[convert_ascii($biodata->nik)];
            $additional_data = array(
                'id_mahasiswa' => $id_mahasiswa
            );
            $data_insert_reg = insertRegPdArray($biodata,$additional_data);
            //$data_insert_reg = insertRegPdNativeArray($biodata,$additional_data);
            $insert_reg = insertRegPd($data_insert_reg);
         
           // exit();
            $check_response = responseCheckHistory($insert_reg);
             
            if ($check_response['is_error']==0) {
                $success_insert[] = $check_response['data'];
                $success_insert_id[] = $biodata->id;
            } else {
                    $error_insert[] = $check_response['data'];
                    $error_insert_id[] = $biodata->id;
            }

        } else {
            $error_insert[] =  array(
                'keterangan' => 'Pencarian biodata berdasarkan NIK tidak ditemukan',
                'status_error' => 2
            ); 
            $error_insert_id[] = $biodata->id;
            $has_biodata = 0;
        }
    } elseif ($_POST['method']=='in') {
        //check if nim is exist
        if (!in_array($biodata->nipd.$biodata->id_sms, array_keys($check_nim_prodi))) {
          
           if (in_array(convert_ascii(strtoupper($biodata->nm_pd)).convert_ascii(tgl_indo_angka($biodata->tgl_lahir).convert_ascii($biodata->nm_ibu_kandung)), array_keys($mhs_data))) {
               $id_mahasiswa = $mhs_data[convert_ascii(strtoupper($biodata->nm_pd)).convert_ascii(tgl_indo_angka($biodata->tgl_lahir)).convert_ascii($biodata->nm_ibu_kandung)];
                $additional_data = array(
                    'id_mahasiswa' => $id_mahasiswa
                );

                $data_insert_reg = insertRegPdArray($biodata,$additional_data);
                //$data_insert_reg = insertRegPdNativeArray($biodata,$additional_data);
                $has_biodata = 1;
                    $insert_reg = insertRegPd($data_insert_reg);
                    /*dump($data_insert_reg);
                    dump($insert_reg);*/
                    $check_response = responseCheckHistory($insert_reg);
                    if ($check_response['is_error']==0) {
                        $success_insert[] = $check_response['data'];
                        $success_insert_id[] = $biodata->id;
                    } else {
                            $error_insert[] = $check_response['data'];
                            $error_insert_id[] = $biodata->id;
                    }
            } else {
                $insert_data_biodata = insertBiodataArray($biodata);
                //dump($insert_data_biodata);
                $insert_biodata = insertBiodata($insert_data_biodata);
                $check_response = responseCheckBiodata($insert_biodata);


                if ($check_response['is_error']==0) {
                    $success_insert[] = $check_response['data'];
                    $success_insert_id[] = $biodata->id;
                    $has_biodata = 1;
                    $additional_data = array(
                        'id_mahasiswa' => $check_response['id_mahasiswa']
                    );
                    $id_mahasiswa = $check_response['id_mahasiswa'];
                    $data_insert_reg = insertRegPdArray($biodata,$additional_data);
                    $insert_reg = insertRegPd($data_insert_reg);
                    $check_response = responseCheckHistory($insert_reg);
                    if ($check_response['is_error']==0) {
                        $success_insert[] = $check_response['data'];
                        $success_insert_id[] = $biodata->id;
                    } else {
                            $error_insert[] = $check_response['data'];
                            $error_insert_id[] = $biodata->id;
                    }
                    //$data_insert_reg = insertRegPdNativeArray($biodata,$additional_data);
                } else {
                    $error_insert[] = $check_response['data'];
                    $error_insert_id[] = $biodata->id;
                    $has_biodata = 0;
                }
            } 
        } else {
            $error_insert[] =  array(
                'keterangan' => 'NIM ini sudah terdaftar di Prodi',
                'status_error' => 2
            ); 
            $error_insert_id[] = $biodata->id;
            $has_biodata = 0;
        }


    } elseif ($_POST['method']=='insert_history') {
                if (in_array(convert_ascii(strtoupper($biodata->nm_pd)).convert_ascii(tgl_indo_angka($biodata->tgl_lahir)).convert_ascii($biodata->nm_ibu_kandung), array_keys($mhs_data))) {
                   $id_mahasiswa = $mhs_data[convert_ascii(strtoupper($biodata->nm_pd)).convert_ascii(tgl_indo_angka($biodata->tgl_lahir)).convert_ascii($biodata->nm_ibu_kandung)];
                    $additional_data = array(
                        'id_mahasiswa' => $id_mahasiswa
                    );
                    $data_insert_reg = insertRegPdArray($biodata,$additional_data);
                    //$data_insert_reg = insertRegPdNativeArray($biodata,$additional_data);
                    $has_biodata = 1;
                    $insert_reg = insertRegPd($data_insert_reg);
                    $check_response = responseCheckHistory($insert_reg);
                    if ($check_response['is_error']==0) {
                        $success_insert[] = $check_response['data'];
                        $success_insert_id[] = $biodata->id;
                    } else {
                            $error_insert[] = $check_response['data'];
                            $error_insert_id[] = $biodata->id;
                    }
                } else {
                     $error_insert[] =  array(
                        'keterangan' => 'Pencarian biodata berdasarkan Nama,tgl_lahir,nama_ibu tidak ditemukan',
                        'status_error' => 2
                    ); 
                    $error_insert_id[] = $biodata->id;
                    $has_biodata = 0;
                }
        }

    $all_id[] = $biodata->id;
     $indexer++;
    //end looping biodatas
}

    
    //update sukses
    if (!empty($success_insert)) {
        updateMulti("mhs", $success_insert, "id", $success_insert_id);
    }
    $success_insert = [];
    $success_insert_id = [];

    //update error
    if (!empty($error_insert)) {
        updateMulti("mhs", $error_insert, "id", $error_insert_id);
        echo $db->getErrorMessage();
    }
    $error_insert = [];
    $error_insert_id = [];
   
    $implode_id = implode(",", $all_id);

    $db->fetch_custom(
        "update mhs set unik_id=" .
            $_POST["random_number"] .
            " where id in (" .
            $implode_id .
            ")"
    );
/*    echo "update mhs set unik_id=" .
            $_POST["random_number"] .
            " where id in (" .
            $implode_id .
            ")";
    echo $db->getErrorMessage();*/
    if (isset($_POST["id_data"])) {
        $jml_error_sukses = $db->fetch_custom_single(
            "SELECT COUNT(IF(status_error='1',1,NULL)) AS jml_sukses,COUNT(IF(status_error='2',1,NULL)) AS jml_error FROM mhs where unik_id=" .
                $_POST["random_number"]
        );

        //dump($jml_error_sukses);

        $jumlah_sukses = $jml_error_sukses->jml_sukses;
        $jumlah_error = $jml_error_sukses->jml_error;

        $db->fetch_custom(
            "update mhs set unik_id=0 where  unik_id=" . $_POST["random_number"]
        );

        $msg =
            "<div class=\"alert alert-warning \" role=\"alert\">
        <font color=\"#3c763d\">" .
            $jumlah_sukses .
            " data Mahasiswa berhasil ditambah/diperbaharui</font><br />
        <font color=\"#ce4844\" >" .
            $jumlah_error .
            " data tidak bisa ditambahkan </font>";

        if (!$jumlah_error == 0) {
            $msg .=
                "<a data-toggle=\"collapse\" href=\"#collapseExample\" aria-expanded=\"false\" aria-controls=\"collapseExample\">Detail error</a>";
            //pesan error
            $all_errors = ["Silakan lihat detail error di list data"];
        }
        //echo "<br />Total: ".$i." baris data";
        $msg .= "<div class=\"collapse\" id=\"collapseExample\">";
        $i = 1;
        foreach ($all_errors as $pesan) {
            $msg .=
                "<div class=\"bs-callout bs-callout-danger\">" .
                $i .
                ". " .
                $pesan .
                "</div><br />";
            $i++;
        }
        $msg .= "</div>
        </div>";

        $jumlah["last_notif"] = $msg;
    }
} else {
    $msg = "Tidak ada data yang bisa diproses<p>";
    $jumlah["last_notif"] = $msg;
}

array_push($json_response, $jumlah);
echo json_encode($json_response);