<?php 

class DbOperations 
{
	
	private $con;

	function __construct(){
		require_once dirname(__FILE__).'/DbConnects.php';

		$db = new DbConnect();
		$this -> con = $db -> connect();
	}

	//CREATE USERS //

	public function createUser($username, $password, $email,$role){
		if($this -> isUserExist($username, $email)){
				return 0;
		}else{
		$password = md5($password);
		$stmt = $this -> con -> prepare("INSERT INTO users (id,username,password,email,role) VALUES (NULL, ?,?,?,?);");
		$stmt -> bind_param ("ssss", $username,$password, $email, $role);

// 		$table = $username . "_orders_table";
// 		$stmtt = $this -> con -> prepare("CREATE TABLE IF NOT EXISTS $table (
//     id INT AUTO_INCREMENT,
//     orders LONGTEXT,
//     update_time TIMESTAMP,
//     day TEXT,
//     user text,
//     status text,
//     PRIMARY KEY (id)
// )  ENGINE=INNODB");

		
		
		if($stmt -> execute()){
			return  1;

		}else{
			return 2;
		}


			
	}
}


// 	public function createTable($username){

// $table = $username . "_orders_table";
// 		$stmtt = $this -> con -> prepare("CREATE TABLE IF NOT EXISTS $table (
//     id INT AUTO_INCREMENT,
//     title VARCHAR(255) NOT NULL,
//     orders LONGTEXT,
//     update_time TIMESTAMP,
//     day TEXT,
//     user text,
//     status text,
//     PRIMARY KEY (id)
// )  ENGINE=INNODB");

// 		$stmtt -> execute();

// 	}
	private function isUserExist($username,$email){
		$stmt = $this -> con -> prepare("SELECT id FROM users WHERE username = ? ");
		$stmt -> bind_param("s", $username);
		$stmt -> execute();
		$stmt ->  store_result();
		return $stmt -> num_rows > 0;
	}

		private function isUserExistReset($username,$email,$code){
		$stmt = $this -> con -> prepare("SELECT * FROM password_reset_request WHERE username = ? and email = ? and arial_code=?");
		$stmt -> bind_param("sss", $username, $email,$code);
		$stmt -> execute();
		$stmt ->  store_result();
		return $stmt -> num_rows > 0;
	}


	//user login

	public function userLogin($username , $password){
		$password = md5($password);
		$stmt = $this ->con -> prepare("SELECT id FROM users WHERE username = ?  AND password = ?");
		$stmt -> bind_param("ss", $username , $password);
		$stmt -> execute();
		$stmt -> store_result();
		return $stmt -> num_rows > 0;
	}

	public function getUserByUsername($username){
		$stmt = $this -> con -> prepare("SELECT * FROM users where username = ?");
		$stmt -> bind_param("s",$username);
		$stmt -> execute();
		return $stmt -> get_result() -> fetch_assoc();
	}

	public function getOrdersByUsername($username){
		$stmt = $this -> con -> prepare("SELECT id,orders,user,day,update_time from orders_made_tb where status ='N' and user='$username' ORDER BY ID,USER,DAY ASC");
		$stmt -> execute();
$stmt -> bind_result($id,$meals,$user,$day,$update_time);
$orderCall = array();

// traversing through all the result 
 while($stmt->fetch()){
 $temp = array();
 $temp['id'] = $id;
 $temp['orders'] = $meals;
 $temp['user'] = $user;
 $temp['day'] = $day;
 $temp['update_time'] = $update_time; 
 array_push($orderCall, $temp);
 }
//  // print_r($orderCall); echo "<br/><br/>";
//  //displaying the result in json format 
echo json_encode($orderCall);
		
		
	}
	
	

	//creating meals
	public function createMondayMenu($day,$meal1,$meal2){
	
		$stmt = $this -> con -> prepare("INSERT INTO monday_menu_tb (id,day,meal1,meal2) VALUES (NULL,?,?,?);");
		$stmt -> bind_param ("sss", $day,$meal1, $meal2);

		if($stmt -> execute()){
			return  1;
		}else{
			return 0;
		}
	}

