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

        <!-- Page content -->
        <div class="container-fluid mt--6">
            <div class="content-wrapper">
                <div class="row">
                    <div class="col-md-8">
                        <div class="card" id="edit">
                            <div class="card-header header-elements-inline">
                                <h3 class="mb-0">Update account information</h3>
                            </div>
                            <div class="card-body">
                                <form action="" method="post">
                                    <input type="hidden" name="_token" value="49hYQTXTIsbhw5u0UFOzisXtoK8nPzgIIXDezzuR">
                                    <div class="form-group row">
                                        <label class="col-form-label col-lg-2"></label>
                                        <div class="media align-items-center">
                                            <span class="col-lg-10">
                                                <img src="<?php echo 'profile_img/' . $passport ?>" class="" alt="Image placeholder" class="rounded" width="115" height="111">

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

                                    <div class="form-group row">
                                        <label class="col-form-label col-lg-2">Account#</label>
                                        <div class="col-lg-10">
                                            <a data-toggle="modal" data-target="" class="btn btn-sm btn-neutral"><?= $account_no ?> </a>
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label class="col-form-label col-lg-2">Account Type</label>
                                        <div class="col-lg-10">
                                            <a data-toggle="modal" data-target="" class="btn btn-sm btn-neutral"><?= $account_type ?> </a>
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label class="col-form-label col-lg-2">Name</label>
                                        <div class="col-lg-10">
                                            <input type="text" name="name" class="form-control" Readonly value="<?= $first_name . ' ' . $last_name ?>">
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label class="col-form-label col-lg-2">Mobile</label>
                                        <div class="col-lg-10">
                                            <input type="text" name="phone" class="form-control" value="<?= $phone_number ?> ">
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label class="col-form-label col-lg-2">Country</label>
                                        <div class="col-lg-10">
                                            <input type="text" name="country" class="form-control" value="<?= $nation ?>">
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label class="col-form-label col-lg-2">City</label>
                                        <div class="col-lg-10">
                                            <input type="text" name="city" class="form-control" value="<?= $city ?>">
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label class="col-form-label col-lg-2">Zip code</label>
                                        <div class="col-lg-10">
                                            <input type="text" name="zip_code" class="form-control" value="<?= $zip_code ?>">
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label class="col-form-label col-lg-2">Address</label>
                                        <div class="col-lg-10">
                                            <input type="text" name="address" class="form-control" value="<?= $address ?>">
                                        </div>
                                    </div>
                                    <div class="text-right">
                                        <button type="submit" name='b_submit' class="btn btn-neutral">Update<i class="icon-paperplane ml-2"></i></button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>


                    <div class="col-md-4">
                        <div class="card" id="kyc">
                            <div class="card-body">
                                <h3 class="card-title mb-3">Kyc verification</h3>

                                <p class="card-text mb-4">Upload an identity document, for example, driver license, voters card, international passport, national ID.</p>

                                <?php
                                if ($kyc_status ==  1) {
                                    echo '
                                        <span class="badge badge-pill badge-primary">
                                            Verified
                                        </span>
                                        ';
                                } elseif ($kyc_status ==  0) {
                                    echo '
                                        <span class="badge badge-pill badge-warning">
                                            Verified
                                        </span>
                                        ';
                                } else {



                                ?>
                                    <span class="badge badge-pill badge-danger">
                                        not Verified
                                    </span>
                                    <br><br>

                                    <div class="card-body">
                                        <form enctype="multipart/form-data" method="post">
                                            <input type="hidden" name="_token" value="49hYQTXTIsbhw5u0UFOzisXtoK8nPzgIIXDezzuR">
                                            <div class="form-group">
                                                <div class="custom-file">
                                                    <input type="file" class="custom-file-input" id="customFileLang" name="file" accept="image/*" required>
                                                    <label class="custom-file-label" for="customFileLang">Select file</label>
                                                </div>
                                                <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
                                                <span class="form-text text-muted">Accepted formats:png, jpg.</span>
                                            </div>
                                            <div class="text-right">
                                                <button type="submit" name='upload_cert' class="btn btn-neutral">Upload<i class="icon-paperplane ml-2"></i></button>
                                            </div>
                                        </form>
                                    </div>

                                <?php

                                }

                                ?>

                            </div>
                        </div>
                        <div class="card">
                            <div class="card-header header-elements-inline">
                                <h3 class="mb-0">Change account photo</h3>
                            </div>

                            <div class="card-body">
                                <form enctype="multipart/form-data" method="post">
                                    <input type="hidden" name="_token" value="49hYQTXTIsbhw5u0UFOzisXtoK8nPzgIIXDezzuR">
                                    <div class="form-group">
                                        <div class="custom-file">
                                            <input type="file" class="custom-file-input" id="customFileLang" name="file" accept="image/*" required>
                                            <label class="custom-file-label" for="customFileLang">Select photo</label>
                                        </div>
                                        <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
                                        <span class="form-text text-muted">Accepted formats:png, jpg.</span>
                                    </div>
                                    <div class="text-right">
                                        <button type="submit" name='upload_photo' class="btn btn-neutral">Upload<i class="icon-paperplane ml-2"></i></button>
                                    </div>
                                </form>
                            </div>
                        </div>

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


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

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

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

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



<?php




function swal_success()
{
    echo "
        <script> 
            $(document).ready(function(){
            Swal.fire({
                title: 'Success',
                text: 'Success!',
                icon: 'success',
                showCancelButton: false,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Continue'
            }).then((result) => {
                if (result.isConfirmed) {
                window.location.replace('profile.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>
";
}


// writing code for the profile photo 
if (isset($_POST['upload_cert'])) {

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


    $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 =  $account_no . "." . $fileActualExt;
                $fileDestination = 'kyc/' . $fileNameNew;
                //main upload code
                move_uploaded_file($fileTmpName, $fileDestination);
            }
        }
    } //end if for in_array

    // now we need to update the database 
    $sql_update2 = "UPDATE personal_information SET kyc_doc = '$fileNameNew' WHERE id = '$id';  ";
    $query_update2 = $db_conx->query($sql_update2);

    swal_success();
}


// writing code for the profile photo 
if (isset($_POST['upload_photo'])) {

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


    $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 =  $account_no . "." . $fileActualExt;
                $fileDestination = 'profile_img/' . $fileNameNew;
                //main upload code
                move_uploaded_file($fileTmpName, $fileDestination);
            }
        }
    } //end if for in_array

    $sql_update2 = "UPDATE personal_identification SET passport = '$fileNameNew' WHERE id = '$id';  ";
    $query_update2 = $db_conx->query($sql_update2);



    swal_success();
}






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




    $phone = $db_conx->real_escape_string($_POST['phone']);
    $country = $db_conx->real_escape_string($_POST['country']);
    $city = $db_conx->real_escape_string($_POST['city']);
    $zip_code = $db_conx->real_escape_string($_POST['zip_code']);
    $address = $db_conx->real_escape_string($_POST['address']);




    // there will be no need to check for empty space 


    $sql_update2 = "UPDATE personal_information SET phone_number = '$phone',nation = '$country',city = '$city', zip_code = '$zip_code',address = '$address' WHERE id = '$id'";
    $query_update2 = $db_conx->query($sql_update2);

    if ($query_update2) {
        swal_success();
    } else {
        $msg  = 'an error has occurred';
        swal_error($msg);
    }






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




}

?>