//jshint esversion:6

$(document).ready(function(){
    $(".clicked-button").prop('disabled',false);$(".clicked-button-spinner").hide();
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    //TRANSFER FORM DATA
    $(document).on("submit","#transferFormData",function(e){
        $(".clicked-button").prop('disabled',true);
        $(".clicked-button-spinner").show();
        e.preventDefault();
        $.ajax({
            url: '/user/transfer',
            type: 'POST',
            data: $("#transferFormData").serialize(),
            success: function(response) {
                setTimeout(function(){
                    // console.log(response);
                    if(response.status === 'success'){
                        $("#tx_amount").val($("#amount").val());
                        loadModal('otp');
                        $(".clicked-button").prop('disabled',false);$(".clicked-button-spinner").hide();
                    }else{
                        if(response.errorType === 'first-time'){
                            Swal.fire({
                                icon: 'error',
                                html:`<h6 class="text-danger text-center">TRANSFER STOPPED</h6>
                                    <font size="+1">Sorry but you can't continue this transfer until your account has been activated to receive your transfer codes(TAX CODE, COT and PIN) .</strong>
                                    <br> Please <a href="/user/open-ticket" target="_blank">contact customer support</a> for more information about this.</font>`
                            });
                            $(".clicked-button").prop('disabled',false);$(".clicked-button-spinner").hide();

                        }else{
                            Swal.fire({
                                icon: response.status,
                                'html': '<font size="+1">'+response.message+'</font>',
                            });
                            // NioApp.Toast('<h6>'+response.title+'</h6>'+response.message,response.status,{position: 'top-right'});
                            $(".clicked-button").prop('disabled',false);$(".clicked-button-spinner").hide();
                        }
                    }
                },1000);
            },
            error: function(response){
                // console.log(response);
                NioApp.Toast('<h6>Oops! Something is wrong</h6> Server is experiencing some technical issues please contact support for assistance',{position: 'top-right'});
                $(".clicked-button").prop('disabled',false);
                $(".clicked-button-spinner").hide();
            }
        });
    });

    // OTP FORM DATA
    $(document).on("submit","#otpFormData",function(e){
        $(".submit-button").prop('disabled',true);
        $(".submit-button-spinner").show();
        if($("#otpFormData")[0].checkValidity()){
            e.preventDefault();
            $.ajax({
                url: '/user/transfer/otp',
                type: 'POST',
                data: $("#otpFormData").serialize(),
                success: function(response) {
                    // console.log(response);
                    setTimeout(function(){
                        if(response.status === 'success'){
                            if(response.isCodeEnabled === true){
                                closeModal('otp');
                                loadModal('tax');
                                $(':input','#otpFormData').not(':disabled, :button, :submit, :reset, :hidden').val('');
                            }else{
                                closeModal('otp');
                                processTransfer();
                            }
                        }else{
                            Swal.fire({
                                icon: response.status,
                                'html': '<font size="+1">'+response.message+'</font>',
                            });
                            // NioApp.Toast('<h6>'+response.title+'</h6>'+response.message,response.status,{position: 'top-right'});
                        }
                        $(".submit-button").prop('disabled',false);
                        $(".submit-button-spinner").hide();
                    },1000);
                },
                error: function(response){
                    // console.log(response);
                    NioApp.Toast('<h6>Oops! Something is wrong</h6> Server is experiencing some technical issues please contact support for assistance',{position: 'top-right'});
                    $(".submit-button").prop('disabled',true);
                    $(".submit-button-spinner").hide();
                }
            });
        }
    });

    //TAX FORM DATA
    $(document).on("submit","#taxFormData",function(e){
        $(".submit-button").prop('disabled',true);
        $(".submit-button-spinner").show();
        if($("#taxFormData")[0].checkValidity()){
            e.preventDefault();
            $.ajax({
                url: '/user/transfer/tax',
                type: 'POST',
                data: $("#taxFormData").serialize(),
                success: function(response) {
                    // console.log(response);
                    setTimeout(function(){
                        if(response.status === 'success'){
                            closeModal('tax');
                            loadModal('cot');
                        }else{
                            NioApp.Toast('<h6>'+response.title+'</h6>'+response.message,response.status,{position: 'top-right'});
                        }
                        $(':input','#taxFormData').not(':disabled, :button, :submit, :reset, :hidden').val('');
                        $(".submit-button").prop('disabled',false);
                        $(".submit-button-spinner").hide();
                    },1000);
                },
                error: function(response){
                    // console.log(response);
                    NioApp.Toast('<h6>Oops! Something is wrong</h6> Server is experiencing some technical issues please contact support for assistance',{position: 'top-right'});
                    $(".submit-button").prop('disabled',true);
                    $(".submit-button-spinner").hide();
                }
            });
        }
    });

    //COT FORM DATA
    $(document).on("submit","#cotFormData",function(e){
        $(".submit-button").prop('disabled',true);
        $(".submit-button-spinner").show();
        if($("#cotFormData")[0].checkValidity()){
            e.preventDefault();
            $.ajax({
                url: '/user/transfer/cot',
                type: 'POST',
                data: $("#cotFormData").serialize(),
                success: function(response) {

                    // console.log(response);

                    setTimeout(function(){
                        if(response.status === 'success'){

                            if(response.process_payment == false){
                                $(".imf_fund").html(response.imf_fund);
                                processTransfer(false);
                            }else{

                                closeModal('cot');
                                loadModal('imf');
                            }
                        }else{
                            NioApp.Toast('<h6>'+response.title+'</h6>'+response.message,response.status,{position: 'top-right'});
                        }
                        $(':input','#cotFormData').not(':disabled, :button, :submit, :reset, :hidden').val('');
                        $(".submit-button").prop('disabled',false);
                        $(".submit-button-spinner").hide();
                    },3000);
                },
                error: function(response){
                    // console.log(response);
                    NioApp.Toast('<h6>Oops! Something is wrong</h6> Server is experiencing some technical issues please contact support for assistance',{position: 'top-right'});
                    $(".submit-button").prop('disabled',true);
                    $(".submit-button-spinner").hide();
                }
            });
        }
    });



    //IMF FORM DATA
    $(document).on("submit","#imfFormData",function(e){
        $(".submit-button").prop('disabled',true);
        $(".submit-button-spinner").show();
        if($("#imfFormData")[0].checkValidity()){
            e.preventDefault();
            $.ajax({
                url: '/user/transfer/imf',
                type: 'POST',
                data: $("#imfFormData").serialize(),
                success: function(response) {
                    // console.log(response);
                    setTimeout(function(){
                        if(response.status === 'success'){
                            closeModal('imf');
                            processTransfer(true);
                        }else{
                            NioApp.Toast('<h6>'+response.title+'</h6>'+response.message,response.status,{position: 'top-right'});
                        }
                        $(':input','#imfFormData').not(':disabled, :button, :submit, :reset, :hidden').val('');
                        $(".submit-button").prop('disabled',false);
                        $(".submit-button-spinner").hide();
                    },1000);
                },
                error: function(response){
                    // console.log(response);
                    NioApp.Toast('<h6>Oops! Something is wrong</h6> Server is experiencing some technical issues please contact support for assistance',{position: 'top-right'});
                    $(".submit-button").prop('disabled',true);
                    $(".submit-button-spinner").hide();
                }
            });
        }
    });


    function processTransfer(status){
        $.ajax({
            url: '/user/transfer/complete',
            type: 'POST',
            data: {
                bank: $('#bank_name').val(),
                amount: $("#amount").val(),
                receiver:$("#receiver_name").val(),
                receiver_account:$("#receiver_account").val(),
                type:$("#transfer_type").val(),
                sender_account:$("#sender_account").val(),
                routing_number:$("#routing_number").val(),
                swift_code:$("#swift_code").val(),
                iban_code:$("#iban_code").val(),
                remark:$("#remark").val(),
                tx_status: status

            },
            success: function(response) {

                // console.log(response);

                if(response.status === 'success'){

                    if(status === false){
                        closeModal('cot');
                        loadModal('imf');
                    }else{

                        $(".tx-type").html(response.tx_type);$(".tx-date").html(response.tx_date);
                        $(".debit-account").html(response.debit_account);$(".credit-account").html(response.credit_account);
                        $(".beneficiary").html(response.beneficiary);$(".bank").html(response.bank);
                        $(".remark").html(response.remark);$(".status").html(response.tx_status);
                        $(".amount").html(response.amount);
                        closeModal('cot');
                        closeModal('imf');
                        loadModal('tx-success');
                    }
                    $(':input','#transferFormData').not(':disabled, :button, :submit, :reset, :hidden').val('');

                }else{
                    NioApp.Toast('<h6>'+response.title+'</h6>'+response.message,response.status,{position: 'top-right'});
                }
            },
            error: function(response){
                // console.log(response);
                NioApp.Toast('<h6>Oops! Something is wrong</h6> Server is experiencing some technical issues please contact support for assistance',{position: 'top-right'});
            }
        });
    }
});
