///// withdraw capital
$(".cap_with_btn").click(function(){    
    var data = $(this).attr('data-info');	
    if(data.length > 0 ){
        var dataSetValues = data.split("|");
        var plan_name = dataSetValues[0];
        var plan_id = dataSetValues[1];
        var capital = dataSetValues[2];
        var capital_status = dataSetValues[3];
        var cap_status = '<span class="badge bg-danger"> Locked</span>';
        var with_fee = '35%';
        var fee_rate = 0.35;
        if(capital_status == 1){
            cap_status = '<span class="badge bg-success"> Unlocked</span>';
            with_fee = '5%';
            fee_rate = 0.05;
        }
        var fee = parseFloat(capital) * fee_rate;
        var amt_receive = parseFloat(capital) - fee;
        $(".with_pln_name").html(plan_name);
        $(".with_pln_id").html("#"+plan_id);
        $(".cap_status").html(cap_status);
        $(".with_fee").html(with_fee);
        $("#amount").val(capital);
        $(".w_fee").html("$"+fee);
        $(".a_receive").html("$"+amt_receive);
        $("#port_id").val(plan_id);
        $("#cap_withdraw_form ").removeClass('d-none');
        $("#preview1").addClass('d-none');
        $("#addNewCCModal").modal("show");
    }
});
$("#cap_withdraw_btn").click(function(){  
    var amount = $("#amount").val();
    var with_method = $("#withdraw_method").val();
    var with_from = $("#with_from").val();
    var wallet = $("#withdraw_details").val();
    var port_id = $("#port_id").val();
    var status = '#status_pay';
    if(with_method.length == 0 || wallet.length < 20){
        Msg("<i class='fa fa-warning'></i>&nbsp; All fields required...","alert-danger",0,status,12000);
    }else if(port_id.length == 0 || amount.length == 0){
        Msg("<i class='fa fa-warning'></i>&nbsp; Reload page and try again...","alert-danger",0,status,12000);
    }else{
        $(status).html('');
        withdraw_capital_funds();
    }   
});
////////////////////// account withdrawal request ///////////////
function withdraw_capital_funds(){
    var status = '#status_pay';
    var btn = $("#cap_withdraw_btn");
    var btcontent = btn.html();
    btn.prop('disabled',true);
	var data = $("#cap_withdraw_form").serialize();
	$.ajax({
        type:'POST',
        url:'../controller/post.php?capital_withdraw_funds',
        data:data,
        beforeSend: function(){	
            btn.html('<i class="fa fa-spinner fa-spin"></i>');
        },
            success: function(data){
                if(data == 1){
                    $("#cap_withdraw_form ").addClass('d-none');
                    $("#preview1").removeClass('d-none');
                    setTimeout(function() {
                        $("#cap_withdraw_form").trigger('reset');
                    },6000);
                } else if (data == 2){
                        Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> All fields are required!!!","alert-danger",0,status,12000);
                }else if (data == 21){
                    Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> Wallet type not available","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>&nbsp;</strong> Action Rejected!!!. You must fund your account first","alert-danger",0,status,12000);				
                }else if(data == 5){
                        Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> Withdrawal is already being processed...","alert-danger",0,status,12000);				
                }else if(data == 6){
                        Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> Minimum withdrawal is $"+min_with,"alert-danger",0,status,12000);				
                }else if(data == 7){
                        Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> Incorrect security pin","alert-danger",0,status,12000);				
                }else if(data == 31){
                        Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> Invalid google 2FA code","alert-danger",0,status,12000);				
                } else if(data == 8){
                    Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> Invalid withdrawal address","alert-danger",0,status,12000);				
                }else if(data == 9){
                        Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> You have multiple pending capital withdrawals on this account...","alert-danger",0,status,12000);				
                }else{
                    Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> "+data,"alert-danger",0,status,12000);
                } 
                if(data != 1){
                    btn.prop('disabled',false);
                    $(".step_back").prop('disabled',false);
                }
                
                btn.html(btcontent);
            }
	});
}
/////////////////////////
/////////////
//////////////////////
$(".walletRadio").click(function(){    
    var data = $(this).attr('data-info');	
    if(data.length > 0 ){
        var dataSetValues = data.split("|");
        var currency = dataSetValues[0];
        var address = dataSetValues[1];
        var bar_code = dataSetValues[2];
        $("#withdraw_method").val(currency);
        $(".fund_method").html(currency);
        $(".coin_name").html(currency);
        $(".coin_address").html(address);
    }
});
function check_amount(){
    var bal = parseFloat($("#with_bal").val());
    var min_with = parseFloat($("#min_withdrawal").val());
    if(bal > min_with ){
        $(".step_1_btn").prop('disabled',false);
    }
}
check_amount();
$(".step_1_btn").click(function(){  
    var val = $("#amount").val();
    var with_method = $("#withdraw_method").val();
    var with_from = $("#with_from").val();
    var wallet = $("#withdraw_details").val();
    var status = '#status_pay';
    var min_with = parseFloat($("#min_withdrawal").val());
    var with_balance = parseFloat($("#with_bal").val());
    if(with_method.length == 0 || val.length == 0||with_from.length == 0|| wallet.length < 20){
        Msg("<i class='fa fa-warning'></i>&nbsp; all values are required...","alert-danger",0,status,12000);
    }else if(parseFloat(val) < min_with){
        Msg("<i class='fa fa-warning'></i>&nbsp; minimum amount is $"+min_with+"...","alert-danger",0,status,12000);
        $("#amount").focus();
    }else if(with_balance < parseFloat(val)){
        Msg("<i class='fa fa-warning'></i>&nbsp; Insufficient balance","alert-danger",0,status,12000);
        $("#amount").focus();
    } else{
        $(status).html('');
        withdraw_funds();
    }   
    
});
////////////////////// account withdrawal request ///////////////
function withdraw_funds(){
    var status = '#status_pay';
    var btcontent = $("#withdraw_btn").html();
    var min_with = parseFloat($("#min_withdrawal").val());
    $(".step_back").prop('disabled',true);
    $("#withdraw_btn").prop('disabled',true);
	var data = $("#withdraw_form").serialize();
	$.ajax({
        type:'POST',
        url:'../controller/post.php?withdraw_funds',
        data:data,
        beforeSend: function(){	
            $("#withdraw_btn").html('<i class="fa fa-spinner fa-spin"></i>');
        },
            success: function(data){
                if(data == 1){
                    $("#withdraw_form").addClass('d-none');
                    $("#preview1").removeClass('d-none');
                    setTimeout(function() {
                        $("#withdraw_form").trigger('reset');
                    },6000);
                } else if (data == 2){
                        Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> all fields are required!!!","alert-danger",0,status,12000);
                }else if (data == 21){
                    Msg("<strong><i class='fa fa-warning'></i>&nbsp;</strong> Wallet type not available","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>&nbsp;</strong> Action Rejected!!!. You must fund your account first","alert-danger",0,status,12000);				
                }else if(data == 5){
                        Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> Insufficient balance","alert-danger",0,status,12000);				
                }else if(data == 6){
                        Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> Minimum withdrawal is $"+min_with,"alert-danger",0,status,12000);				
                }else if(data == 7){
                        Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> Incorrect security pin","alert-danger",0,status,12000);				
                }else if(data == 31){
                        Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> Invalid google 2FA code","alert-danger",0,status,12000);				
                } else if(data == 8){
                    Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> Invalid withdrawal address","alert-danger",0,status,12000);				
                }else if(data == 9){
                        Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> You have multiple pending withdrawals on this account...","alert-danger",0,status,12000);				
                }else{
                    Msg("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong> "+data,"alert-danger",0,status,12000);
                } 
                if(data != 1){
                    $("#withdraw_btn").prop('disabled',false);
                    $(".step_back").prop('disabled',false);
                }
                
                $("#withdraw_btn").html(btcontent);
            }
	});
}
////////////////////////