
<link rel="stylesheet" href="<?php echo plugins_url('opay/');?>css/withdrawal.css?ver=<?php echo $rand;?>" />


<div class="container position-relative" id="main-page">

    <div id="airtime-page-header" class="row p-4 bg-white">
        <div class="col d-flex flex-row justify-content-start align-items-start">
           <span class="general-text"><a href="#" class="back cursor-pointer text-decoration-none text-black"><i class="fa-solid fa-angle-left me-3 "></i></a>Withdrawal</span>
        </div>
    </div>


    <?php       
                    if($total_bal_with >= $minwithle){
                echo'
    
    <div class="with-form-container dark-white">
    <div class="input-group mb-2 mt-1">
    
    </div>
    
    <span class="with-form">
        <div class="form">
        <form method="post" target="_self" class="py-5 px-3 withdraw_now bg-white">
    
    
    <label class="form-label dark">From:</label><br>

        <div class="source-to-select d-flex justify-content-start align-items-center py-2">

          <div class="source-sel w-100">
            <select name="source" class="source form-control">
              <option value="">-- Please Select --</option>
              <option value="bonus">Total Bonus Balance</option>
              <option value="wallet">My Wallet Balance</option>
             </select>
          </div>
           <div class="source-amt d-flex align-items-center bg-white">
              <input class="form-control text-end fw-bold frombonus" type="number" value="'.$total_bal_with.'" readonly max="'.$total_bal_with.'">
              <input class="form-control text-end fw-bold fromwallet" type="number" value="'.$bal.'" max="'.$bal.'">
              <span class="naira fw-bold ">₦</span>
            </div>


        </div>

        <div class="d-flex  divider justify-content-center align-items-center"></div>

    

            
    <label class="form-label dark">To:</label><br>
            <select name="withto" class="withto  form-control" class="form-select">
                <option value="" class="none"> -- Please Select --</option>
                <option value="wallet" class="towallet ">Wallet</option>
                ';
                if(strtolower(vp_option_array($option_array,"allow_to_bank")) == "yes"){
                    echo'
                <option value="bank" class="tobank">Bank Account</option>
                ';
                }
                echo'
        </select>
                <div class="bankdetails" style="display:none;width:100%;">
                <br>
          <label class="form-label dark">Bank Details:</label><br>
                <textarea class="form-control" name="bankdetails" style="width:100%;" value="Enter your bank details here"></textarea>
                </div>
                <br>


            <input type="button" value="submit" name="withdrawit" class="mt-2 bg-success w-100 p-1 text-xs fw-bold text-white  withdrawit"><br>
            
            
    </form>
        
        </div>
        
        </div>
            </span>
    
            
            
            <script>
            //Default show frombonus and hide fromwallet
    jQuery(".fromwallet").hide();
    jQuery(".frombonus").show();
            
            //get value and display
    var showthis = jQuery(".source").val();
    
    if(showthis == "bonus"){
        jQuery(".fromwallet").hide();
        jQuery(".fromwallet").removeClass("withamt");
        jQuery(".fromwallet").removeAttr("name","withamt");
        
        jQuery(".frombonus").show();
        jQuery(".frombonus").addClass("withamt");
        jQuery(".frombonus").attr("name","withamt");
        
        jQuery(".tobank").show();
        jQuery(".towallet").show();
        jQuery(".frombonus").addClass("active");
        
        
    
    }
    else if(showthis == "wallet"){
        jQuery(".frombonus").hide();
        jQuery(".frombonus").removeClass("withamt");
        jQuery(".frombonus").removeAttr("name","withamt");
        
        jQuery(".fromwallet").show();
        jQuery(".fromwallet").addClass("withamt");
        jQuery(".fromwallet").attr("name","withamt");
        
        jQuery(".tobank").show();
        jQuery(".towallet").hide();
        jQuery(".withto").val("");
        jQuery(".fromwallet").addClass("active");

    }
    
    
    
            
    jQuery(".source").on("change",function(){
    var showth = jQuery(".source").val();
    
    if(showth == "bonus"){
        jQuery(".fromwallet").hide();
        jQuery(".fromwallet").removeClass("withamt");
        jQuery(".fromwallet").removeAttr("name","withamt");
        
        jQuery(".frombonus").show();
        jQuery(".frombonus").addClass("withamt");
        jQuery(".frombonus").attr("name","withamt");
        
        jQuery(".tobank").show();
        jQuery(".towallet").show();
        jQuery(".frombonus").addClass("active");
        
        
    
    }
    else if(showth == "wallet"){
        jQuery(".frombonus").hide();
        jQuery(".frombonus").removeClass("withamt");
        jQuery(".frombonus").removeAttr("name","withamt");
        
        jQuery(".fromwallet").show();
        jQuery(".fromwallet").addClass("withamt");
        jQuery(".fromwallet").attr("name","withamt");
        
        jQuery(".tobank").show();
        jQuery(".towallet").hide();
        jQuery(".withto").val("");
        jQuery(".fromwallet").addClass("active");


    }
    else{
      showToast("Select A Valid Source","red");
    }
    
    });	
            
            
            
        
        
  
            
    jQuery(".withdrawit").click(function(){

    var showth = jQuery(".source").val();
    var withto = jQuery(".withto").val();
    var amt = jQuery("input[type=number].active").val();

      if(showth == ""){
        showToast("Select A Valid Source","red");
        return;
      }
      
      if(withto == ""){
        showToast("Select A Valid Destination","red");
        return;
      }

      if(amt == undefined){
        showToast("Amount Undefined","red");
        return;
      }
      else if(amt == "" || amt == "0"){
        showToast("Amount Can\'t be 0","red");
        return;
      }
        showToast("Processing","yellow",5000);
        
        var obj = {};
        var toatl_input = jQuery(".withdraw_now input, .withdraw_now select, .withdraw_now textarea").length;
    var run_obj;
    
    for(run_obj = 0; run_obj <= toatl_input; run_obj++){
    var current_input = jQuery(".withdraw_now input, .withdraw_now select, .withdraw_now textarea").eq(run_obj);
    
    var obj_name = current_input.attr("name");
    var obj_value = current_input.val();
    
    if(typeof obj_name !== typeof undefined && obj_name !== false){
    obj[obj_name] = obj_value;

    }	
    }
    
    jQuery.ajax({
      url: "'.esc_url(plugins_url("vtupress/vend.php")).'",
      data: obj,
      dataType: "json",
      "cache": false,
      "async": true,
    
      error: function (jqXHR, exception) {
          
            var msg = "";
            if (jqXHR.status === 0) {
                msg = "No Connection.\n Verify Network.";
      
            } else if (jqXHR.status == 404) {
                msg = "Requested page not found. [404]";
            } else if (jqXHR.status == 500) {
                msg = "Internal Server Error [500].";
            } else if (exception === "parsererror") {
                msg = "Requested JSON parse failed.";
            } else if (exception === "timeout") {
                msg = "Time out error.";
            } else if (exception === "abort") {
                msg = "Ajax request aborted.";
            } else {
                msg = "Uncaught Error." + jqXHR.responseText;
            }

        showToast(msg,"red");

        },
      success: function(data){

        var msg;
            if(data.status == "100" ){
        jQuery("input.currbal").val(0);

        msg = "Withdrawal To Wallet Successful";
        showToast(msg,"green");
        location.reload();
          }
          else if(data.status == "101" ){
              jQuery("input.currbal").val(0);
          msg = "Withdrawal Request Sent";
        showToast(msg,"yellow",3000);

        setTimeout(function(){
          location.reload();
        },2000);

          }
         else if(data.status == "400" ){
              jQuery("input.currbal").val(0);
              msg = "Withdrawal Request Sent";
              showToast(msg,"red",3000);
          }
               else if(data.status == "450" ){
              jQuery("input.currbal").val(0);
              msg = "Can\'t withdraw wallet to wallet for now";
              showToast(msg,"red",3000);
          }
          else if(data.status == "410" ){
              jQuery("input.currbal").val(0);
      msg = "The withdraw amount must be your total bonus amount";
              showToast(msg,"red",3000);
          }
      },
      type: "POST"
    });
    
    });
    
            
            
            
            
            
            jQuery(".withto").on("change", function(){
        var option = jQuery(".withto option:selected").val();
        if(option == "bank"){
            jQuery(".bankdetails").show();
        }
        else{
            jQuery(".bankdetails").hide();
        }
            });
            </script>
    
    </div>
                ';
                
                }
                else{
                    
                    echo"
                    <!--Add alert-->
                    <div class=\"alert alert-warning alert-dismissible fade show\" role=\"alert\">
                    <strong>Warning!</strong> You are not eligible for a withdrawal at the moment - <br>
                    Your Withdrawal Balance is: $total_bal_with <br>
                    Minimum Withdrawable amount is: $minwithle<br>
                    <button type=\"button\" class=\"btn-close\" data-bs-dismiss=\"alert\" aria-label=\"Close\"></button>
                    </div>
                    <!--Add alert-->
                    ";
                    
                }
                

                
    ?>


</div>