	public function createTuesdayMenu($day,$meal1,$meal2){
	
		$stmt = $this -> con -> prepare("INSERT INTO tuesday_menu_tb (id,day,meal1,meal2) VALUES (NULL,?,?,?);");
		$stmt -> bind_param ("sss", $day,$meal1, $meal2);

		if($stmt -> execute()){
			return  1;
		}else{
			return 0;
		}
	}


	public function createWednesdayMenu($day,$meal1,$meal2){
	
		$stmt = $this -> con -> prepare("INSERT INTO wednesday_menu_tb (id,day,meal1,meal2) VALUES (NULL,?,?,?);");
		$stmt -> bind_param ("sss", $day,$meal1, $meal2);

		if($stmt -> execute()){
			return  1;
		}else{
			return 0;
		}
	}


	public function createThursdayMenu($day,$meal1,$meal2){
	
		$stmt = $this -> con -> prepare("INSERT INTO thursday_menu_tb (id,day,meal1,meal2) VALUES (NULL,?,?,?);");
		$stmt -> bind_param ("sss", $day,$meal1, $meal2);

		if($stmt -> execute()){
			return  1;
		}else{
			return 0;
		}
	}


	public function createFridayMenu($day,$meal1,$meal2){
	
		$stmt = $this -> con -> prepare("INSERT INTO friday_menu_tb (id,day,meal1,meal2) VALUES (NULL,?,?,?);");
		$stmt -> bind_param ("sss", $day,$meal1, $meal2);

		if($stmt -> execute()){
			return  1;
		}else{
			return 0;
		}
	}



	public function createSaturdayMenu($day,$meal1,$meal2){
	
		$stmt = $this -> con -> prepare("INSERT INTO saturday_menu_tb (id,day,meal1,meal2) VALUES (NULL,?,?,?);");
		$stmt -> bind_param ("sss", $day,$meal1, $meal2);

		if($stmt -> execute()){
			return  1;
		}else{
			return 0;
		}
	}


	public function createSundayMenu($day,$meal1,$meal2){
	
		$stmt = $this -> con -> prepare("INSERT INTO sunday_menu_tb (id,day,meal1,meal2) VALUES (NULL,?,?,?);");
		$stmt -> bind_param ("sss", $day,$meal1, $meal2);

		if($stmt -> execute()){
			return  1;
		}else{
			return 0;
		}
	}

/// DISPLAY ORDERS

	public function getMealList($day){
		$table = $day."_menu_tb";

		$stmt = $this -> con -> prepare("SELECT * FROM $table where day = ?");
		$stmt -> bind_param("s",$day);
		$stmt -> execute();
		return $stmt -> get_result() -> fetch_assoc();


	}




	public function createMondayOrder($day,$orders,$user,$status){
		if($this -> isEnteredAlready($day,$user,$status)){
				return 5;
		}else{
		$stmt = $this -> con -> prepare("INSERT INTO orders_made_tb (id,day,orders,user,status) VALUES (NULL,?,?,?,?);");
		$stmt -> bind_param ("ssss", $day,$orders, $user,$status);

		if($stmt -> execute()){
			return  1;
		}else{
			return 0;
		}
	}
}

	public function createTuesdayOrder($day,$orders,$user,$status){
		if($this -> isEnteredAlready($day,$user,$status)){
				return 5;
		}else{
		$stmt = $this -> con -> prepare("INSERT INTO orders_made_tb (id,day,orders,user,status) VALUES (NULL,?,?,?,?);");
		$stmt -> bind_param ("ssss", $day,$orders, $user,$status);

		if($stmt -> execute()){
			return  1;
		}else{
			return 0;
		}
	}
}


	
	public function createWednesdayOrder($day,$orders,$user,$status){
		if($this -> isEnteredAlready($day,$user,$status)){
				return 5;
		}else{
		$stmt = $this -> con -> prepare("INSERT INTO orders_made_tb (id,day,orders,user,status) VALUES (NULL,?,?,?,?);");
		$stmt -> bind_param ("ssss", $day,$orders, $user,$status);

		if($stmt -> execute()){
			return  1;
		}else{
			return 0;
		}
	}
}




	
	public function createThursdayOrder($day,$orders,$user,$status){
		if($this -> isEnteredAlready($day,$user,$status)){
				return 5;
		}else{
		$stmt = $this -> con -> prepare("INSERT INTO orders_made_tb (id,day,orders,user,status) VALUES (NULL,?,?,?,?);");
		$stmt -> bind_param ("ssss", $day,$orders, $user,$status);

		if($stmt -> execute()){
			return  1;
		}else{
			return 0;
		}
	}
}


	
	public function createFridayOrder($day,$orders,$user,$status){
		if($this -> isEnteredAlready($day,$user,$status)){
				return 5;
		}else{
		$stmt = $this -> con -> prepare("INSERT INTO orders_made_tb (id,day,orders,user,status) VALUES (NULL,?,?,?,?);");
		$stmt -> bind_param ("ssss", $day,$orders, $user,$status);

		if($stmt -> execute()){
			return  1;
		}else{
			return 0;
		}
	}
}





