$(".walletRadio").click(function(){    
    var data = $(this).attr('data-info');	
    if(data.length > 0 ){
        var dataSetValues = data.split("|");
        var plan_name = dataSetValues[0];
        var min_invest = dataSetValues[1];
        var roi = dataSetValues[2];
        var capital_locked = dataSetValues[3];
        var inv_days = dataSetValues[4];
        $("#fund_plan").val(plan_name);
        $(".fund_plan").html(plan_name);
        $("#min_invest").val(min_invest);
        $(".min_holder").html('$'+min_invest.toLocaleString('en-US'));
        $(".cap_lock").html(capital_locked +" days after activation");
        $("#plan_roi").val(roi);
        $("#inv_days").val(inv_days);
        $(".inv_days").html(inv_days);
        if($("#amount2").val().length > 0){
            calculator($("#amount2").val());
        }
    }
});
$("#amount2").on("keyup change", function(){
    if($("#amount2").val().length > 0){
        calculator($("#amount2").val());
    }
});
function calculator(x){
    var val = parseFloat(x);
    var fund_plan = $("#fund_plan").val();
    var inv_days = parseFloat($("#inv_days").val());
    var d_rio = parseFloat($("#plan_roi").val());
    var day_reward = ( val * d_rio * 8)/100;    
    if(fund_plan == 'LTSP'){
        var week_reward = compound_reward(val,6);
        var month_reward = compound_reward(val,24);
        var six_months_reward = compound_reward(val,144);
        var one_year_reward = compound_reward(val,288);
        var total_roi = (d_rio * 864) * 8;
        var reward = compound_reward(val,864);
        var total_roi = (reward / val) * 100;
    }else{
        var week_reward = day_reward * 6;
        var month_reward = week_reward * 4;
        var six_months_reward = month_reward * 6;
        var one_year_reward = month_reward * 12; 
        var total_roi = (d_rio * 288) * 8;
        var reward =( val * total_roi)/100;
    }
    var new_amount = val.toLocaleString('en-US');
    var total = val + reward;
    $(".fund_amt2").html("$"+new_amount);
    $(".d_earn2").html('$'+day_reward.toLocaleString('en-US'));
    $(".w_earn2").html('$'+week_reward.toLocaleString('en-US'));
    $(".m_earn2").html('$'+month_reward.toLocaleString('en-US'));
    $(".sm_earn2").html('$'+six_months_reward.toLocaleString('en-US'));
    $(".y_earn2").html('$'+one_year_reward.toLocaleString('en-US'));
    $(".d_total2").html('$'+total.toLocaleString('en-US') + " ("+ (total_roi+100).toLocaleString('en-US') +"%)");
    ////////////////////////////////////
    $(".d_earn").html('$'+reward.toLocaleString('en-US') + " ("+total_roi.toLocaleString('en-US') +"%)");
    $(".d_total").html('$'+total.toLocaleString('en-US') + " ("+ (total_roi+100).toLocaleString('en-US') +"%)");
}
function compound_reward(x,y){
    var val = x;
    var plan_roi = parseFloat($("#plan_roi").val());
    var d_rio = (plan_roi * 8)/100;
    var day_reward = 0;
    var total_reward = 0;
    for(var i = 0 ; i < y ; i++){
        day_reward = val * d_rio;
        
        console.log("capital: " + val +" -- Reward: "+ day_reward+" -- Roi: "+ d_rio);
        val += day_reward;
        total_reward += day_reward;
    }
    return total_reward;
}
// $("#fund_info").change(function(){
//     check_plan();
// });
// function check_plan(){		
//     var plan = $('#fund_info').val();	
//     if(plan.length > 0 ){
//         var dataSetValues = plan.split("|");
//         var plan_id = dataSetValues[0];
//         var plan_min = dataSetValues[1];
//         var plan_name = dataSetValues[2];
//         var plan_roi = dataSetValues[3];
//         $("#fund_plan").val(plan_id);
//         $(".fund_plan").html(plan_id);
//         $("#min_dep").val(plan_min);
//         $("#plan_roi").val(plan_roi);
//         $("#amount").prop('min',plan_min);
//         $("#min_holder").html('$'+plan_min.toLocaleString('en-US'));
//         $(".port_name").html(plan_name);
//         $(".plan_roi").html(plan_roi * 100 +'%');
//         /////////////
//     }
// }
// check_plan();
/////////////////////////
$(".step_1_btn").click(function(){    
    var status = '#dep_status';
    var min = parseFloat($("#min_invest").val());
    var d_rio = parseFloat($("#plan_roi").val());
    var val = parseFloat($("#amount").val());
    var bal = parseFloat($("#acct_bal").val());
    var inv_days = parseFloat($("#inv_days").val());
    if($("#fund_plan").val().length == 0){
        Msg("<i class='fa fa-warning'></i>&nbsp; Select portfolio...","alert-danger",0,status,12000);
    } else if($("#amount").val().length == 0){
        Msg("<i class='fa fa-warning'></i>&nbsp; Enter amount...","alert-danger",0,status,12000);
    } else if(val < min){
        Msg("<i class='fa fa-warning'></i>&nbsp; Minimum investment $"+min,"alert-danger",0,status,12000);
    } else if(val > bal){
        Msg("<i class='fa fa-warning'></i>&nbsp; Insufficient balance","alert-danger",0,status,12000);
    } else{
        //// populate some fields 
//        var new_amount = val.toLocaleString('en-US');
//        var total_roi = (d_rio * inv_days) * 8;
//        var reward =( val * total_roi)/100;
//        var total = val + reward;
//        $(".fund_amt").html("$"+new_amount);
//        $(".d_earn").html('$'+reward.toLocaleString('en-US') + " ("+total_roi.toLocaleString('en-US') +"%)");
//        $(".d_total").html('$'+total.toLocaleString('en-US') + " ("+ (total_roi+100).toLocaleString('en-US') +"%)");
        calculator($("#amount").val());
        ///////////////
        var new_amount = val.toLocaleString('en-US');
        $(".fund_amt").html("$"+new_amount);
        $(status).html('');
        $("#preview2").addClass("d-none");
        $("#preview3").removeClass("d-none");
    }
});
/////////////////////////
$(".step_1_back_btn").click(function(){   
    $("#preview2").removeClass("d-none");
    $("#preview3").addClass("d-none");
});
/////////////////////////
$(".step_2_btn").click(function(){    
    var status = '#dep_status2';
    var min = parseFloat($("#min_invest").val());
    var val = parseFloat($("#amount").val());
    if($("#amount").val().length == 0|| $("#fund_plan").val().length == 0){
        Msg("<i class='fa fa-warning'></i>&nbsp; all values are required...","alert-danger",0,status,12000);
    } else if(val < min){
        Msg("<i class='fa fa-warning'></i>&nbsp; minimum stake is $"+min,"alert-danger",0,status,12000);
    } else{
        $(status).html('');
        invest_funds();
    }
});
////////////////////////
function invest_funds(){
    var status = '#dep_status2';
    var min = parseFloat($("#min_invest").val());
	var data = $("#invest_form").serialize();
    var btn = $('#funds_btn');
    var btn_content = btn.html();
    $('.step_2_back_btn').prop('disabled',true); 
    btn.prop('disabled',true);        
	$.ajax({
		type:'POST',
		url:'../controller/post.php?make_investment',
		data:data,
		beforeSend: function(){	
            $("#funds_btn").html('Please wait... <i class="fa fa-spinner fa-spin"></i>');
		},
		success: function(data){
            if(data == 1){
                $("#preview3").addClass("d-none");
                $("#preview4").removeClass("d-none");
                setTimeout(function() {
                        $("#invest_form").trigger('reset');
                },12000);
            } else if (data == 2){
                    Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> fill the form!!!","alert-danger",0,status,12000);

            }else if(data == 3){
                Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> account does not exist","alert-danger",0,status,12000);				

            }else if(data == 4){
                Msg("<strong><i class='fa fa-times-circle'></i></strong>&nbsp; Minimum investment is $"+min,"alert-danger",0,status,12000);				
            }else if(data == 5){
                    Msg("<strong><i class='fa fa-times-circle'></i></strong>&nbsp; Insufficient balance...","alert-danger",0,status,12000);				
            }else if(data == 22){
                Msg_custom("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> plan does not exist","alert-danger",1,"#purchase_status",12000);				
                
            } else if(data == 34){
                    Msg("<strong><i class='fa fa-times-circle'></i></strong>&nbsp; Maximum deposit is $100,000","alert-danger",0,status,12000);				
            }else if(data == 35){
                Msg("<strong><i class='fa fa-times-circle'></i></strong>&nbsp; Minimum deposit is $2,000 to qualify for Auto Reinvest","alert-danger",0,status,12000);				
            }else if(data == 4){
                    Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> Error!!! Your first deposit can not be below 50 USD.","alert-danger",0,status,12000);				

            }else if(data == 5){
                    Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> Error! Bank Wire is allowed for only deposits from $50,000 above","alert-danger",0,status,12000);				

            }else{
                Msg("<strong><i class='fa fa-times-circle'></i>&nbsp; </strong> "+data,"alert-danger",0,status,12000);	
            } 
            $("#funds_btn").html(btn_content);
            btn.prop('disabled',false);
		}
	});
}
function copy_text(x) {
    //    $("#"+x+"_btn").html('<i class="fas fa-spinner fa-spin"></i>');
    /* Get the text field */
    var copyText = document.getElementById(x);
    var btn = $("#"+x+"_btn");
    var btn_content = btn.html();
    btn.html('Copied');
    setTimeout(function () {
        btn.html(btn_content);
    }, 2000);
    /* Select the text field */
    copyText.select();
    copyText.setSelectionRange(0, 99999); /* For mobile devices */

    /* Copy the text inside the text field */
    navigator.clipboard.writeText(copyText.value);
    
    
    /* Alert the copied text */
    //    $("#"+x+"_btn").html('<i class="fa fa-copy"></i>');
}
// Update the count down every 10 second
$(".pay_btn").click(function(){
    var value = $(this).data("value");
    if(value.length > 0 ){
        var dataSetValues = value.split("|");
        var wallet = dataSetValues[0];
        var amount = dataSetValues[1];
        var method = dataSetValues[2];
        var qr = '<img src="https://chart.googleapis.com/chart?chs=150x150&amp;cht=qr&amp;chl='+wallet+'&amp;choe=UTF-8" title="'+wallet+'">';
        $(".dp_type").html(method);
        $(".dp_amount").html('$'+amount.toLocaleString('en-US'));
        $("#dp_amount").val(amount);
        $(".dp_address").html(wallet);
        $("#dp_address").val(wallet);
        $("#dep_qr").html(qr);
        /////////////
        // $('#myModal').modal('show');
    }
});

