<?php
session_start();
error_reporting(0);
include('config.php');
if(strlen($_SESSION['clogin'])==0)
{
    echo "<script>window.location.href='login.php';</script>";
}
else
{
 /* update password */
  if(isset($_POST['update_password']))
  {
   /*$password=md5($_POST['txtpass']);*/
   $password=$_POST['oldpassword'];
   $newpassword=$_POST['newpassword'];
   $username=$_SESSION['clogin'];
   $sql ="SELECT password FROM tbl_admin WHERE email=:username and password=:password";
   $query= $dbh -> prepare($sql);
   $query-> bindParam(':username', $username, PDO::PARAM_STR);
   $query-> bindParam(':password', $password, PDO::PARAM_STR);
   $query-> execute();
   $results = $query -> fetchAll(PDO::FETCH_OBJ);
   if($query -> rowCount() > 0)
   {
    $con="update tbl_admin set password=:newpassword where email=:username";
    $chngpwd1 = $dbh->prepare($con);
    $chngpwd1-> bindParam(':username', $username, PDO::PARAM_STR);
    $chngpwd1-> bindParam(':newpassword', $newpassword, PDO::PARAM_STR);
    $chngpwd1->execute();
    echo "<script>
    alert('Your Password succesfully changed !.');
    </script>";
//end mail srcipt
  }
  else
  {
    echo "<script>
    alert('Your current password is wrong !.');
    </script>";
  /*
  $error="Your current password is wrong";
  */
  }
  }
}
?>

<!DOCTYPE html>
<html>
<head>
   <?php include 'style.php';?>
</head>
<body>

<?php include 'header.php';?>



        <script type="text/javascript">
  function valid() {
    var password = document.getElementById("newpassword").value;
    var confirmPassword = document.getElementById("confirmpassword").value;
    if (password != confirmPassword) {
      alert("Password and confirmpassword do not match.Enter correct password");
      return false;
    }
    return true;
  }
</script>
                                            

<div class="topnav" style="background-color:transparent">

    <a href="manager.php" class="split" style="background-color:#ff6c2c;border-radius:20px"><i class="fa fa-users"></i> &nbsp;&nbsp;Manager</a>
    <a href="master.php" class="split"><i class="fa fa-line-chart"></i> &nbsp;&nbsp;Master</a>
    <a href="index.php" class="split"><i class="fa fa-user"></i> &nbsp;&nbsp;Profiles</a>
</div>

<div class="container" style="padding:20px">
    <div class="form-container" style="padding:20px">
        <h2 style="text-align:center; color: #fff;">Change Password</h2>
        <!-- Form -->
        <form method="POST" style="max-width: 400px; margin: auto; background: #333; padding: 20px; border-radius: 10px;">
            <div class="form-group" style="padding-right:20px">
                <label for="login-id" style="color: #fff;">Old Password</label>
                <input type="text" id="oldpassword" name="oldpassword" required class="form-control" style="width: 100%; padding: 10px; margin: 10px 0;">
            </div>
            
            <div class="form-group" style="padding-right:20px">
                <label for="login-id" style="color: #fff;">New Password</label>
                <input type="text"  id="newpassword"  name="newpassword" required class="form-control" style="width: 100%; padding: 10px; margin: 10px 0;">
            </div>
            <div class="form-group" style="padding-right:20px">
                <label for="server" style="color: #fff;">Confirm Password</label>
                <input type="text" id="confirmpassword" name="confirmpassword" required class="form-control" style="width: 100%; padding: 10px; margin: 10px 0;">
            </div>

            <div class="form-group" style="text-align: center;padding-right:20px">
                <input type="submit" class="btn" name="update_password" onclick="return valid();" value="Change Password" style="background-color: #2e8b57; color: #fff; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer;">
            </div>
        </form>
    </div>
</div>

</body>
</html>
