<?php
    session_start();
    include "konnection.php";
    include "func.php";
    
    if(!isset($_SESSION["user"])){
        header("location: login.php");
        die(json_encode(array("status"=>false,"message"=>"Auth Invalid")));
    }
    
    $email  = $_SESSION["user"];
    $user = getUserDetails($email);
    $vars = json_decode(file_get_contents("php://input"));
    if(!isset($vars->oldPassword)){
        die(json_encode(array("message"=>"Current Password Not Found")));
    }if(empty($vars->oldPassword)){
        die(json_encode(array("message"=>"Current Password  Empty")));
    }if(!isset($vars->newPassword)){
        die(json_encode(array("message"=>"New Password Not Found")));
    }if(empty($vars->newPassword)){
        die(json_encode(array("message"=>"New Password Empty")));
    }if(!isset($vars->confirmNewPassword)){
        die(json_encode(array("message"=>"Confirm New Password Not Found")));
    }if(empty($vars->confirmNewPassword)){
        die(json_encode(array("message"=>"Confirm New Password Empty")));
    }
    if(isset($user["password"])){
        if($vars->oldPassword == $user["password"]){
            if($vars->newPassword == $vars->confirmNewPassword){
               $up = mysqli_query($conn, "UPDATE users SET password = '$vars->newPassword' WHERE email = '$email'"); 
               if($up){
                   die(json_encode(array("status" => true, "message"=>"Password changed!")));
               }else{
                
                   die(json_encode(array("message"=>"Try Later")));
               }
            }else{
    
                die(json_encode(array("message"=>"Password and Confirm doesn't matched")));
            }
        }else{

            die(json_encode(array("message"=>"Current Password doesn't matched")));
        }
        echo $user["password"];
    }else{
        die(json_encode(array("message"=>"Authorization Not FOund")));
    }
     ?>