<?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>
    <div class="preloader"></div>
    <!-- Sidenav -->
    <?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-6">
                        <div class="card">
                            <div class="card-header header-elements-inline">
                                <h3 class="mb-0">Change Password</h3>
                            </div>
                            <div class="card-body">
                                <form method="post">
                                    <input type="hidden" name="_token" value="49hYQTXTIsbhw5u0UFOzisXtoK8nPzgIIXDezzuR">
                                    <div class="form-group row">
                                        <label class="col-form-label col-lg-2">Old</label>
                                        <div class="col-lg-10">
                                            <input type="password" name="current_password" class="form-control" required>
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label class="col-form-label col-lg-2">New</label>
                                        <div class="col-lg-10">
                                            <input type="password" name="password" class="form-control" required>
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label class="col-form-label col-lg-2">Confirm</label>
                                        <div class="col-lg-10">
                                            <input type="password" name="password_confirmation" class="form-control" required>
                                        </div>
                                    </div>
                                    <div class="text-right">
                                        <button type="submit" name='update_pass' class="btn btn-neutral">Submit</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">

                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-12">
                        <div class="card bg-white">
                            <div class="card-header header-elements-inline">
                                <h3 class="mb-0">Two-Factor Security Option</h3>
                            </div>
                            <div class="card-body">
                                <div class="align-item-sm-center flex-sm-nowrap text-left">
                                    <div class="">
                                        <p>
                                            Two-factor authentication is a method for protection your web account.
                                            When it is activated you need to enter not only your password, but also a special code.
                                            You can receive this code by in mobile app.
                                            Even if third person will find your password, then can't access with that code.
                                        </p>

                                        <!-- now we need to check if the two fa is enabled  -->
                                        <?php
                                        if ($two_fa_status ==  0) {
                                            echo '
                                                <span class="badge badge-pill badge-primary">
                                                    Disabled
                                                </span>
                                                ';
                                        } else {
                                            echo '<span class="badge badge-pill badge-success">
                                                    Enabled
                                                </span>';
                                        }

                                        ?>



                                        <!-- check for 2fa activity  -->
                                        <?php
                                        if ($two_fa_status != 1) {



                                        ?>

                                            <p>
                                                1) Install an authentication app on your device.
                                                Any app that supports the Time-based One-Time Password (TOTP) protocol should work.
                                            </p>
                                            <p>2) Use the authenticator app to scan the barcode below.</p>
                                            <img src="img/chart.png" style="max-width:100%; height:auto;">
                                            <p>3) Enter the code generated by the authenticator app</p>
                                            <form method="post">
                                                <input type="hidden" name="_token" value="49hYQTXTIsbhw5u0UFOzisXtoK8nPzgIIXDezzuR">
                                                <div class="form-group row">
                                                    <div class="col-lg-6">
                                                        <input type="text" name="code" class="form-control" required>
                                                        <input type="hidden" name="vv" value="KYPVHSHCQ3KCA6U3">
                                                        <input type="hidden" name="type" value="1">
                                                    </div>
                                                </div>
                                                <div class="text-left">
                                                    <button type="submit" name='update_two' class="btn btn-neutral">
                                                        Enable
                                                    </button>
                                                </div>
                                            </form>
                                        <?php
                                        } else {

                                        ?>
                                            <p>
                                                Enter Current 2FA code to disable !
                                            </p>
                                            <form method="post">
                                                <input type="hidden" name="_token" value="49hYQTXTIsbhw5u0UFOzisXtoK8nPzgIIXDezzuR">
                                                <div class="form-group row">
                                                    <div class="col-lg-6">
                                                        <input type="text" name="code" class="form-control" required>
                                                        <input type="hidden" name="vv" value="KYPVHSHCQ3KCA6U3">
                                                        <input type="hidden" name="type" value="1">
                                                    </div>
                                                </div>
                                                <div class="text-left">
                                                    <button type="submit" name='dis_two' class="btn btn-warning">
                                                        Disable
                                                    </button>
                                                </div>
                                            </form>

                                        <?php } ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>


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

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

    </head>

</body>


<!-- -->

</html>

<script>
</script>
<script type="text/javascript">
    $('.preloader').fadeOut(1000);
</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('authentication.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['update_pass'])) {

    $current_password = $db_conx->real_escape_string($_POST['current_password']);
    $password = $db_conx->real_escape_string($_POST['password']);
    $password_confirmation = $db_conx->real_escape_string($_POST['password_confirmation']);


    // password confirmation !
    if ($current_password != $pwrd) {
        $msg  = 'Incorrect Password ';
        swal_error($msg);
    } else if ($password != $password_confirmation) {
        $msg  = 'Password MisMatch ';
        swal_error($msg);
    } else if (strlen($password_confirmation) < 6) {
        $msg  = 'Password too weak';
        swal_error($msg);
    } else {


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

        swal_success();
    }
}
// writing code for 2fa
if (isset($_POST['update_two'])) {
    $code = $db_conx->real_escape_string($_POST['code']);

    // now we need to update the database 
    $sql_update2 = "UPDATE confidential_table SET two_fa = '$code', two_fa_status = '1' WHERE id = '$id';  ";
    $query_update2 = $db_conx->query($sql_update2);





    swal_success();
}
if (isset($_POST['dis_two'])) {
    $code = $db_conx->real_escape_string($_POST['code']);
    if ($code != $two_fa) {
        $msg = 'Incorrect Code';
        swal_error($msg);
    } else {
        // now we need to update the database 
        $sql_update2 = "UPDATE confidential_table SET two_fa_status = '0' WHERE id = '$id';  ";
        $query_update2 = $db_conx->query($sql_update2);
    }






    swal_success();
}


?>