//// Update Wallet
function add_wallet_acct(){
    if($("#security_pin").val().length == 0){
        Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> enter security pin/authentication code","alert-danger",0,"#wallet_status",12000);
    } else {
        var btn = $('#wallet_btn');
        var btn_content = btn.html();
        btn.prop('disabled',true);
        var data = $("#wallet_form").serialize();
        $.ajax({
            type:'POST',
            url:'index.php?update_wallet',
            data:data,
            beforeSend: function(){	
                btn.html('<i class="fa fa-spinner fa-spin"></i>');
            },
            success: function(data){
                if(data == 1){
                    Msg("<strong><i class='fa fa-check-square'></i>&nbsp;</strong>  Wallet updated successfully","alert-success",0,"#wallet_status",10000);
                } else if (data == 2){
                    Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> enter security pin","alert-danger",0,"#wallet_status",12000);
                } else if (data == 211){
                    Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> enter google 2FA code","alert-danger",0,"#wallet_status",12000);
                }else if (data == 21){
                    Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> Invalid USDT Bep20 address","alert-danger",0,"#wallet_status",12000);
                }else if (data == 22){
                    Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> Invalid USDT Trc20 address","alert-danger",0,"#wallet_status",12000);
                }else if (data == 23){
                    Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> Invalid USDT Erc20 address","alert-danger",0,"#wallet_status",12000);
                }else if (data == 24){
                    Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> Invalid BUSD Bbep20 address","alert-danger",0,"#wallet_status",12000);
                } else if(data == 3){
                    Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> Invalid security pin","alert-danger",0,"#wallet_status",12000);				
                }else if(data == 31){
                    Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> Invalid google 2FA code","alert-danger",0,"#wallet_status",12000);				
                }  else if(data == 4){
                    Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> User not found. Reload page","alert-danger",0,"#wallet_status",12000);				
                } else if(data == 5){
                    Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> Invalid verification code","alert-danger",0,"#wallet_status",12000);				
                } else{
                    Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> "+data,"alert-danger",0,"#wallet_status",12000);				
                } 
                btn.html(btn_content);
                btn.prop('disabled',false);
            }
        });
    }
}
//////////////// to change password from profile page
function change_pin(){ 
    if( $("#pin").val().length == 0|| $("#pin2").val().length == 0){
            Msg("<em class='icon ni ni-alert'></em>&nbsp; all values are required.","alert-danger",0,"#status_pin",12000);
	} else {
        var btn = $('#pin_btn');
        var btn_content = btn.html();
        btn.prop('disabled',true);
	var data = $("#changePin").serialize();
	$.ajax({
            type:'POST',
            url:'../controller/post.php?user-reset-pin',
            data:data,
            beforeSend: function(){	
                btn.html('<i class="fa fa-spinner fa-spin"></i>');
            },
            success: function(data){
                if(data == 1){
                    Msg("<i class='fa fa-check-square'></i>&nbsp; Security pin successfully updated","alert-success",0,"#status_pin",10000);
                    $('.page-loader').css({'display':'block','opacity':'0.5'});                   
                    setTimeout(function(){  
                        window.location = "../dashboard";
                    },2000); 
                } else if (data == 2){
                    Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> all values are required","alert-danger",0,"#status_pin",12000);
                } else if(data == 3){
                    Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> pins do not match ","alert-danger",0,"#status_pin",12000);
                } else if(data == 31){
                    Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> pins must be 4 digits ","alert-danger",0,"#status_pin",12000);
                } else if(data == 4){
                    Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> account not found","alert-danger",0,"#status_pin",12000);
                }else{
                    Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> "+data,"alert-danger",0,"#status_pin",12000);
                }
                btn.html(btn_content);
                btn.prop('disabled',false);
            }
	});
    }
}
//////////////// to change password from profile page
function change_pass(){ 
        if($("#current_password").val().length == 0 ||  $("#password").val().length == 0|| $("#password2").val().length == 0){
		Msg("<em class='icon ni ni-alert'></em>&nbsp; all values are required.","alert-danger",0,"#status1",12000);
	} else {
        var btn = $('#pass_btn');
        var btn_content = btn.html();
        btn.prop('disabled',true);
	var data = $("#changePass").serialize();
	$.ajax({
        type:'POST',
        url:'../controller/post.php?user-reset-password',
        data:data,
        beforeSend: function(){	
            btn.html('<i class="fa fa-spinner fa-spin"></i>');
        },
        success: function(data){
            if(data == 1){
                Msg("<i class='fa fa-check-square'></i>&nbsp; Password changed successfully.","alert-success",0,"#status1",10000);
                $('.page-loader').css({'display':'block','opacity':'0.5'});                   
                setTimeout(function(){  
                    window.location = "../logout";
                },2000); 
            } else if (data == 2){
                Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> all values are required","alert-danger",0,"#status1",12000);
            } else if(data == 3){
                Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> passwords do not match ","alert-danger",0,"#status1",12000);
            } else if(data == 31){
                Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> incorrect current password ","alert-danger",0,"#status1",12000);
            } else if(data == 4){
                Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> account not found","alert-danger",0,"#status1",12000);
            }else if(data == 5){
                Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> password must be at least 8 character","alert-danger",0,"#status1",12000);
            }else if(data == 6){
                Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> password must be at least an uppercase letter","alert-danger",0,"#status1",12000);
            }else if(data == 7){
                Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> password must be at least a lowercase letter","alert-danger",0,"#status1",12000);
            }else if(data == 8){
                Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> password must be at least a number","alert-danger",0,"#status1",12000);
            }else if(data == 9){
                Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> password must be at least a special character ( @ ! # $ . )","alert-danger",0,"#status1",12000);
            }else{
                Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> "+data,"alert-danger",0,"#status1",12000);
            }
            btn.html(btn_content);
            btn.prop('disabled',false);
        }
	});
    }
}
// google authenticator script here
//////////////// to activate google authenticator
/////////////// reset passwordends here //////////////////////////
//
////////////////////// update portfolio page ///////////////
function update_account(){
    if($("#name").val().length == 0 || $("#UserMobile").val().length == 0|| $("#country").val().length == 0){
		Msg("<i class='fa fa-warning'></i>&nbsp; all values are required","alert-danger",0,"#update_status",12000);
	} else {
        var btn = $('#acct_btn');
        var btn_content = btn.html();
        btn.prop('disabled',true);
	var data = $("#accountForm").serialize();
	$.ajax({
		type:'POST',
		url:'index.php?update_account',
		data:data,
		beforeSend: function(){	
			btn.html('<i class="fa fa-spinner fa-spin"></i>');
		},
		success: function(data){
			if(data == 1){
                            Msg("<strong><i class='fa fa-check-square'></i>&nbsp;</strong> Profile updated...","alert-success",0,"#update_status",10000);
			} else if (data == 2){
                            Msg("<strong><i class='fa fa-warning'></i>&nbsp; all values are required</strong>","alert-danger",0,"#update_status",12000);
			} else if(data == 3){
                            Msg("<strong><i class='fa fa-times-circle'></i>&nbsp; account does not exist</strong>","alert-danger",0,"#update_status",12000);				
			}  else if(data == 4){
                            Msg("<strong><i class='fa fa-times-circle'></i>&nbsp; Enter a valid bitcoin wallet address.</strong>","alert-danger",0,"#update_status",12000);				
			} else{
                            Msg("<strong><i class='fa fa-times-circle'></i>&nbsp; "+data+"</strong>","alert-danger",0,"#update_status",12000);				
			} 
            btn.prop('disabled',false);
            btn.html(btn_content);
		}
	});
    }
}
////////////// update settings
function update_settings(done){  
    if($("#two_fa_method").val().length == 0 ||$("#auth_code").val().length == 0 ){
		Msg("<i class='fa fa-warning'></i>&nbsp; Enter 6-digit code","alert-danger",0,"#status3",12000);
	} else if(done == 0) {
        var btn = $('#two_fa_btn');
        var btn_content = btn.html();
        btn.prop('disabled',true);
	var data = $("#Two_Fa").serialize();
	$.ajax({
		type:'POST',
		url:'index.php?update_settings',
		data:data,
		beforeSend: function(){	
			btn.html('<i class="fa fa-spinner fa-spin"></i>');
		},
		success: function(data){
			if(data == 1){
				Msg("<strong><i class='fa fa-check-square'></i>&nbsp;</strong>  Two factor athentication successfully updated.","alert-success",0,"#status3",10000);
				setTimeout(function() {
                    window.location = "../account-settings";
				},2000);
			} else if (data == 2){
				Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> Enter 6-digit code","alert-danger",0,"#status3",12000);
			} else if(data == 3){
				Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> Code incorrect.","alert-danger",0,"#status3",12000);				
			}else{
				Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> Unknown error. please try again","alert-danger",0,"#status3",12000);				
			} 
            done = 1;
            if(data != 1){
                btn.prop('disabled',false);
            }
            btn.html(btn_content);
		}
	});
    }
}
// google authenticator script here
//////////////// to activate google authenticator
function google_pass(){ 
    if( $("#code").val().length == 0){
    Msg("<em class='icon ni ni-alert'></em>&nbsp; Enter  6-digit code","alert-danger",0,"#status2",12000);
    } else {
        var btn = $('#auth_set_up');
        var btn_content = btn.html();
        btn.prop('disabled',true);
        var data = $("#GoogleAuth").serialize();
        $.ajax({
            type:'POST',
            url:'../controller/post.php?google_auth',
            data:data,
            beforeSend: function(){	
                btn.html('<i class="fa fa-spinner fa-spin"></i>');
            },
            success: function(data){
                if(data == 1){
                    Msg("<i class='fa fa-check-square'></i>&nbsp; Two-factor authentication successfully activated.","alert-success",0,"#status2",10000);                    
                    setTimeout(function(){   
                        window.location = "../account-settings";
                    },2000); 
                } else if (data == 2){
                    Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> Error! Refresh the page and try again.","alert-danger",0,"#status2",12000);
                } else if(data == 3){
                    Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> Error! Code not correct. ","alert-danger",0,"#status2",12000);
                } else if(data == 4){
                    Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> Error, account not found","alert-danger",0,"#status2",12000);
                }
                if(data != 1){
                    btn.prop('disabled',false);
                }
                btn.html('Submit');
            }
        });
    }
}

$(document).ready(function(){
    $("#acct_btn").click(function(e) {
        update_account();
        e.preventDefault();
    });
    $("#wallet_btn").click(function(e) {
        add_wallet_acct();
        e.preventDefault();
    });
    $("#pass_btn").click(function(e) {
        change_pass();
        e.preventDefault();
    });
    $("#auth_set_up").click(function(e) {
        google_pass();
        e.preventDefault();
    });
    $("#two_fa_btn").click(function(e) {
        update_settings(0);
        e.preventDefault();
    });
    $("#pin_btn").click(function(e) {
        change_pin();
        e.preventDefault();
    });
    $("#ben_btn").click(function(e) {
        update_beneficiary();
        e.preventDefault();
    });
});
$(".password_toggle").click(function(e) {
    if($('.toggle_1').hasClass('d-none')){
        $('.toggle_1').removeClass('d-none');
        $('.toggle_2').addClass('d-none');        
    }else{
        $('.toggle_1').addClass('d-none');
        $('.toggle_2').removeClass('d-none'); 
    }
});
$(".auth_btn_ctl").click(function(e) {
    if($('.auth_step_one').hasClass('d-none')){
        $('.auth_step_one').removeClass('d-none');
        $('.auth_step_two').addClass('d-none');        
    }else{
        $('.auth_step_one').addClass('d-none');
        $('.auth_step_two').removeClass('d-none'); 
        $('#code').focus();
    }
});
