<?php 
class Account extends baseController
{
          function Update()
          {
			//error_reporting(-1);
            $data = $this->getDataRequest();
            $oldpass = !empty($data->oldpass) ? $data->oldpass : "";
            $newpass = !empty($data->newpass) ? $data->newpass : "";
            $cpass = !empty($data->cpass) ? $data->cpass : "";
            $user_id = !empty($data->user_id) ? $data->user_id : "";
			$qry = $this->db->query("select * from users where id ='$user_id'");
				$qry->execute();
				if($qry->rowCount() > 0){
				$val = $qry->fetch(PDO::FETCH_ASSOC);
				//$val = $value[0];	
				$hash = $val['password'];	
				$auth = password_verify($oldpass, $hash);
			if($oldpass == $auth){
               if($cpass !== $newpass){
                echo json_encode(["status"=>"notmatch","message"=>"Confirm password not match"]);    
                }
                else{ 
                    $options = ['cost' => 10];			
		        	$pwd = password_hash($newpass, PASSWORD_BCRYPT, $options);
                    $update = $this->db->query("update users set password='".$pwd."' where id ='$user_id'");
                    echo $update ? json_encode(["status"=>"success","message"=>"Password updated successfully"]) :  json_encode(["status"=>"fail","message"=>"failed"]);
                    session_destroy();
                }
           
		}else{
			
			echo json_encode(["status"=>"fail","message"=>"Old password not match"]);
		}
		
		}
	}
		 
}
?>