<?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');
}
?>

<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>
        <br>
        <br>
        <!-- header end -->

        <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="">You can send cash out request for balance.</h3>
                                    <p class="mt-0 mb-5">Service charge is 3%.</p>
                                    <a data-toggle="modal" data-target="#modal-formx" href="#" class="btn btn-sm btn-neutral"><i class="fa fa-arrow-right"></i> Create request</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <div class="modal fade" id="modal-formx" tabindex="-1" role="dialog" aria-labelledby="modal-form" aria-hidden="true">
                            <div class="modal-dialog modal- modal-dialog-centered modal-lg" role="document">
                                <div class="modal-content">
                                    <div class="modal-body p-0">
                                        <div class="card bg-white border-0 mb-0">
                                            <div class="card-body px-lg-5 py-lg-5">
                                                <form method="post">
                                                    <input type="hidden" name="_token" value="49hYQTXTIsbhw5u0UFOzisXtoK8nPzgIIXDezzuR">
                                                    <div class="form-group row">
                                                        <label class="col-form-label col-lg-2">Amount</label>
                                                        <div class="col-lg-10">
                                                            <div class="input-group input-group-merge">
                                                                <div class="input-group-prepend">
                                                                    <span class="input-group-text">$</span>
                                                                </div>
                                                                <input type="number" step="any" name="amount" maxlength="10" class="form-control" required="">
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="form-group row">
                                                        <label class="col-form-label col-lg-2">Method</label>
                                                        <div class="col-lg-10">
                                                            <select class="form-control select" name="method" data-dropdown-css-class="bg-primary" data-fouc required>
                                                                <option value='Bitcoin'>Bitcoin </option>
                                                                <option value='Paypal'>Paypal</option>
                                                                <option value='Skrill'>Skrill</option>
                                                                <option value='Ethereum'>Ethereum</option>
                                                            </select>
                                                        </div>
                                                    </div>
                                                    <div class="form-group row">
                                                        <label class="col-form-label col-lg-2">Details</label>
                                                        <div class="col-lg-10">
                                                            <textarea name="details" class="form-control" rows="4" required></textarea>
                                                        </div>
                                                    </div>
                                                    <div class="text-right">
                                                        <button type="submit" name='b_submit' class="btn btn-primary">Submit</button>
                                                    </div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                </div>



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

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


        <?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: 'Generate receipt'
                                            }).then((result) => {
                                                if (result.isConfirmed) {
                                                window.location.replace('receipt_withdraw.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['b_submit'])) {




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

            $charge = 3;


            // collecting the passport uploaded 




            $permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

            $ref =  substr(str_shuffle($permitted_chars), 0, 15);

            // now we need to check the amount the user inserted if it is above the account balance 

            if ($amount > $avaliable_balance) {
                $msg = 'insufficient Funds';
                Swal_error($msg);
            } else {

                // now we need to reduce the amount from the user balance 


                // now we will put it into the admin 
                // $sql_withdraw24 = "INSERT INTO with_log(user_id,amount,charges,details) VALUES('$id','$amount','$charge',$details)";
                $sql_withdraw24 = "INSERT INTO with_log(user_id,amount,charges,details,method,ref,date) VALUES('$id','$amount','$charge','$details','$method','$ref','$current_time')";
                $result_b24 = $db_conx->query($sql_withdraw24);
                if ($result_b24) {
                    $new_bal = $avaliable_balance - $amount;

                    $sql_upa = "UPDATE confidential_table SET avaliable_balance = '$new_bal' WHERE id = '$id '";
                    $query_upa  = $db_conx->query($sql_upa);

                    swal_success();
                } else {
                    $msg = 'an error has occur';
                    Swal_error($msg);
                }



                $_SESSION['amount'] = $amount;
                $_SESSION['method'] = $method;
                $_SESSION['details'] = $details;
                $_SESSION['charge'] = $charge;
                $_SESSION['ref'] = $ref;
            }
        }

        ?>

</body>


<!-- -->

</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>