<?php session_start();
include_once('includes/config.php');
if (strlen($_SESSION['id']) == 0) {
    header('location:logout.php');
} else {

    //For changing  User  Profile Password
    if (isset($_POST['update'])) {
        $currentpwd = md5($_POST['cpass']);
        $newpwd = md5($_POST['newpass']);
        $uid = $_SESSION['id'];
        $sql = mysqli_query($con, "SELECT id FROM  users where password='$currentpwd' and id='$uid'");
        $num = mysqli_num_rows($sql);
        if ($num > 0) {
            $con = mysqli_query($con, "update users set password='$newpwd' where id='$uid'");
            echo "<script>alert('Password Changed Successfully !!');</script>";
            echo "<script type='text/javascript'> document.location ='change-password.php'; </script>";
        } else {
            echo "<script>alert('Current Password not match !!');</script>";
            echo "<script type='text/javascript'> document.location ='change-password.php'; </script>";
        }
    }
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <?php include('includes/head.php'); ?>
    <script type="text/javascript">
        function valid() {
            if (document.chngpwd.cpass.value == "") {
                alert("Current Password Filed is Empty !!");
                document.chngpwd.cpass.focus();
                return false;
            }
            else if (document.chngpwd.newpass.value == "") {
                alert("New Password Filed is Empty !!");
                document.chngpwd.newpass.focus();
                return false;
            }
            else if (document.chngpwd.cnfpass.value == "") {
                alert("Confirm Password Filed is Empty !!");
                document.chngpwd.cnfpass.focus();
                return false;
            }
            else if (document.chngpwd.newpass.value != document.chngpwd.cnfpass.value) {
                alert("Password and Confirm Password Field do not match  !!");
                document.chngpwd.cnfpass.focus();
                return false;
            }
            return true;
        }
    </script>
    <style type="text/css"></style>

    <body class="cnt-home">
        <header class="header-style-1">
            <?php include('includes/top-header.php'); ?>
            <?php include('includes/main-header.php'); ?>
            <?php include('includes/menu-bar.php'); ?>
        </header>

        <!-- Breadcrumb -->
        <div class="breadcrumb mt-2">
            <div class="container">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="index.php">Home</a></li>
                        <li class="breadcrumb-item"><a href="my-profile.php">My Profile</a></li>
                        <li class="breadcrumb-item active">Change Password</li>
                    </ol>
                </nav>
            </div>
        </div>

        <!-- Main Content -->
        <div class="body-content outer-top-xs">
            <div class="container">
                <!-- Section-->
                <section class="py-5">
                    <div class="container px-4  mt-5">

                        <form method="post" name="chngpwd" onSubmit="return valid();">
                            <div class="row">
                                <div class="col-2">Current Password</div>
                                <div class="col-6">
                                    <input type="password" class="form-control" id="cpass" name="cpass" required="required">
                                </div>
                            </div>
                            <div class="row mt-3">
                                <div class="col-2">New Password</div>
                                <div class="col-6">
                                    <input type="password" class="form-control" id="newpass" name="newpass">
                                </div>

                            </div>

                            <div class="row mt-3">
                                <div class="col-2">Confirm Password</div>
                                <div class="col-6"><input type="password" class="form-control" id="cnfpass" name="cnfpass"
                                        required="required"></div>
                            </div>



                            <div class="row mt-3">
                                <div class="col-4">&nbsp;</div>
                                <div class="col-6"><input type="submit" name="update" id="update" class="btn btn-primary"
                                        value="Update" required></div>
                            </div>
                        </form>

                    </div>

            </div>
        </div><!-- /.container -->
        </div><!-- /.body-content -->

        <!-- Footer -->
        <?php include('includes/footer.php'); ?>
        <?php include('includes/foot.php'); ?>

    </html>
<?php } ?>
