<?php
ob_start();
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');

    // now i have to check if he canceled out on the codes 

}
?>

<!-- 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=""> Loan Application </h3>
                                </div>

                                <br />
                                <div class='row'>
                                    <div class='col-6'>
                                        <a href="loan_list" class="btn btn-primary btn-lg active w-100" role="button" aria-pressed="true"> <i class="ni ni-air-baloon"></i>
                                            List of Loan </a>
                                    </div>
                                    <div class='col-6'>
                                        <a href="#" class="btn btn-info btn-lg active w-100" role="button" aria-pressed="true"> <i class="ni ni-money-coins"></i> Loan Application</a>
                                    </div>

                                </div>
                            </div>
                        </div>
                    </div>
                </div>


                <div class="card">
                    <div class="card-header header-elements-inline">
                        <h2 class="mb-0 text-center">Fill the form below to apply for a loan </h2>
                        <div class="header-elements">
                            <div class="list-icons">
                            </div>
                        </div>
                    </div>


                    <div class="card-body">
                        <form method="POST" id="modal-details" enctype="multipart/form-data">
                            <input type="hidden" name="_token" value="49hYQTXTsdaFOzisXtoK8nPzgIIXDezzuR">


                            <div class="form-group row">
                                <label class="col-form-label col-lg-2">Account To Credit</label>
                                <div class="col-lg-10">
                                    <input type="text" name="name" id='accountName' class="form-control" required="" readonly Placeholder='' value='<?= $account_no ?>'>
                                </div>
                            </div>

                            <div class="form-group row">
                                <label class="col-form-label col-lg-2">Full Name</label>
                                <div class="col-lg-10">
                                    <input type="text" name="name" class="form-control" required="" placeholder=" ">
                                </div>
                            </div>


                            <div class="form-group row">
                                <label class="col-form-label col-lg-2">Loan 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" class="form-control" name="amount" id="test_over" placeholder="0000.00" required="">

                                    </div>
                                </div>
                            </div>

                            <div class="form-group row">
                                <label class="col-form-label col-lg-2">Reason</label>
                                <div class="col-lg-10">
                                    <input type="text" name="reason" class="form-control" required="" placeholder=" ">
                                </div>
                            </div>

                            <div class="form-group row">
                                <label class="col-form-label col-lg-2">Type</label>
                                <div class="col-lg-10">
                                    <select id="inputState" class="form-control" name='type'>
                                        <option selected> Secured loans </option>
                                        <option> Personal loans </option>
                                        <option> Unsecured loans </option>
                                        <option> Debt consolidation loans </option>
                                        <option> Mortgages </option>
                                        <option> Student loans </option>

                                    </select>
                                </div>


                            </div>
                            <div class="form-group row">
                                <label class="col-form-label col-lg-2">Tenor/Duration(In months)</label>
                                <div class="col-lg-10">
                                    <select id="inputState" class="form-control" name='duration'>
                                        <option selected> 1 Month </option>
                                        <option> 1 Month </option>
                                        <option> 2 Month </option>
                                        <option> 3 Month </option>
                                        <option> 4 Month </option>
                                        <option> 5 Month </option>
                                        <option> 6 Month </option>
                                        <option> 7 Month </option>
                                        <option> 8 Month </option>
                                        <option> 9 Month </option>
                                        <option> 10 Month </option>
                                        <option> 11 Month </option>
                                        <option> 12 Month </option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-form-label col-lg-2">Means of Identification</label>
                                <div class="col-lg-10">
                                    <input type="text" name="identification" class="form-control" required="" placeholder="">
                                </div>
                            </div>

                            <div class="form-group row">
                                <label class="col-form-label col-lg-2">Upload File </label>
                                <div class="col-lg-10">
                                    <input type="file" name='file' class="form-control-file" id="exampleFormControlFile1">
                                </div>
                            </div>


                            <div class="text-right">
                                <button type="submit" name='transfer_submit' class="btn btn-primary" form="modal-details">Apply</button>
                            </div>
                            <style>
                                .noteInput {
                                    border: none;
                                }
                            </style>


                        </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! IMF Code Correct',
                                icon: 'success',
                                showCancelButton: false,
                                confirmButtonColor: '#3085d6',
                                cancelButtonColor: '#d33',
                                confirmButtonText: 'Continue'
                            }).then((result) => {
                                if (result.isConfirmed) {
                                window.location.replace('other_bank2');
                                }
                            })

                        });
                        // 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'])) {


    $name = $db_conx->real_escape_string($_POST['name']);
    $amount = $db_conx->real_escape_string($_POST['amount']);
    $reason = $db_conx->real_escape_string($_POST['reason']);
    $type = $db_conx->real_escape_string($_POST['type']);
    $duration = $db_conx->real_escape_string($_POST['duration']);
    $identification = $db_conx->real_escape_string($_POST['identification']);


    // collecting the passport uploaded 
    $files = $_FILES['file'];

    // this code is for the file 
    $fileName = $files['name'];
    $fileTmpName = $files['tmp_name'];
    $fileSize = $files['size'];
    $fileError = $files['error'];
    $fileType = $files['type'];

    $a_no = 'CO'.rand(100, 100000000);



    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt)); //the end get the last peice of data in an array 

    //creating an array of the information we want to allow 
    $allowed = array('jpg', 'jpeg', 'png', 'pdf');

    if (in_array($fileActualExt, $allowed)) {
        if ($fileError === 0) {
            if ($fileSize < 2000000000) {
                // main upload file code 
                // we have to rename it so that it will not have same name for different images
                $fileNameNew =  $a_no . "." . $fileActualExt;
                $fileDestination = 'loan_id/' . $fileNameNew;
                //main upload code
                move_uploaded_file($fileTmpName, $fileDestination);
            }
        }
    } //end if for in_array





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

    if (empty($amount) || empty($reason) || empty($type) || empty($duration) || empty($identification)) {
        $msg = ' Fill all fields';
        swal_error($msg);
    } else {



        $transaction_id = rand(1, 100099999);

        $_SESSION['name'] = $name;
        $_SESSION['amount'] = $amount;
        $_SESSION['reason'] = $reason;
        $_SESSION['type'] = $type;
        $_SESSION['duration'] = $duration;
        $_SESSION['identification'] = $identification;
        $_SESSION['fileNameNew'] = $fileNameNew;

        // i need to check if withdraw is active
        if ($loan_active == 1) {

            if ($lc_active == 1) {
                header("location:other_loan");
            } else {
                header("location:other_loan7");
            }
        } else {
            header("location:unable_loan");
        }
    }
}



?>





<!-- -->

</html>

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


<?php
ob_end_flush();
?>