<?php

$errmsg='';
$sucmsg='';
if(isset($_POST['chgpswd'])){
    extract($_POST);
    $errmsg='';
    $sucmsg='';
    $oldpswd=trim($oldpswd);
    $newpswd=trim($newpswd);
    $conpswd=trim($conpswd);
    //$oldpswd=$oldpswd;
    $oldpswd=sha1($oldpswd);
    $newpswd=sha1($newpswd);
    $conpswd=sha1($conpswd);
    if($newpswd!=$conpswd){
        $errmsg='New password and Confirm password mismatch';
    }
    $qry=mysqli_query($con, "Select password from exam_admin where id=$admin_id");
    $row=mysqli_fetch_assoc($qry);
    $old=$row['password'];
    
    
    if($oldpswd!=$old){
        $errmsg='Old password is incorrect';
    }else{
        $qry2=mysqli_query($con, "UPDATE exam_admin SET password='$newpswd' WHERE id=$admin_id");
        if($qry2){
            $sucmsg='Password Changed Successfully!';
        }else{
            $errmsg=die(mysqli_error($con));
        }
        
    }
}
?>

<div class="graph-visual tables-main">
<h2 class="inner-tittle">CHANGE PASSWORD</h2>
	<div class="graph-form">
    		<div class="form-body">
    			<form method="post" name="password" action="index.php?page=password&i=1"> 
                <?php 


                if($errmsg!=''){
                    echo '<div class="alert alert-danger">'.$errmsg.'</div>';
                }elseif($sucmsg!=''){
                    echo '<div class="alert alert-success">'.$sucmsg.'</div>';
                }
                
                ?>
                    <div class="form-group"> 
                        <label for="oldpswd">Old Password</label> 
                        <input type="password" class="form-control" name="oldpswd" placeholder="Old Password" required=""/> 
                    </div> 
                    <div class="form-group"> 
                        <label for="newpswd">New Password</label> 
                        <input type="password" class="form-control" name="newpswd" placeholder="New Password" required=""/> 
                    </div> 
                    <div class="form-group"> 
                        <label for="conpswd">Confirm New Password</label> 
                        <input type="password" class="form-control" name="conpswd" placeholder="Confirm New Password" required=""/> 
                    </div>
                    <input name="chgpswd" type="submit" class="btn btn-default" value="Change Password"/> 
                </form> 
    		</div>
    
    </div>

</div>