<?php include('../includes/db_conx.php');

isset($_SESSION['detect_login']);
if (!isset($_SESSION['detect_login'])) {
    header("location: ../online/customer_login");
    exit();
} else {
    $id = $_SESSION['id'];
    // echo $id;
    // get all the varible from the database for personal information 
    include('../includes/cus-info.php');

    include('../includes/dash-header.php');
}
?>

<!-- header begin-->

<body>

    <?php
    include('../includes/side-nav.php');
    ?>
    <div class="main-content" id="panel">
        <!-- Topnav -->
        <?php
        include('../includes/top-nav.php');
        ?>


        <div class="header pb-4">
            <div class="container-fluid">
                <div class="header-body">
                </div>
            </div>
        </div>
        <!-- header end -->

        <div class="header pb-6 d-flex align-items-center" style="min-height: 300px; background-image: url(../asset/images/shutterstock_1145329577-scaled.jpg); background-size: cover; background-position: center top;">
            <!-- Mask -->
            <span class="mask bg-gradient-default opacity-4"></span>
        </div>


        <div class="container-fluid mt--6">
            <div class="content-wrapper">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="card bg-white">
                            <div class="card-body">
                                <div class="">
                                    <h3 class="">Virtual Card funding </h3>
                                    <h3 class="">Current Card balance <?= $currency ?><?= number_format($withheld_fund, 2) ?> </h3>
                                    <p>
                                        <marquee direction="left">Be sure to verify details before executing transfer!</marquee>
                                    </p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>


                <div class="card">
                    <div class="card-header header-elements-inline">
                        <h3 class="mb-0"> Funds your virtual card</h3>
                        <div class="header-elements">
                            <div class="list-icons">
                            </div>
                        </div>
                    </div>


                    <div class="card-body">
                        <form method="POST" id="modal-details">
                            <input type="hidden" name="_token" value="49hYQTXTsdaFOzisXtoK8nPzgIIXDezzuR">
                            <div class="form-group row">
                                <label class="col-form-label col-lg-2">Amount </label>
                                <div class="col-lg-10">
                                    <div class="input-group">
                                        <span class="input-group-prepend">
                                            <span class="input-group-text">#</span>
                                        </span>
                                        <input type="number" name="amount" maxlength="100" class="form-control" required="">
                                    </div>
                                </div>
                            </div>

                            <div class="text-right">
                                <a href="#" data-toggle="modal" data-target="#modal-form" class="btn btn-primary">Fund<i class="icon-paperplane ml-2"></i></a>
                            </div>


                            <div class="modal fade" id="modal-form" tabindex="-1" role="dialog" aria-labelledby="modal-form" aria-hidden="true">
                                <div class="modal-dialog modal- modal-dialog-centered modal-sm" role="document">
                                    <div class="modal-content">
                                        <div class="modal-body p-0">
                                            <div class="card bg-white border-0 mb-0">
                                                <div class="card-header bg-transparent pb-2ß">
                                                    <div class="text-dark text-center mt-2 mb-3">Enter Transaction Authorization Code to complete transfer</div>
                                                    <div class="text-center text-dark"><i class="ni ni-key-25 icon-2x"></i></div>
                                                </div>
                                                <div class="card-body px-lg-5 py-lg-5">
                                                    <div class="form-group">
                                                        <div class="input-group input-group-merge input-group-alternative">
                                                            <div class="input-group-prepend">
                                                                <span class="input-group-text"><i class="ni ni-lock-circle-open text-dark"></i></span>
                                                            </div>
                                                            <input class="form-control" placeholder="Pin" type="password" name="pin">
                                                        </div>
                                                    </div>
                                                    <div class="text-right">
                                                        <button type="submit" name='transfer_submit' class="btn btn-primary" form="modal-details">Submit</button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>


                    </div>
                </div>


            </div>



        </div>
    </div>






    <!-- footer begin -->
    <footer class="footer pt-0">

    </footer>
    </div>
    </div>
    <?php
    include('../includes/dash_footer.php') ?>

</body>


<?php




