$(function(){
    var _client_id = '89662634431-3fmfddg8hb9s8a992odnlf9a07ng23be.apps.googleusercontent.com';
    const url = window.location.href;
    const searchParams = new URLSearchParams(new URL(url).search);
    const rParam = searchParams.get('r');
    
    initGoogleScript()
    function init(fn) {
        gload(fn)
    }
    function initGoogleScript(){
        var t = document.createElement("script");
        t.setAttribute("async", ""),
        t.src = "https://accounts.google.com/gsi/client",
        document.body.appendChild(t);
        t.onload = function() {
            console.log(2);
            init(handleCredentialResponse)
        };
    }
    
    
    function gload(fn) {
        window.google.accounts.id.initialize({
            client_id: _client_id,
            callback: fn,
            locale: 'en-US'
        });
        window.google.accounts.id.renderButton(
                document.getElementById("g_id_signin2"), {
                theme: "filled_blue",
                size: 'large',
                width: '100%',
                height: '50',
                type: 'standard',
                text: 'signin_with'
            }
        );
    }

    // $('#g_id_signin2').on('click', function() {
    //     console.log(444);
        
    // })
  
    function handleCredentialResponse(response){
        if (response) {
            SubmitLogin(response.credential)
            
        }
    
    }

    function SubmitLogin(tokens){

        $.ajax({
            url: requestApi+"/v1/gLogin",
            dataType:"json",
            type:"POST",
            aysc:false,
            data:{
                oauth_token: tokens
                
            },
            success:function(res){
                
                if(res.code == 0){
                    localStorage.setItem('session',res.data.token);
                    localStorage.setItem("user", JSON.stringify(res.data));
                    const expiresDate = new Date();
                    const expires = res.data.token_cache == 0 ? 9999999999999 : res.data.token_cache;
                    expiresDate.setDate(expiresDate.getDate() + expires);
                    const domain = window.location.host.split(".").slice(-2).join(".");
                    Cookies.set("common_session", res.data.token, { expires: expiresDate, domain});
                    if(!rParam){
                        gtag_report_conversion('https://dash.cliproxy.com/cli/home');
                    }else{
                        gtag_report_conversion('https://cliproxy.com'+rParam); 
                    }
                }

                
            }
        })
    }

    function gtag_report_conversion(url) {
        var callback = function () {
            if (typeof(url) != 'undefined') {
            window.location = url;
            }else{
                window.location.href = '/';
            }
        };
        gtag('event', 'conversion', {
            'send_to': 'AW-11505915218/tAJJCNmDx_kZENKque4q',
            'value': 1.0,
            'currency': 'USD',
            'event_callback': callback
        });
        return false;
    }

    
})