<script type="text/javascript">
    window.WebConfig = <?php echo json_encode(general(), 15, 512) ?>;
</script>
<style>
.wa-icon {
    display: none; /* Sembunyikan dari awal */
    /* opacity: 0; visibility: hidden; -> Gunakan ini jika template bosku pakai sistem class active */
}
</style>
<script type="text/javascript">
  var loadScript = (e) => new Promise((resolve, reject) => {
    var s = document.createElement("script");
    s.src = e + '?' + Math.random();
    s.defer = true;
    s.onload = function() {
      resolve(true);
    };
    
    var target = document.body || document.head || document.documentElement;
    target.appendChild(s);
  });

  document.addEventListener("DOMContentLoaded", function() {
      loadScript('<?php echo e(asset("func/jsx/global.js?v=2")); ?>').then(() => {
        if (typeof globalObj !== 'undefined' && globalObj.isLaunchEvent) {
          let intv = setInterval(() => {
            if (typeof webTracker != 'undefined') {
              clearInterval(intv);
              webTracker.trackPublic();
            }
          });
        }
        loadScript('<?php echo e(asset("func/jsx/cms.js?v=2")); ?>').then(() => {
          ['<?php echo e(asset("assets/js/common.js")); ?>?v=2', '<?php echo e(asset("assets/js/template.js?v=2")); ?>'].forEach(e => {
            var s = document.createElement("script");
            s.src = e;
            s.defer = true;
            
            var target = document.body || document.head || document.documentElement;
            target.appendChild(s);
          });
        });
      });
  });

  function inViewport(element) {
    if (!element || element.nodeType !== 1) return false;
    var html = document.documentElement;
    var rect = element.getBoundingClientRect();
    return rect && rect.bottom >= 0 && rect.right >= 0 && rect.left <= html.clientWidth && rect.top <= html.clientHeight;
  }

  function loadStyle(force) {
    if (!$('.social-logo').hasClass('loaded') && (force || inViewport($('.social-logo')[0]))) {
      $('.social-logo').addClass('loaded');
      $('.faw').attr('rel', 'stylesheet').attr('media', 'all');
    }
  }
  
  document.addEventListener("scroll", () => loadStyle());
  
  window.addEventListener('load', function() {
    $('.nav__menu, .g8pop-login').click(() => loadStyle(true));
  });
</script>

<?php if(auth()->guard()->check()): ?>
<script type="text/javascript">
$(document).ready(function() {
    let $refreshBtns = $('.btn-refresh-balance');
    let $icons = $refreshBtns.find('i');
    let $balanceSpans = $('.g8-bal-total');

    // 1. Fungsi Utama Tarik Saldo
    function fetchBalance(showSpinner = false) {
        if (showSpinner) $icons.addClass('fa-spin');
        $refreshBtns.css('pointer-events', 'none'); 
        
        $.ajax({
            url: "<?php echo e(route('api.balance')); ?>", 
            type: "GET",
            success: function(response) {
                if (showSpinner) $icons.removeClass('fa-spin');
                $refreshBtns.css('pointer-events', 'auto'); 
                
                if (response && response.d) {
                    $balanceSpans.html(response.d);
                }
            },
            error: function() {
                if (showSpinner) $icons.removeClass('fa-spin');
                $refreshBtns.css('pointer-events', 'auto');
            }
        });
    }

    // 2. Refresh saat halaman pertama kali dimuat
    fetchBalance(false);

    // 3. Refresh manual saat diklik
    $refreshBtns.on('click', function(e) {
        e.preventDefault();
        fetchBalance(true); 
    });

    // ========================================================================
    // 🔥 FITUR BARU: AUTO-REFRESH SAAT KELUAR GAME / KEMBALI KE LOBI
    // ========================================================================
    
    // A. Deteksi jika pemain pindah Tab/Aplikasi lalu kembali ke Tab web kita
    document.addEventListener("visibilitychange", function() {
        // Jika status layar kembali terlihat (visible) oleh mata pemain
        if (document.visibilityState === 'visible') {
            console.log("Player kembali ke layar, Auto-Refresh saldo berjalan...");
            fetchBalance(true); // Putar spinner agar player tahu saldo sedang di-update
        }
    });

    // B. Deteksi jika pemain menekan tombol "Back" (Kembali) di browser dari halaman Game
    // (Mencegah browser meload halaman lama dari Cache yang saldonya masih 0)
    window.addEventListener('pageshow', function(event) {
        if (event.persisted) {
            console.log("Player menggunakan tombol Back, Auto-Refresh saldo berjalan...");
            fetchBalance(true);
        }
    });

});
</script>
<?php endif; ?>

<script src="<?php echo e(asset('js/jquery.marquee.min.js')); ?>" defer></script><?php /**PATH /www/wwwroot/website/sewa/9win2api/resources/views/frontend/partials/scripts.blade.php ENDPATH**/ ?>