	public function createSaturdayOrder($day,$orders,$user,$status){
		if($this -> isEnteredAlready($day,$user,$status)){
				return 5;
		}else{
		$stmt = $this -> con -> prepare("INSERT INTO orders_made_tb (id,day,orders,user,status) VALUES (NULL,?,?,?,?);");
		$stmt -> bind_param ("ssss", $day,$orders, $user,$status);

		if($stmt -> execute()){
			return  1;
		}else{
			return 0;
		}
	}
}




	public function createSundayOrder($day,$orders,$user,$status){
		if($this -> isEnteredAlready($day,$user,$status)){
				return 5;
		}else{
		$stmt = $this -> con -> prepare("INSERT INTO orders_made_tb (id,day,orders,user,status) VALUES (NULL,?,?,?,?);");
		$stmt -> bind_param ("ssss", $day,$orders, $user,$status);

		if($stmt -> execute()){
			return  1;
		}else{
			return 0;
		}
	}
}

private function isEnteredAlready($day,$user,$status){
		$stmt = $this -> con -> prepare("SELECT day,user FROM orders_made_tb  WHERE day = ? OR user = ?");
		$stmt -> bind_param("sss", $day,$user,$status);
		$stmt -> execute();
		$stmt ->  store_result();
		return $stmt -> num_rows > 0;
	}




public function clearingOrders($status){
	
		$stmt = $this -> con -> prepare("UPDATE orders_made_tb SET status = ?;");
		$stmt -> bind_param ("s", $status);

		if($stmt -> execute()){
			return  1;
		}else{
			return 0;
		}
	}

public function passreset($username,$email){
	 if($this -> isUserExist($username, $email)){
				$status = "Y";
		$random_string = substr(str_shuffle(str_repeat("123456789abcdefghijklmnopqrstuvwxyz", 6)), 0, 6);
		$stmt = $this -> con -> prepare("INSERT INTO  password_reset_request (id,username,email,arial_code,status) VALUES (NULL, ?,?,?,?);");
		$stmt -> bind_param ("ssss", $username, $email,$random_string,$status);
		$message = "This is your reset code <br> <span style='font-weight:bold'> {$random_string} </span> </br> Kindly click on <a href='https://api.treenetlabs.com/android/vi/passthru(command)reset.php'>this link to Change your password </a> . Please ignore if password reset was not initiated by you.";
		
		if($stmt -> execute()){
		 mail('info@treenetlabs.com', 'Password Reset - HIT', $message);
			
		return  1;
		
	 	}else{
		 return 0;
			
	}


		}
	}

	public function checkuserreset($username,$email,$code,$password){
		if($this -> isUserExistReset($username, $email,$code)){
			$password = md5($password);
			$stmts = $this -> con -> prepare("UPDATE users SET password = ? WHERE username = ? and email=?;");
			$stmts -> bind_param ("sss", $password,$username,$email);
			
			if($stmts -> execute()){

			$stmt = $this -> con -> prepare("DELETE FROM password_reset_request WHERE username = ? and email=? ;");
		$stmt -> bind_param ("ss", $username,$email);
	

		if($stmt -> execute()){
			return  1;
		}else{
			return 0;
		}	

		}else{
			return 2;
		}
	 }
	}


}

?>