<?php
	
	class forgot_password{
		private $db;
		private $query;
		private $response;
		private $data;
		private $email;
		private $pattern;
		private $length;
		private $i;
		private $password = [];
		private $indexing_number;
		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->query = $this->db->prepare("SELECT * FROM user_login");
            	$this->query->execute();
            	$this->response = $this->query->get_result();
            	if($this->response->num_rows !=0)
            	{
            		$this->data = $this->response->fetch_assoc();
            		$this->email = $this->data['email'];

            		$this->pattern = "01234567890";
					$this->length = strlen($this->pattern)-1;
					for($this->i=0;$this->i<6;$this->i++)
					{
						$this->indexing_number = rand(0,$this->length);
						$this->password[] = $this->pattern[$this->indexing_number];
					}
					$this->otp = implode($this->password);
					$this->query = $this->db->prepare("UPDATE user_login SET forgot_otp=? WHERE email=?");
					$this->query->bind_param('ss',$this->otp,$this->email);
					if($this->query->execute())
					{
							
						if(mail($this->email,"Admin Login OTP","Your OTP Is ".$this->otp))
						{
							
							$this->result = array(
								"status" => "ok",
								"cause" => "Please check your Email"
							);
							echo json_encode($this->result);
						}
					}
					else{
						$this->result = array(
							"status" => "error",
							"cause" => "unable to store otp in db"
						);
						echo json_encode($this->result);
					}

            	}
            	else{
            		$this->result = array(
						"status" => "error",
						"cause" => "Yor email not register in our database"	
					);
					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->email);
           unset($this->pattern);
           unset($this->length);
           unset($this->i);
           unset($this->password);
           unset($this->indexing_number);
           unset($this->otp);
        }
	}

	new forgot_password();
?>