////////////top up code
$(".top_up_btn").click(function(){    
    var status = '.dep_status';
    var min = parseFloat($("#min_invest").val());
    var val = parseFloat($("#amount").val());
    if($("#amount").val().length == 0|| $("#fund_plan").val().length == 0){
        Msg("<i class='fa fa-warning'></i>&nbsp; all values are required...","alert-danger",0,status,12000);
    } else if(val < min){
        Msg("<i class='fa fa-warning'></i>&nbsp; minimum top-up $"+min,"alert-danger",0,status,12000);
    } else{
        $(status).html('');
        top_up_funds();
    }
});
////////////////////////
function top_up_funds(){
    var status = '.dep_status';
    var min = parseFloat($("#min_invest").val());
	var data = $("#top_up_form").serialize();
    var btn = $('.top_up_btn');
    var btn_content = btn.html();
    btn.prop('disabled',true);        
	$.ajax({
		type:'POST',
		url:'../controller/post.php?top_up_funds',
		data:data,
		beforeSend: function(){	
            btn.html('Please wait... <i class="fa fa-spinner fa-spin"></i>');
		},
		success: function(data){
            if(data == 1){
                $("#preview2").addClass("d-none");
                $("#preview4").removeClass("d-none");
                setTimeout(function() {
                        $("#invest_form").trigger('reset');
                },12000);
            } else if (data == 2){
                    Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> fill the form!!!","alert-danger",0,status,12000);

            }else if(data == 3){
                Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> account does not exist","alert-danger",0,status,12000);				

            }else if(data == 4){
                Msg("<strong><i class='fa fa-times-circle'></i></strong>&nbsp; Minimum top-up is $"+min,"alert-danger",0,status,12000);				
            }else if(data == 5){
                    Msg("<strong><i class='fa fa-times-circle'></i></strong>&nbsp; Insufficient balance...","alert-danger",0,status,12000);				
            }else if(data == 22){
                Msg_custom("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> portfolio does not exist","alert-danger",1,"#purchase_status",12000);				
                
            }else if(data == 4){
                    Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> Error!!! Your first deposit can not be below 50 USD.","alert-danger",0,status,12000);				

            }else if(data == 5){
                    Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> Error! Bank Wire is allowed for only deposits from $50,000 above","alert-danger",0,status,12000);				

            }else{
                Msg("<strong><i class='fa fa-times-circle'></i>&nbsp; </strong> "+data,"alert-danger",0,status,12000);	
            } 
            btn.html(btn_content);
            btn.prop('disabled',false);
		}
	});
}