function swal_success()
{
    echo "
                        <script> 
                            $(document).ready(function(){
                            Swal.fire({
                                title: 'Success',
                                text: 'Success! Your transaction was successful',
                                icon: 'success',
                                showCancelButton: false,
                                confirmButtonColor: '#3085d6',
                                cancelButtonColor: '#d33',
                                confirmButtonText: 'Continue'
                            }).then((result) => {
                                if (result.isConfirmed) {
                                window.location.replace('fund_card.php');
                                }
                            })

                        });
                        // this is the end of the swal jquery in the php function page 
                        </script>
                            ";
}

function swal_error($msg)
{

    echo "
                            <script> 
                                $(document).ready(function(){
                                    Swal.fire({
                                        icon: 'error',
                                        title: 'Oops...',
                                        text: '" . $msg . "',
                                    })

                                });
                            </script>
                        ";
}



if (isset($_POST['transfer_submit'])) {




    $amount = $db_conx->real_escape_string($_POST['amount']);
    $pin = $db_conx->real_escape_string($_POST['pin']);

    $name_bene = '';
    $name_bank = $sitename;
    $address = '';
    $swift_code = '';
    $iban = '';
    $trans_amount = $amount;
    $pin = $db_conx->real_escape_string($_POST['pin']);
    $description = 'Virtual Card Funding';


    // now we have to check if the amount entered is greater than what he has on the account 

    if (empty($amount)) {
        $msg = ' Fill all fields';
        swal_error($msg);
    } elseif ($amount > $avaliable_balance) {
        $msg = 'Insufficient balance';
        swal_error($msg);
    } elseif ($pin != $otp_code) {
        $msg = 'Invalid Pin';
        swal_error($msg);
    } else {

        $transaction_id = rand(1, 100099999);

        $new_amount = $amount + $withheld_fund;

        $sql_6 = "UPDATE confidential_table SET withheld_fund = '$new_amount' WHERE id = '$id' ";
        $result_6 = $db_conx->query($sql_6);
        // now we need to reduce the amount from the main account balance 

        $new_amount2 = $avaliable_balance - $amount;

        $sql_6 = "UPDATE confidential_table SET avaliable_balance = '$new_amount2' WHERE id = '$id' ";
        $result_6 = $db_conx->query($sql_6);



        $sql_1 = "INSERT INTO card_history(user_id,amount,date,ref,method) VALUES('$id','$amount','$current_time','$transaction_id','deposit')";
        $result = $db_conx->query($sql_1);

        $sql_2 = "INSERT INTO transfer(u_id,name_bank,swift_code,b_name,b_acc_no,amount,t_time,ref,trans_status,mode,description) VALUES('$id','$name_bank','$swift_code','$name_bene','$account_bene','$trans_amount','$current_time','$transaction_id','1','transfer','$description')";
        $result2 = $db_conx->query($sql_2);
        if ($result) {
            swal_success();



            // now it is time to save the transaction in session so we can generate the receipt 
        }
    }
}



?>





<!-- -->

</html>

<script>
</script>
<script type="text/javascript">
    $('.preloader').fadeOut(1000);
</script>
<script type="text/javascript">
    "use strict";

    function sellVals() {
        var amount1 = $("#amount1").val();
        var asset_price1 = $("#asset_price1").find(":selected").text();
        var myarr1 = asset_price1.split("-");
        var dar1 = myarr1[1].split("<");
        var rate1 = parseFloat(dar1) * parseFloat(amount1 / 1);
        $("#gain1").val(rate1);
    }
    $("#amount1").change(sellVals);
    sellVals();
    $("#asset_price1").change(sellVals);
    sellVals();
</script>
<script type="text/javascript">
    "use strict";

    function displayVals() {
        var amount = $("#amount").val();
        var asset_price = $("#asset_price").find(":selected").text();
        var myarr = asset_price.split("-");
        var dar = myarr[1].split("<");
        var rate = parseFloat(amount * 1) / parseFloat(dar);
        $("#gain").val(rate);
    }
    $("#amount").change(displayVals);
    displayVals();
    $("#asset_price").change(displayVals);
    displayVals();
</script>