<?php
	class change_password{
		private $db;
		private $query;
		private $response;
		private $data;
		private $new_pwd;
		private $confirm_pwd;
		private $otp;
		private $result;

		function __construct()
		{
			require_once("../db.php");
			$this->db = new websupportservice();
            $this->db = $this->db->__construct();
            if(!$this->db->connect_error)
            {
            	$this->new_pwd = trim($_POST['new_pwd']);
            	$this->new_pwd = htmlspecialchars($this->new_pwd,ENT_QUOTES);
            	$this->new_pwd = mysqli_real_escape_string($this->db,$this->new_pwd);
            	$this->new_pwd = md5($this->new_pwd);

            	$this->confirm_pwd = trim($_POST['confirm_pwd']);
            	$this->confirm_pwd = htmlspecialchars($this->confirm_pwd,ENT_QUOTES);
            	$this->confirm_pwd = mysqli_real_escape_string($this->db,$this->confirm_pwd);
            	$this->confirm_pwd = md5($this->confirm_pwd);

            	$this->otp = trim($_POST['otp']);
            	$this->otp = htmlspecialchars($this->otp,ENT_QUOTES);
            	$this->otp = mysqli_real_escape_string($this->db,$this->otp);

            	if($this->new_pwd == $this->confirm_pwd)
            	{
            		$this->query = $this->db->prepare("UPDATE user_login SET user_pw=? WHERE forgot_otp=?");
            		$this->query->bind_param('ss',$this->confirm_pwd,$this->otp);
					$this->response = $this->query->execute();
					if($this->response)
					{
						$this->response = $this->db->query("UPDATE user_login SET forgot_otp='null' WHERE user_id='admin'");
						
						if($this->response)
						{
							$this->result = array(
								"status" => "ok"
							);
							echo json_encode($this->result);
						}
						else{
							$this->result = array(
							"status" => "error",
							"cause" => "OTP not Expired"	
							);
							echo json_encode($this->result);
						}
					}
					else{
						$this->result = array(
						"status" => "error",
						"cause" => "OTP Expired"	
						);
						echo json_encode($this->result);
					}
            	}
            	else{
            		$this->result = array(
					"status" => "error",
					"cause" => "Password not matched"	
					);
					echo json_encode($this->result);
	            }
            }
            else{
            	$this->result = array(
					"status" => "error",
					"cause" => "Database not connected"	
				);
				echo json_encode($this->result);
            }
    	}

    	function __destruct(){
          $this->query->close();
          $this->db->close();

           unset($this->db);
           unset($this->query);
           unset($this->response);
           unset($this->data);
           unset($this->new_pwd);
           unset($this->confirm_pwd);
           unset($this->otp);
           unset($this->result);
        }
	}
	new change_password();
?>