<?php
include 'db_connection.php';

/* start the session */
session_start();

if(!isset($_SESSION['username'])){ // Jika session username ada berarti dia sudah login   
  echo "<script>  window.location.href='../../login.php';</script>"; // Kita Redirect ke halaman welcome.php
}

if (isset($_SESSION['username'])) {       
    $data = mysqli_query($conn,"SELECT * FROM user WHERE status IN ('I','D') AND user_id ='".$_SESSION['username']."'");
    $cek_usrBaned = mysqli_num_rows($data);

    if($cek_usrBaned > 0){ // found the banned user       
      session_destroy(); 
      echo "<script>  window.location.href='../../login.php';</script>";
    }

} else {    

}

?>

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="sweetalert-master/docs/assets/sweetalert/sweetalert.min.js"></script>
<script src="../E-Leave/jquery/jquery-3.4.1.min.js"></script>
<style>
/* Style all input fields */
input.ipwd {
  width: 30%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
  margin-top: 6px;
  margin-bottom: 16px;
}

input.spwd {
  width: 15%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
  margin-top: 6px;
  margin-bottom: 16px;
}

/* Style the submit button */
input[type=submit] {
  background-color: #4CAF50;
  color: white;
  cursor:pointer;
}

/* Style the container for inputs */
.container {
  background-color: #f1f1f1;
  padding: 20px;
}

form { 
/*text-align: center;*/
}

div.header{
text-align: center;
background-color: #4CAF50;
}

div.message{
text-align: center;
background-color: #FF0000;
}

table.tblchangepwd{
}
</style>

</head>

<body>
<div class="header">
<h3>Change Password</h3>
</div>

<div class="container">
  <form method="post" action="change_password.php">
      <div class="message">  
      <?php
        //include 'db_con_splitrow_users.php';
        if(isset($_POST['save_click'])) {
            //session_start();
            $cek_uid =$_SESSION['username']; 
            $password = $_POST["confirm"];
            $datetime = date('Y-m-d H:i:s'); 
            
            //query cek old password            
           $data = "SELECT * FROM user where user_id = '$cek_uid'";
           $result = mysqli_query($conn, $data);           

           while($row = mysqli_fetch_assoc($result)) {						
                $oldpasswordDB = $row['user_pwd'];     		
            }
            //echo "<script type='text/javascript'>alert('{$oldpassword}');</script>";            
            //check fields
                $oldpassword = sha1($_POST['oldpwd']);  
            //echo "<script type='text/javascript'>alert('{$oldpassword}');</script>";   
            
            //validation password match required            
                if ($oldpassword != $oldpasswordDB) { 
                    echo "<h4><align='center'><font color=white>Old password doesnt match!</font></h4>";
                }
                elseif ($_POST["newpwd"] === $_POST["confirm"]) {
                $sql  = ("UPDATE user SET user_pwd = sha1('$password'), update_by = '$cek_uid', last_activity = '$datetime' WHERE user_id = '$cek_uid'");        
                $query = mysqli_query($conn, $sql); 
                 echo '
                    <script>                    
					   alert("Change Password Successfully");
                       window.location.href="logout.php";
                    </script>
                    ';                         
                }
                else{                   
                    echo "<h4><align='center'><font color=white>New Password and Confirm Password not match</font></h4>";                
                }}
                                
            if(isset($_POST['cancel'])) { 
                echo "
                   <script>  
                        window.location.href='../../index.php';
                   </script>     
                    ";                             
                }
     ?>    
       </div>   
<table class="tblchangepwd" width="100%">
    <tr>
        <td width="40%">
        </td>
        <td> 
            <label class="newpwd">Old Password</label><br>
            <input type="password" id="oldpwd" name="oldpwd" class="ipwd" required><br>    
            <label class="newpwd">New Password</label><br>
            <input type="password" id="newpwd" name="newpwd" class="ipwd" required><br>
            <label class="confirm">Confirm Password</label><br>
            <input type="password" id="confirm" name="confirm" class="ipwd" required><br>             
            <input type="submit" value="Cancel" name="cancel" class="spwd" formnovalidate>            
            <input type="submit" value="Save" name="save_click" class="spwd"> 
        </td>
        <td>  
        </td>
    </tr>
</table>  
  </form>       
</div>    
</body>
</html>
