<?php




function buildInspeksiTanah($rows)
{
    $dataToInsertJson = [];
    $tanah = [
        'luas_tanah'            => ['sesuai' => null],
        'hadap_mata_angin'      => ['sesuai' => null],
        'bentuk_tanah'          => ['bentuk_tanah' => [], 'lainnya' => null],
        'kontur_tanah'          => [],
        'ketinggian_tanah'      => ['ketinggian' => [], 'lebih_tinggi' => null, 'lebih_rendah' => null],
        'kontur_jalan'          => null,
        'ketinggian_jalan'      => [],
        'posisi_kavling'        => ['posisi_kavling' => [], 'lainnya' => null],
        'tusuk_sate'            => 'no',
        'lockland'              => 'no',
        'kondisi_fisik_tanah'   => ['kondisi_fisik_tanah' => [], 'lainnya' => null]
    ];

    foreach ($rows as $row) {
        $name = trim($row['name']);
        $keySesuaiORTidak = !empty($row['mig_name_keterangan_lain']) ? 'sesuai' : 'tidak sesuai';
        $value = !empty($row['mig_name_keterangan_lain']) ? $row['mig_name_keterangan_lain'] : $row['mig_name_keterangan'];

        switch ($name) {
            case 'Luas Tanah':
                $tanah['luas_tanah'][$keySesuaiORTidak] = $value;
                break;

            case 'Permukaan dengan Jalan':
                $tanah['hadap_mata_angin'][$keySesuaiORTidak] = $value;
                break;

            case 'Bentuk Tanah':
                if ($value === 'Lainnya') {
                    $tanah['bentuk_tanah']['bentuk_tanah'][] = $value;
                    $tanah['bentuk_tanah']['lainnya'] = $row['mig_name_keterangan_lain'];
                } else {
                    $tanah['bentuk_tanah']['bentuk_tanah'][] = $value;
                }
                break;

            case 'Kontur Tanah':
                $tanah['kontur_tanah'][] = $value;
                break;

            case 'Peruntukan Tanah':
                $tanah['ketinggian_tanah']['ketinggian'][] = $value;
                break;

            case 'Fisik Tanah':
                $tanah['kondisi_fisik_tanah']['kondisi_fisik_tanah'][] = $value;
                break;
        }
    }

    // Isi default jika kosong
    if (empty($tanah['luas_tanah']['sesuai'])) {
        $tanah['luas_tanah'] = ['sesuai' => null];
    }
    if (empty($tanah['hadap_mata_angin']['sesuai'])) {
        $tanah['hadap_mata_angin'] = ['sesuai' => null];
    }

    // Masukkan ke array JSON
    $dataToInsertJson[] = json_encode([
        'tanah' => $tanah
    ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

    return $dataToInsertJson;
}


function buildInspeksiBangunan($rows)
{
    $dataToInsertJson = [];

    // Inisialisasi struktur bangunan
    $bangunan = [
        'luas_tanah_bagunan' => ['sesuai' => null],
        'jenis_bangunan' => [],
        'kondisi_bangunan' => [],
        'sifat_bangunan' => [],
        'sifat_bangunan_input' => [null, null, null],
        'spesifikasi_bangunan' => [
            [
                'bagunan' => 'Bangunan 1',
                'spek_kategori_bangunan' => [
                    'Pondasi' => ['0' => null, '1' => null, 'lainnya' => null],
                    'Struktur' => ['0' => null, '1' => null, 'lainnya' => null],
                    'Rangka Atap' => ['0' => null, 'lainnya' => null],
                    'Tutup Atap' => ['0' => null, 'lainnya' => null],
                    'Plafond' => ['lainnya' => null],
                    'Dinding' => ['0' => null, '1' => null, 'lainnya' => null],
                    'Pelapis Dinding' => ['0' => null, 'lainnya' => null],
                    'Pintu Jendela' => ['0' => null, '1' => null, '2' => null, '3' => null, 'lainnya' => null],
                    'Lantai' => ['0' => null, '1' => null, 'lainnya' => null],
                ]
            ]
        ],
        'sarana_pelengkap' => [],
        'sarana_pelengkap_input' => [null, null, null, null, null, null, null, null, null]
    ];

    foreach ($rows as $row) {
        $name = trim($row['name']);
        $keySesuaiORTidak = !empty($row['mig_name_keterangan_lain']) ? 'sesuai' : 'tidak sesuai';
        $value = !empty($row['mig_name_keterangan_lain']) ? $row['mig_name_keterangan_lain'] : $row['mig_name_keterangan'];

        switch ($name) {
            case 'Luas Bangunan':
                $bangunan['luas_tanah_bagunan'][$keySesuaiORTidak] = $value;
                break;

            case 'Jenis Bangunan':
                if ($value === 'Lainnya') {
                    $bangunan['jenis_bangunan'][] = $value;
                } else {
                    $bangunan['jenis_bangunan'][] = $value;
                }
                break;

            case 'Kondisi Bangunan':
                $bangunan['kondisi_bangunan'][] = $value;
                break;

            case 'Sifat Bangunan':
                $bangunan['sifat_bangunan'][] = $value;
                break;

            case 'Lantai':
                $bangunan['spesifikasi_bangunan'][0]['spek_kategori_bangunan']['Lantai']['0'] = $value;
                break;

            case 'Dinding':
                $bangunan['spesifikasi_bangunan'][0]['spek_kategori_bangunan']['Dinding']['0'] = $value;
                break;

            case 'Langit-langit / Plafon':
                $bangunan['spesifikasi_bangunan'][0]['spek_kategori_bangunan']['Plafond']['lainnya'] = $value;
                break;

            case 'Kusen-kusen':
                $bangunan['spesifikasi_bangunan'][0]['spek_kategori_bangunan']['Pintu Jendela']['0'] = $value;
                break;

            case 'Penutup Atap':
                $bangunan['spesifikasi_bangunan'][0]['spek_kategori_bangunan']['Tutup Atap']['0'] = $value;
                break;

            case 'Partisi':
                $bangunan['spesifikasi_bangunan'][0]['spek_kategori_bangunan']['Dinding']['1'] = $value;
                break;
        }
    }

    // Isi default jika kosong
    if (empty($bangunan['luas_tanah_bagunan']['sesuai'])) {
        $bangunan['luas_tanah_bagunan'] = ['sesuai' => null];
    }

    // Masukkan ke array JSON
    $dataToInsertJson[] = json_encode([
        'bangunan' => $bangunan
    ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

    return $dataToInsertJson;
}

function buildRapJsonData($rows)
{
    $dataToInsertJson = [];

    $rapData = [
       "perizinan" => [],
       "brosur_price_list" => [],
       "pengalaman_developer" => null,
       "developer_anggota" => null,
       "lainnya_developer" => [],
       "kapan_mulai_dibangun" => null,
       "kondisi_perumahan" => null,
       "progres_pembangunan" => null,
       "kontraktor" => null,
       "lingkungan_sekitar" => null,
       "komplek_disekitar" => null,
       "pusat_keramaian" => null,
       "transportasi_umum" => null,
       "lainnya_kondisi" => [],
       "partisi" => new \stdClass(), // Objek partisi
       "jumlah_unit" => [],
       "batas_batas_perumahan" => [],
       "fasus_fasum" => [],
       "progres_penjualan" => [],
       "harga_unit" => [],
       "target_market" => [],
       "kerjasama_dengan_bank" => [],
       "rute_menuju_lokasi" => null,
       "asset" => new \stdClass(), // Struktur asset
       "fakta" => new \stdClass(), // Struktur fakta
       "foto_gistaru" => null,
       "foto_bhumi" => null,
       "foto_argis_region" => null,
       "foto_tempat" => null
    ];

    foreach ($rows as $row) {
        if (!isset($row['mig_key_keterangan'], $row['mig_nilai'])) {
            continue;
        }

        $key = $row['mig_key_keterangan'];
        $value = $row['mig_nilai'];

        switch ($key) {
            case 'pengalaman_developer':
                $rapData['pengalaman_developer'] = $value;
                break;

            case 'developer_anggota':
                $rapData['developer_anggota'] = $value;
                break;

            case 'kapan_mulai_dibangun':
                $rapData['kapan_mulai_dibangun'] = $value;
                break;

            case 'kondisi_perumahan':
                $rapData['kondisi_perumahan'] = $value;
                break;

            case 'progres_pembangunan':
                $rapData['progres_pembangunan'] = $value;
                break;

            case 'kontraktor':
                $rapData['kontraktor'] = $value;
                break;

            case 'lingkungan_sekitar':
                $rapData['lingkungan_sekitar'] = $value;
                break;

            case 'komplek_disekitar':
                $rapData['komplek_disekitar'] = $value;
                break;

            case 'pusat_keramaian':
                $rapData['pusat_keramaian'] = $value;
                break;

            case 'transportasi_umum':
                $rapData['transportasi_umum'] = $value;
                break;

            case 'jumlah_unit':
                $rapData['jumlah_unit'][] = $value;
                break;

            case 'batas_batas_perumahan':
                $rapData['batas_batas_perumahan'][] = $value;
                break;

            case 'fasus_fasum':
                $rapData['fasus_fasum'][] = $value;
                break;

            case 'progres_penjualan':
                $rapData['progres_penjualan'][] = $value;
                break;

            case 'harga_unit':
                $rapData['harga_unit'][] = $value;
                break;

            case 'target_market':
                $rapData['target_market'][] = $value;
                break;

            case 'kerjasama_dengan_bank':
                $rapData['kerjasama_dengan_bank'][] = $value;
                break;

            case 'rute_menuju_lokasi':
                $rapData['rute_menuju_lokasi'] = $value;
                break;

            case 'lainnya_developer':
                $rapData['lainnya_developer'][] = $value;
                break;

            case 'lainnya_kondisi':
                $rapData['lainnya_kondisi'][] = $value;
                break;

            case 'keterangan':
                if (!isset($rapData['fakta']->keterangan)) {
                    $rapData['fakta']->keterangan = [];
                }
                $rapData['fakta']->keterangan[] = $value;
                break;
            case 'Pondasi':
            case 'Struktur':
            case 'Rangka Atap':
            case 'Tutup Atap':
            case 'Plafond':
            case 'Dinding':
            case 'Pelapis Dinding':
            case 'Pintu Jendela':
            case 'Lantai':
                if (!isset($rapData['partisi']->$key)) {
                    $rapData['partisi']->$key = new \stdClass();
                }
                $rapData['partisi']->$key->nama = $key;
                $rapData['partisi']->$key->value = $value;
                break;

                // Asset
            case 'debitur_perwakilan':
                if (!isset($rapData['asset']->debitur_perwakilan)) {
                    $rapData['asset']->debitur_perwakilan = [];
                }
                $rapData['asset']->debitur_perwakilan[] = $value;
                break;

            case 'jenis_asset_sesuai':
                if (!isset($rapData['asset']->jenis_asset)) {
                    $rapData['asset']->jenis_asset = new \stdClass();
                }
                $rapData['asset']->jenis_asset->sesuai = $value;
                break;

            case 'alamat_sesuai':
                if (!isset($rapData['asset']->alamat)) {
                    $rapData['asset']->alamat = new \stdClass();
                    $rapData['asset']->alamat->sesuai = new \stdClass();
                }
                $rapData['asset']->alamat->sesuai->address = $value;
                break;

            case 'hub_cadeb_sesuai':
                if (!isset($rapData['asset']->hub_cadeb)) {
                    $rapData['asset']->hub_cadeb = new \stdClass();
                }
                $rapData['asset']->hub_cadeb->sesuai = $value;
                break;

            case 'hub_cadeb_penghuni_sesuai':
                if (!isset($rapData['asset']->hub_cadeb_penghuni)) {
                    $rapData['asset']->hub_cadeb_penghuni = new \stdClass();
                }
                $rapData['asset']->hub_cadeb_penghuni->sesuai = $value;
                break;

            case 'pihak_bank':
                $rapData['asset']->pihak_bank = $value;
                break;

            case 'nomor_nib':
                $rapData['asset']->nomor_nib = $value;
                break;

            case 'kordinat_lng':
                $rapData['asset']->kordinat_lng = $value;
                break;

            case 'kordinat_lat':
                $rapData['asset']->kordinat_lat = $value;
                break;

                // Fakta
            case 'fakta_positif':
                if (!isset($rapData['fakta']->fakta_positif)) {
                    $rapData['fakta']->fakta_positif = [];
                }
                $rapData['fakta']->fakta_positif[] = $value;
                break;

            case 'fakta_negatif':
                if (!isset($rapData['fakta']->fakta_negatif)) {
                    $rapData['fakta']->fakta_negatif = [];
                }
                $rapData['fakta']->fakta_negatif[] = $value;
                break;

            case 'rute_menuju':
                if (!isset($rapData['fakta']->rute_menuju)) {
                    $rapData['fakta']->rute_menuju = [];
                }
                $rapData['fakta']->rute_menuju[] = $value;
                break;

            case 'batas_batas':
                if (!isset($rapData['fakta']->batas_batas)) {
                    $rapData['fakta']->batas_batas = [];
                }
                $rapData['fakta']->batas_batas[] = $value;
                break;

            case 'kondisi_lingkungan':
                if (!isset($rapData['fakta']->kondisi_lingkungan)) {
                    $rapData['fakta']->kondisi_lingkungan = [];
                }
                $rapData['fakta']->kondisi_lingkungan[] = $value;
                break;

            case 'peruntukan':
                $rapData['fakta']->peruntukan = $value;
                break;

            case 'kdb':
                $rapData['fakta']->kdb = $value;
                break;

            case 'kdh':
                $rapData['fakta']->kdh = $value;
                break;

            case 'gsb':
                $rapData['fakta']->gsb = $value;
                break;

            case 'max_lantai':
                $rapData['fakta']->max_lantai = $value;
                break;

            case 'klb':
                $rapData['fakta']->klb = $value;
                break;

            case 'gss':
                $rapData['fakta']->gss = $value;
                break;

            case 'pelebaran_jalan':
                $rapData['fakta']->pelebaran_jalan = $value;
                break;

            case 'nama_petugas':
                $rapData['fakta']->nama_petugas = $value;
                break;

            default:
                break;
        }
    }
    $dataToInsertJson = json_encode($rapData, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
    return $dataToInsertJson;

}



function createInspeksiKendaraan($rows)
{
    $buildDataJsonKendaraan = [];

    // Struktur kendaraan sesuai dengan data JSON target
    $kendaraan = [
        'action' => $rows['action'] ?? null,
        'tanggal_survey' => $rows['tanggal_survey'] ?? null,
        'nama_wakil' => $rows['nama_wakil'] ?? null,
        'hub_calon_debitur' => $rows['hub_calon_debitur'] ?? null,
        'nama_jalan' => $rows['nama_jalan'] ?? null,
        'perumahan_gang' => $rows['perumahan_gang'] ?? null,
        'blok_nomor' => $rows['blok_nomor'] ?? null,
        'alamat' => [
            'sesuai' => $rows['alamat']['sesuai'] ?? [
                "village_code" => null,
                "district_code" => null,
                "city_code" => null,
                "province_code" => null
            ],
            'tidak_sesuai' => $rows['alamat']['tidak_sesuai'] ?? null
        ],
        'masa_stnk' => $rows['masa_stnk'] ?? null,
        'masa_pajak' => $rows['masa_pajak'] ?? null,
        'kendaraan' => $rows['kendaraan'] ?? [],
        'kendaraan_input' => $rows['kendaraan_input'] ?? [],
        'kondisi' => $rows['kondisi'] ?? null,
        'nomor_polisi' => $rows['nomor_polisi'] ?? null,
        'nomor_polis_tidak_sesuai' => $rows['nomor_polis_tidak_sesuai'] ?? null,
        'merek' => $rows['merek'] ?? null,
        'merek_tidak_sesuai' => $rows['merek_tidak_sesuai'] ?? null,
        'warna' => $rows['warna'] ?? null,
        'warna_tidak_sesuai' => $rows['warna_tidak_sesuai'] ?? null,
        'nomor_rangka' => $rows['nomor_rangka'] ?? null,
        'nomor_rangka_tidak_sesuai' => $rows['nomor_rangka_tidak_sesuai'] ?? null,
        'nomor_mesin' => $rows['nomor_mesin'] ?? null,
        'nomor_mesin_tidak_sesuai' => $rows['nomor_mesin_tidak_sesuai'] ?? null,
        'posisi_kilometer' => $rows['posisi_kilometer'] ?? null,
        'transmisi' => $rows['transmisi'] ?? [],
        'transmisi_input' => $rows['transmisi_input'] ?? [],
        'mesin_panel_instrument' => $rows['mesin_panel_instrument'] ?? [],
        'mesin_panel_instrument_input' => $rows['mesin_panel_instrument_input'] ?? [],
        'fungsi_mesin_panel_instrument' => $rows['fungsi_mesin_panel_instrument'] ?? [],
        'fungsi_mesin_panel_instrument_input' => $rows['fungsi_mesin_panel_instrument_input'] ?? [],
        'interior' => $rows['interior'] ?? [],
        'interior_input' => $rows['interior_input'] ?? [],
        'jumlah_pintu' => $rows['jumlah_pintu'] ?? [],
        'jumlah_pintu_input' => $rows['jumlah_pintu_input'] ?? [],
        'rangka_karoseri' => $rows['rangka_karoseri'] ?? [],
        'rangka_karoseri_input' => $rows['rangka_karoseri_input'] ?? [],
        'ban' => $rows['ban'] ?? [],
        'ban_input' => $rows['ban_input'] ?? [],
        'velg' => $rows['velg'] ?? [],
        'velg_input' => $rows['velg_input'] ?? [],
        'bamper_depan' => $rows['bamper_depan'] ?? [],
        'bamper_depan_input' => $rows['bamper_depan_input'] ?? [],
        'bamper_belakang' => $rows['bamper_belakang'] ?? [],
        'bamper_belakang_input' => $rows['bamper_belakang_input'] ?? [],
        'lampu_depan' => $rows['lampu_depan'] ?? [],
        'lampu_depan_input' => $rows['lampu_depan_input'] ?? [],
        'lampu_belakang' => $rows['lampu_belakang'] ?? [],
        'lampu_belakang_input' => $rows['lampu_belakang_input'] ?? [],
        'kaca_kendaraan' => $rows['kaca_kendaraan'] ?? [],
        'kaca_kendaraan_input' => $rows['kaca_kendaraan_input'] ?? [],
        'air_conditioner' => $rows['air_conditioner'] ?? [],
        'air_conditioner_input' => $rows['air_conditioner_input'] ?? [],
        'tape_radio_cd' => $rows['tape_radio_cd'] ?? [],
        'tape_radio_cd_input' => $rows['tape_radio_cd_input'] ?? [],
        'sensor_parkir' => $rows['sensor_parkir'] ?? [],
        'sensor_parkir_input' => $rows['sensor_parkir_input'] ?? [],
        'sensor_camera_recorder' => $rows['sensor_camera_recorder'] ?? [],
        'sensor_camera_recorder_input' => $rows['sensor_camera_recorder_input'] ?? [],
        'lcd' => $rows['lcd'] ?? [],
        'lcd_input' => $rows['lcd_input'] ?? [],
        'sabuk_keselamatan' => $rows['sabuk_keselamatan'] ?? [],
        'sabuk_keselamatan_input' => $rows['sabuk_keselamatan_input'] ?? [],
        'airbag' => $rows['airbag'] ?? [],
        'airbag_input' => $rows['airbag_input'] ?? [],
        'asuransi' => $rows['asuransi'] ?? [],
        'asuransi_input' => $rows['asuransi_input'] ?? [],
        'perusahaan_asuransi' => $rows['perusahaan_asuransi'] ?? null,
        'tahun_berakhir' => $rows['tahun_berakhir'] ?? null,
        'fakta_positif' => $rows['fakta_positif'] ?? [],
        'fakta_negatif' => $rows['fakta_negatif'] ?? [],
        'analisa_makro' => $rows['analisa_makro'] ?? [],
        'kesimpulan' => $rows['kesimpulan'] ?? [],
        'catatan' => $rows['catatan'] ?? []
    ];

    // Tambahkan ke array build
    $buildDataJsonKendaraan = json_encode($kendaraan, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);


    return $buildDataJsonKendaraan;
}

function createInspeksiAlatBerat($rows)
{
    $buildDataJson = [];

    // Pastikan rows selalu dalam bentuk array
    if (!isset($rows[0])) {
        $rows = [$rows];
    }

    foreach ($rows as $row) {
        $data = [
            'action' => $row['action'] ?? null,

            // Informasi Survey
            'tanggal_survey' => $row['tanggal_survey'] ?? null,
            'nama_wakil' => $row['nama_wakil'] ?? null,
            'hub_calon_debitur' => $row['hub_calon_debitur'] ?? null,
            'dengan_wadeb' => $row['dengan_wadeb'] ?? null,

            // Alamat
            'nama_jalan' => $row['nama_jalan'] ?? null,
            'perumahan_gang' => $row['perumahan_gang'] ?? null,
            'blok_nomor' => $row['blok_nomor'] ?? null,
            'alamat' => [
                'sesuai' => isset($row['alamat']['sesuai']) ? (array)$row['alamat']['sesuai'] : [
                    "village_code" => null,
                    "district_code" => null,
                    "city_code" => null,
                    "province_code" => null
                ],
                'tidak_sesuai' => $row['alamat']['tidak_sesuai'] ?? null
            ],

            // Data Unit
            'jenis_model' => $row['jenis_model'] ?? [],
            'nomor_lambung' => $row['nomor_lambung'] ?? null,
            'model_unit' => $row['model_unit'] ?? null,
            'tahun_pembuatan' => $row['tahun_pembuatan'] ?? null,
            'merk' => $row['merk'] ?? null,
            'negara_pembuat' => $row['negara_pembuat'] ?? null,
            'tahun_pembelian' => $row['tahun_pembelian'] ?? null,
            'nomor_faktur' => $row['nomor_faktur'] ?? null,
            'nomor_kontrak' => $row['nomor_kontrak'] ?? null,
            'nama_pemilik' => $row['nama_pemilik'] ?? null,
            'alamat_pemilik' => $row['alamat_pemilik'] ?? null,
            'nomor_asuransi' => $row['nomor_asuransi'] ?? null,
            'nomor_rangka' => $row['nomor_rangka'] ?? null,
            'nomor_mesin' => $row['nomor_mesin'] ?? null,
            'hour_meters' => $row['hour_mesters'] ?? null,
            'overhaul_mesin' => $row['overhaul_mesin'] ?? null,

            // Kondisi Unit
            'mesin_panel' => $row['mesin_panel'] ?? [],
            'mesin_panel_input' => $row['mesin_panel_input'] ?? [],
            'fungsi_panel' => $row['fungsi_panel'] ?? [],
            'fungsi_panel_input' => $row['fungsi_panel_input'] ?? [],
            'interior' => $row['interior'] ?? [],
            'interior_input' => $row['interior_input'] ?? [],
            'rangka_Karoseri' => $row['rangka_Karoseri'] ?? [],
            'rangka_Karoseri_input' => $row['rangka_Karoseri_input'] ?? [],
            'ban' => $row['ban'] ?? [],
            'ban_input' => $row['ban_input'] ?? null,
            'velg' => $row['velg'] ?? [],
            'velg_input' => $row['velg_input'] ?? [],
            'air_conditioner' => $row['air_conditioner'] ?? [],
            'air_conditioner_input' => $row['air_conditioner_input'] ?? [],
            'aksesoris' => $row['aksesoris'] ?? [],
            'aksesoris_input' => $row['aksesoris_input'] ?? [],
            'lcd' => $row['lcd'] ?? [],
            'lcd_input' => $row['lcd_input'] ?? null,
            'perlengkapan' => $row['perlengkapan'] ?? [],
            'perlengkapan_input' => $row['perlengkapan_input'] ?? [],
            'asuransi' => $row['asuransi'] ?? [],
            'asuransi_input' => $row['asuransi_input'] ?? [],
            'perusahaan_asuransi' => $row['perusahaan_asuransi'] ?? null,
            'tahun_berakhir' => $row['tahun_berakhir'] ?? null,

            // Keselamatan & Fasilitas
            'sensor_kamera' => $row['sensor_kamera'] ?? null,
            'sabuk_keselamatan' => $row['sabuk_keselamatan'] ?? null,
            'air_bag' => $row['air_bag'] ?? null,

            // Analisa
            'fakta_positif' => $row['fakta_positif'] ?? [],
            'fakta_negatif' => $row['fakta_negatif'] ?? [],
            'kesimpulan' => $row['kesimpulan'] ?? null,
            'catatan' => $row['catatan'] ?? null,

            // Info Tambahan
            'bangunan' => $row['bangunan'] ?? [],
            'tanah' => $row['tanah'] ?? [],
            'nama' => $row['nama'] ?? null,
            'type' => $row['type'] ?? null,
            'warna' => $row['warna'] ?? null,
            'lokasi' => $row['lokasi'] ?? null,
            'total' => $row['total'] ?? null,
            'diskon' => $row['diskon'] ?? null,
            'harga_diskon' => $row['harga_diskon'] ?? null,
            'sumber_data' => $row['sumber_data'] ?? null,
            'harga' => $row['harga'] ?? null,
            'tahun' => $row['tahun'] ?? null,
            'transmisi' => $row['transmisi'] ?? null,
            'kordinat_lat' => $row['kordinat_lat'] ?? null,
            'kordinat_lng' => $row['kordinat_lng'] ?? null,
            'merek_buatan' => $row['merek_buatan'] ?? null,
            'kapasitas' => $row['kapasitas'] ?? null,
            'power' => $row['power'] ?? null,
            'kondisi' => $row['kondisi'] ?? null,
        ];

        $buildDataJson[] = $data;
    }

    $buildDataJson = json_encode($buildDataJson, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
    return $buildDataJson;
}

function createInspeksiKapal($rows)
{
    $buildDataJson = [];

    // Jika input hanya satu baris, ubah jadi array agar bisa di-loop
    if (!isset($rows[0])) {
        $rows = [$rows];
    }

    foreach ($rows as $row) {
        $data = [
            'action' => $row['action'] ?? null,

            // Identitas Wakil
            'nama_wakil_debitur' => $row['nama_wakil_debitur'] ?? null,
            'hub_calon_debitur' => $row['hub_calon_debitur'] ?? null,

            // Alamat
            'dermaga' => $row['dermaga'] ?? null,
            'nama_jalan' => $row['nama_jalan'] ?? null,
            'perumahan_gang' => $row['perumahan_gang'] ?? null,
            'blok_nomor' => $row['blok_nomor'] ?? null,
            'bandara' => $row['bandara'] ?? null,
            'alamat' => [
                'sesuai' => isset($row['alamat']['sesuai']) ? (array)$row['alamat']['sesuai'] : [
                    'village_code' => null,
                    'district_code' => null,
                    'city_code' => null,
                    'province_code' => null
                ],
                'tidak_sesuai' => $row['alamat']['tidak_sesuai'] ?? null
            ],

            // Data Kapal
            'jenis_kapal' => $row['jenis_kapal'] ?? [],
            'jenis_kapal_lainnya' => $row['jenis_kapal_lainnya'] ?? null,
            'size' => $row['size'] ?? [],
            'kondisi' => $row['kondisi'] ?? null,
            'klasifikasi' => $row['klasifikasi'] ?? [],
            'nama_kapal' => $row['nama_kapal'] ?? null,
            'pemilik_kapal' => $row['pemilik_kapal'] ?? null,
            'bendera' => $row['bendera'] ?? null,
            'nomor_selar' => $row['nomor_selar'] ?? null,
            'kapal' => $row['kapal'] ?? null,
            'galangan_kapal' => $row['galangan_kapal'] ?? null,
            'kapal_shipyard' => $row['kapal_shipyard'] ?? null,
            'tahun_pembuatan' => $row['tahun_pembuatan'] ?? null,
            'tahun_launcing' => $row['tahun_launcing'] ?? null,
            'dwt' => $row['dwt'] ?? null,
            'lwt' => $row['lwt'] ?? null,
            'gross_tonnage' => $row['gross_tonnage'] ?? null,
            'net_tonnage' => $row['net_tonnage'] ?? null,
            'tenaga_mesin' => $row['tenaga_mesin'] ?? null,
            'loa' => $row['loa'] ?? null,
            'lbp' => $row['lbp'] ?? null,
            'beam' => $row['beam'] ?? null,
            'depth' => $row['depth'] ?? null,
            'draft' => $row['draft'] ?? null,

            // Struktur
            'lambung_kapal' => $row['lambung_kapal'] ?? null,
            'dek' => $row['dek'] ?? null,
            'struktur_rangka' => $row['struktur_rangka'] ?? null,
            'palka' => $row['palka'] ?? null,
            'pondasi_mesin' => $row['pondasi_mesin'] ?? null,
            'area_mesin' => $row['area_mesin'] ?? null,
            'cat_dan_korosi' => $row['cat_dan_korosi'] ?? null,
            'sistem_pengelasan' => $row['sistem_pengelasan'] ?? null,
            'deskripsi_struktur' => $row['deskripsi_struktur'] ?? null,

            // Peralatan Keselamatan
            'sekoci' => $row['sekoci'] ?? null,
            'jaket_pelampung' => $row['jaket_pelampung'] ?? null,
            'alat_pemadaman' => $row['alat_pemadaman'] ?? null,
            'rambu_darurat' => $row['rambu_darurat'] ?? null,
            'sistem_alarm' => $row['sistem_alarm'] ?? null,
            'sistem_pencegah' => $row['sistem_pencegah'] ?? null,
            'kebakaran' => $row['kebakaran'] ?? null,
            'lampu_darurat' => $row['lampu_darurat'] ?? null,
            'deskripsi_peralatan' => $row['deskripsi_peralatan'] ?? null,

            // Navigasi
            'gps' => $row['gps'] ?? null,
            'radar' => $row['radar'] ?? null,
            'radio_komunikasi' => $row['radio_komunikasi'] ?? null,
            'lampu_navigasi' => $row['lampu_navigasi'] ?? null,
            'sistem_kendali_otomatis' => $row['sistem_kendali_otomatis'] ?? null,
            'kompas' => $row['kompas'] ?? null,
            'deskripsi_navigasi' => $row['deskripsi_navigasi'] ?? null,

            // Mesin Penggerak
            'mesin_utama' => $row['mesin_utama'] ?? null,
            'mesin_bantu' => $row['mesin_bantu'] ?? null,
            'pompa_pendingin' => $row['pompa_pendingin'] ?? null,
            'sistem_pelumasan' => $row['sistem_pelumasan'] ?? null,
            'propeller' => $row['propeller'] ?? null,
            'sistem_kelistrikan' => $row['sistem_kelistrikan'] ?? null,
            'deskripsi_mesin_penggerak' => $row['deskripsi_mesin_penggerak'] ?? null,

            // Kelistrikan
            'sistem_penerangan' => $row['sistem_penerangan'] ?? null,
            'sistem_panel_distribusi' => $row['sistem_panel_distribusi'] ?? null,
            'kabel_perangkat' => $row['kabel_perangkat'] ?? null,
            'deskripsi_kelistrikan' => $row['deskripsi_kelistrikan'] ?? null,

            // Kebersihan & Lingkungan
            'kebersihan_dek_luar' => $row['kebersihan_dek_luar'] ?? null,
            'tangki_limbah' => $row['tangki_limbah'] ?? null,
            'sistem_pengelolaan_limbah' => $row['sistem_pengelolaan_limbah'] ?? null,
            'pengelolaan_air_ballast' => $row['pengelolaan_air_ballast'] ?? null,
            'deskripsi_kebersihan' => $row['deskripsi_kebersihan'] ?? null,

            // Analisa
            'fakta_positif' => $row['fakta_positif'] ?? [],
            'fakta_negatif' => $row['fakta_negatif'] ?? [],
            'analisa_makro' => $row['analisa_makro'] ?? null,
            'kesimpulan' => $row['kesimpulan'] ?? null,
            'catatan' => $row['catatan'] ?? [],

            // Asset
            'bangunan' => $row['bangunan'] ?? [],
            'tanah' => $row['tanah'] ?? [],
            'asset' => $row['asset'] ?? null,

            // Info Tambahan
            'nama' => $row['nama'] ?? null,
            'type' => $row['type'] ?? null,
            'warna' => $row['warna'] ?? null,
            'lokasi' => $row['lokasi'] ?? null,
            'total' => $row['total'] ?? null,
            'diskon' => $row['diskon'] ?? null,
            'harga_diskon' => $row['harga_diskon'] ?? null,
            'sumber_data' => $row['sumber_data'] ?? null,
            'harga' => $row['harga'] ?? null,
            'tahun' => $row['tahun'] ?? null,
            'transmisi' => $row['transmisi'] ?? null,
            'kordinat_lat' => $row['kordinat_lat'] ?? null,
            'kordinat_lng' => $row['kordinat_lng'] ?? null,
            'merek_buatan' => $row['merek_buatan'] ?? null,
            'kapasitas' => $row['kapasitas'] ?? null,
            'power' => $row['power'] ?? null,
        ];

        $buildDataJson[] = $data;
    }

    $buildDataJson = json_encode($buildDataJson, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
    return $buildDataJson;
}
function createInspeksiPesawat($rows)
{
    $buildDataJson = [];

    // Pastikan rows selalu dalam bentuk array
    if (!isset($rows[0])) {
        $rows = [$rows];
    }

    foreach ($rows as $row) {
        $data = [
            'action' => $row['action'] ?? null,

            // Informasi Survey
            'tanggal_survey' => $row['tanggal_survey'] ?? null,
            'nama_wakil' => $row['nama_wakil'] ?? null,
            'hub_calon_debitur' => $row['hub_calon_debitur'] ?? null,

            // Alamat
            'nama_jalan' => $row['nama_jalan'] ?? null,
            'perumahan_gang' => $row['perumahan_gang'] ?? null,
            'blok_nomor' => $row['blok_nomor'] ?? null,
            'bandara' => $row['bandara'] ?? null,
            'alamat' => [
                'sesuai' => isset($row['alamat']['sesuai']) ? (array)$row['alamat']['sesuai'] : [
                    'village_code' => null,
                    'district_code' => null,
                    'city_code' => null,
                    'province_code' => null
                ],
                'tidak_sesuai' => $row['alamat']['tidak_sesuai'] ?? null
            ],

            // Data Pesawat
            'jenis_pesawat' => $row['jenis_pesawat'] ?? [],
            'jenis_pesawat_lainnya' => $row['jenis_pesawat_lainnya'] ?? null,
            'size' => $row['size'] ?? [],
            'kondisi' => $row['kondisi'] ?? [],
            'nama_pesawat' => $row['nama_pesawat'] ?? null,
            'model' => $row['model'] ?? null,
            'nomor_registrasi' => $row['nomor_registrasi'] ?? null,
            'tahun_pembuatan' => $row['tahun_pembuatan'] ?? null,
            'certificate_of_airworthines' => $row['certificate_of_airworthines'] ?? null,
            'certificate_of_registration' => $row['certificate_of_registration'] ?? null,
            'total_service_hours' => $row['total_service_hours'] ?? null,
            'total_service_cycles' => $row['total_service_cycles'] ?? null,

            // Maintenance Schedule
            'last_a_check' => $row['last_a_check'] ?? null,
            'next_a_check' => $row['next_a_check'] ?? null,
            'last_b_check' => $row['last_b_check'] ?? null,
            'next_b_check' => $row['next_b_check'] ?? null,
            'last_c_check' => $row['last_c_check'] ?? null,
            'next_c_check' => $row['next_c_check'] ?? null,
            'last_d_check' => $row['last_d_check'] ?? null,
            'next_d_check' => $row['next_d_check'] ?? null,
            'deskripsi_maintenence' => $row['deskripsi_maintenence'] ?? null,

            // Sistem Navigasi & Konfigurasi
            'instrument_landing_system' => $row['instrument_landing_system'] ?? null,
            'traffic_collision_avoidance_system' => $row['traffic_collision_avoidance_system'] ?? null,
            'windshear' => $row['windshear'] ?? null,
            'electronic_flight' => $row['electronic_flight'] ?? null,
            'winglets' => $row['winglets'] ?? null,
            'deskripsi_konfigurasi' => $row['deskripsi_konfigurasi'] ?? null,

            // Interior Kabin
            'maksimal_penumpang' => $row['maksimal_penumpang'] ?? null,
            'jumlah_kursi' => $row['jumlah_kursi'] ?? null,
            'kursi_pramugari_pramugara' => $row['kursi_pramugari_pramugara'] ?? null,
            'kartu_fitur_keselamatan' => $row['kartu_fitur_keselamatan'] ?? null,
            'sabuk_pengaman' => $row['sabuk_pengaman'] ?? null,
            'lampu_kabin' => $row['lampu_kabin'] ?? null,
            'lampu_pintu_keluar' => $row['lampu_pintu_keluar'] ?? null,
            'intercom_kabin' => $row['intercom_kabin'] ?? null,
            'deskripsi_kabin' => $row['deskripsi_kabin'] ?? null,

            // Struktur Pesawat
            'badan_pesawat' => $row['badan_pesawat'] ?? null,
            'sayap_pesawat' => $row['sayap_pesawat'] ?? null,
            'ekor_pesawat' => $row['ekor_pesawat'] ?? null,
            'landing_gear' => $row['landing_gear'] ?? null,
            'sistem_pengelasan' => $row['sistem_pengelasan'] ?? null,
            'deskripsi_struktur' => $row['deskripsi_struktur'] ?? null,

            // Navigasi
            'gps' => $row['gps'] ?? null,
            'radar' => $row['radar'] ?? null,
            'radio_komunikasi' => $row['radio_komunikasi'] ?? null,
            'lampu_navigasi' => $row['lampu_navigasi'] ?? null,
            'sistem_autopilot' => $row['sistem_autopilot'] ?? null,
            'deskripsi_navigasi' => $row['deskripsi_navigasi'] ?? null,

            // Bahan Bakar & Hidrolik
            'tangki_bahan_bakar' => $row['tangki_bahan_bakar'] ?? null,
            'saluran_pipa_bahan_bakar' => $row['saluran_pipa_bahan_bakar'] ?? null,
            'pompa_bahan_bakar' => $row['pompa_bahan_bakar'] ?? null,
            'sistem_hidrolik_utama' => $row['sistem_hidrolik_utama'] ?? null,
            'sistem_pendigin_hidrolik' => $row['sistem_pendigin_hidrolik'] ?? null,
            'deskripsi_hidrolik' => $row['deskripsi_hidrolik'] ?? null,

            // Mesin Penggerak
            'mesin_utama' => $row['mesin_utama'] ?? null,
            'sistem_pendorong' => $row['sistem_pendorong'] ?? null,
            'sistem_pendigin_mesin' => $row['sistem_pendigin_mesin'] ?? null,
            'sistem_pelumasan' => $row['sistem_pelumasan'] ?? null,
            'filter_dan_perangkat_pendukung' => $row['filter_dan_perangkat_pendukung'] ?? null,
            'deskripsi_kondisi_mesin' => $row['deskripsi_kondisi_mesin'] ?? null,

            // Keselamatan Darurat
            'jaket_pelampung' => $row['jaket_pelampung'] ?? null,
            'pintu_darurat' => $row['pintu_darurat'] ?? null,
            'alat_pemadaman_kebakaran' => $row['alat_pemadaman_kebakaran'] ?? null,
            'sistem_alaram_darurat' => $row['sistem_alaram_darurat'] ?? null,
            'sekoci' => $row['sekoci'] ?? null,
            'masker_oxigen' => $row['masker_oxigen'] ?? null,
            'deskripsi_fungsi_keselamatan' => $row['deskripsi_fungsi_keselamatan'] ?? null,

            // Interior Tambahan
            'sistem_ventilasi_ac' => $row['sistem_ventilasi_ac'] ?? null,
            'sistem_penerangan_kabin' => $row['sistem_penerangan_kabin'] ?? null,
            'panel_informasi_penumpang' => $row['panel_informasi_penumpang'] ?? null,
            'sistem_hiburan_kabin' => $row['sistem_hiburan_kabin'] ?? null,
            'deskripsi_Interior' => $row['deskripsi_Interior'] ?? null,

            // Analisa
            'fakta_positif' => $row['fakta_positif'] ?? [],
            'fakta_negatif' => $row['fakta_negatif'] ?? [],
            'kesimpulan' => $row['kesimpulan'] ?? null,
            'catatan' => $row['catatan'] ?? []
        ];

        $buildDataJson[] = $data;
    }
    $buildDataJson = json_encode($buildDataJson, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
    return $buildDataJson;
}

function createInspeksiMesin($rows)
{
    $buildDataJson = [];

    // Pastikan input selalu array
    if (!isset($rows[0])) {
        $rows = [$rows];
    }

    foreach ($rows as $row) {
        $data = [
            'action' => $row['action'] ?? null,
            'nama_wakil' => $row['nama_wakil'] ?? null,
            'nama_jalan' => $row['nama_jalan'] ?? null,
            'perumahan_gang' => $row['perumahan_gang'] ?? null,
            'blok' => $row['blok'] ?? null,
            'desa_kelurahan' => $row['desa_kelurahan'] ?? null,
            'kecamatan' => $row['kecamatan'] ?? null,
            'kota_madya' => $row['kota_madya'] ?? null,
            'provinsi' => $row['provinsi'] ?? null,
            'hub_calon_debitur' => $row['hub_calon_debitur'] ?? null,
            'tipe_model' => $row['tipe_model'] ?? null,
            'merek' => $row['merek'] ?? null,
            'tahun_pembuatan' => $row['tahun_pembuatan'] ?? null,
            'negara_pembuat' => $row['negara_pembuat'] ?? null,
            'kondisi_mesin' => $row['kondisi_mesin'] ?? null,
            'faktor_positif' => is_array($row['faktor_positif']) ? $row['faktor_positif'] : [],
            'faktor_negatif' => is_array($row['faktor_negatif']) ? $row['faktor_negatif'] : [],
            'kesimpulan' => $row['kesimpulan'] ?? null,
            'catatan' => is_array($row['catatan']) ? $row['catatan'] : []
        ];

        $buildDataJson[] = $data;
    }
    $buildDataJson = json_encode($buildDataJson, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
    return $buildDataJson;
}


function createInspeksiRap($rows)
{
    $buildDataJson = [];

    // Pastikan input selalu dalam bentuk array
    if (!isset($rows[0])) {
        $rows = [$rows];
    }

    foreach ($rows as $row) {
        $data = [
            'perizinan' => isset($row['perizinan']) && is_array($row['perizinan']) ? $row['perizinan'] : [],
            'brosur_price_list' => isset($row['brosur_price_list']) && is_array($row['brosur_price_list']) ? $row['brosur_price_list'] : [],
            'pengalaman_developer' => $row['pengalaman_developer'] ?? null,
            'developer_anggota' => $row['developer_anggota'] ?? null,
            'lainnya_developer' => $row['lainnya_developer'] ?? null,
            'kapan_mulai_dibangun' => $row['kapan_mulai_dibangun'] ?? null,
            'kondisi_perumahan' => $row['kondisi_perumahan'] ?? null,
            'progres_pembangunan' => $row['progres_pembangunan'] ?? null,
            'kontraktor' => $row['kontraktor'] ?? null,
            'lingkungan_sekitar' => $row['lingkungan_sekitar'] ?? null,
            'komplek_disekitar' => $row['komplek_disekitar'] ?? null,
            'pusat_keramaian' => $row['pusat_keramaian'] ?? null,
            'transportasi_umum' => $row['transportasi_umum'] ?? null,
            'lainnya_kondisi' => $row['lainnya_kondisi'] ?? null,
            'partisi' => is_array($row['partisi']) ? $row['partisi'] : [],
            'jumlah_unit' => $row['jumlah_unit'] ?? null,
            'batas_batas_perumahan' => $row['batas_batas_perumahan'] ?? null,
            'fasus_fasum' => $row['fasus_fasum'] ?? null,
            'progres_penjualan' => $row['progres_penjualan'] ?? null,
            'harga_unit' => $row['harga_unit'] ?? null,
            'target_market' => $row['target_market'] ?? null,
            'kerjasama_dengan_bank' => $row['kerjasama_dengan_bank'] ?? null,
            'rute_menuju_lokasi' => $row['rute_menuju_lokasi'] ?? null,
            'properti_sejenis' => $row['properti_sejenis'] ?? null
        ];

        $buildDataJson[] = $data;
    }

    return $buildDataJson;
}
