<?php
$cleanNotificationUrlScript = <<<'HTML'
<script>
    (function () {
        if (!window.history || !window.history.replaceState) {
            return;
        }

        var url = new URL(window.location.href);
        var params = url.searchParams;
        var page = params.get("page");
        var shouldClean = params.has("msg") || params.has("alert") || page;

        if (!shouldClean) {
            return;
        }

        params.delete("msg");
        params.delete("alert");

        if (page) {
            params.delete("page");
            url.pathname = "/" + encodeURIComponent(page);
        } else if (/\/index(?:\.php)?$/i.test(url.pathname)) {
            url.pathname = "/";
        } else if (/\/login\.php$/i.test(url.pathname)) {
            url.pathname = "/login";
        }

        url.search = params.toString();
        window.history.replaceState({}, document.title, url.pathname + url.search + url.hash);
    })();
</script>
HTML;

echo $cleanNotificationUrlScript;

if (isset($_GET['msg'])) {
    $msg = intval($_GET['msg']);
    $messages = [
        3 => ["success", "Selamat!", "Login berhasil."],
        5 => ["error", "Opps!", "Isi semua kolom."],
        6 => ["error", "Opps!", "Akun Anda diblokir."],
        7 => ["error", "Opps!", "Username atau password salah."],
        8 => ["error", "Opps!", "Akun tidak ditemukan."],
        9 => ["error", "Opps!", "Agen dengan kode ini sudah terdaftar."],
        10 => ["error", "Opps!", "Agen dengan nama dan upline yang sama sudah ada."],
        11 => ["error", "Opps!", "Nama agen dan password tidak boleh kosong."],
        12 => ["success", "Berhasil!", "Agen berhasil ditambahkan."],
        13 => ["error", "Opps!", "Terjadi kesalahan, coba lagi."],
        14 => ["error", "Opps!", "Level agen tidak valid."],
        15 => ["success", "Berhasil!", "Update Data Agent Berhasil."],
        16 => ["error", "Opps!", "Format Tidak valid"],
        17 => ["error", "Opps!", "Balance tidak boleh nol"],
        18 => ["error", "Opps!", "Saldo tidak cukup"],
        19 => ["success", "Berhasil!", "Add Balance berhasil"],
        20 => ["error", "Opps!", "Gagal melakukan Add Balance"],
        21 => ["success", "Berhasil!", "Reduce Balance berhasil"],
        22 => ["error", "Opps!", "Gagal melakukan Reduce Balance"],
        // topup Notification
        23 => ["error", "Opps!", "Pilih Metode Terlebih dahulu!"],
        24 => ["error", "Opps!", "Tidak Ada Akses."],
        25 => ["error", "Opps!", "Balance Harus Di Isi"],
        26 => ["error", "Opps!", "Masih ada data pending."],
        27 => ["error", "Opps!", "Harus Sesuai Dengan Minimal"],
        28 => ["error", "Opps!", "Metode Tidak Ditemukan"],
        29 => ["success", "Berhasil!", "Top-up berhasil, menunggu konfirmasi"],
        30 => ["error", "Opps!", "Invalid IP Address Format"],
        31 => ["success", "Berhasil!", "Anda Berhasil Keluar"],
        // GAME Notification
        32 => ["error", "Opps!", "Data Sudah Ada"],
        33 => ["error", "Opps!", "Gagal Menambahkan Data"],
        34 => ["success", "Berhasil!", "Berhasil Menambahkan Data"],
        35 => ["error", "Opps!", "Invalid IP Address Format"],
        // WEBSITE AND BANK
        36 => ["error", "Opps!", "All fields are required!"],
        37 => ["error", "Opps!", "Min Topup must be a valid number!"],
        38 => ["success", "Berhasil!", "Website settings updated successfully!"],
        39 => ["error", "Opps!", "Invalid Request"],
        40 => ["success", "Berhasil!", "Bank updated successfully!"],
        41 => ["error", "Opps!", "Bank Not Found"],
        
        42 => ["error", "Opps!", "Invalid Bank Name"],
        43 => ["error", "Opps!", "Invalid Account Number"],
        44 => ["success", "Berhasil!", "Website settings updated successfully!"],
        45 => ["error", "Opps!", "Invalid Bank"],
        46 => ["success", "Berhasil!", "Bank updated successfully!"],
        47 => ["error", "Opps!", "Bank Not Found"],
        
        48 => ["error", "Opps!", "Blocked Agent Fail"],
        49 => ["success", "Berhasil!", "Agent Blocked successfully!"],
        50 => ["error", "Opps!", "Nomor WhatsApp tidak valid."],
        51 => ["success", "Berhasil!", "Konfigurasi API dan WhatsApp berhasil disimpan."],
        52 => ["error", "Opps!", "Field WhatsApp harus diisi lengkap atau dikosongkan semua."],
        53 => ["error", "Opps!", "Data topup sudah diproses atau tidak ditemukan."],
        54 => ["error", "Opps!", "Field WhatsApp Bridge harus diisi lengkap atau dikosongkan semua."],
        55 => ["error", "Opps!", "Nomor admin WhatsApp wajib diisi saat bot diaktifkan."],
        56 => ["error", "Opps!", "Nomor tujuan test WhatsApp tidak valid."],
        57 => ["error", "Opps!", "Pesan test WhatsApp tidak boleh kosong."],
        58 => ["error", "Opps!", "Konfigurasi WhatsApp belum aktif."],
        59 => ["error", "Opps!", "Test kirim WhatsApp gagal. Cek token, device, atau whatsapp.log."],
        60 => ["success", "Berhasil!", "Test kirim WhatsApp berhasil diproses."],
        61 => ["success", "Berhasil!", "Kode OTP WhatsApp berhasil dikirim."],
        62 => ["error", "Opps!", "Gagal mengirim kode OTP WhatsApp."],
        63 => ["error", "Opps!", "Kode OTP salah atau sudah kedaluwarsa."],
        64 => ["success", "Berhasil!", "Nomor WhatsApp berhasil diverifikasi."],
        65 => ["error", "Opps!", "Konfigurasi WhatsApp belum aktif, OTP belum bisa dikirim."],
        66 => ["warning", "Perhatian!", "Nomor WhatsApp berubah. Silakan verifikasi ulang."],
        67 => ["warning", "Tunggu Dulu!", "OTP baru bisa dikirim lagi setelah beberapa saat."],
        68 => ["warning", "Verifikasi Wajib!", "Isi dan verifikasi nomor WhatsApp dulu sebelum lanjut."],
        69 => ["error", "Opps!", "Kode OTP wajib diisi."],
        70 => ["error", "Opps!", "Nomor WhatsApp belum diisi."],
        71 => ["warning", "Device Belum Siap!", "OTP belum bisa dikirim karena device WhatsApp belum connect. Scan QR dulu di Settings admin."],
        72 => ["error", "Opps!", "Konfigurasi QRIS otomatis harus diisi lengkap atau dikosongkan semua."],
        73 => ["error", "Opps!", "Gagal membuat QRIS otomatis. Cek API key, merchant code, secret key, atau koneksi server."],
        74 => ["success", "Berhasil!", "QRIS berhasil dibuat. Silakan selesaikan pembayaran."],
        75 => ["error", "Opps!", "Favicon tidak valid. Gunakan PNG, JPG, GIF, WEBP, atau ICO maksimal 2MB."],
        76 => ["error", "Opps!", "Foto provider tidak valid. Gunakan PNG, JPG, GIF, atau WEBP maksimal 2MB."],
        77 => ["error", "Opps!", "Rate coin harus berupa angka persen lebih dari 0."],
    ];

    if (isset($messages[$msg])) {
        list($icon, $title, $text) = $messages[$msg];
        echo "<script>
            Swal.fire({
                icon: '$icon',
                title: '$title',
                text: '$text',
                toast: true,
                position: 'top-end',
                showConfirmButton: false,
                timer: 3000
            });
        </script>";
    }
}
?>
