<?php
	require_once("../../session_check.php");
	require_once("../../../classes/main_classes.php");

	//OBJECTS
	$insertObj=new INSERT();
	$updateObj=new UPDATE();
	$retrieveObj=new RETRIEVE();


	if(isset($_POST['update_profile_note'])){
		if(empty($_POST['firstname'])){
			die("<p class='alert alert-warning'>Enter firstname</p>");
		}else if(empty($_POST['lastname'])){
			die("<p class='alert alert-warning'>Enter lastname</p>");
		}else if(empty($_POST['username'])){
			die("<p class='alert alert-warning'>Enter username</p>");
		}else if(empty($_POST['email'])){
			die("<p class='alert alert-warning'>Enter email</p>");
		}
		$firstname=addslashes($_POST['firstname']);
		$lastname=addslashes($_POST['lastname']);
		$username=addslashes($_POST['username']);
		$email=addslashes($_POST['email']);

		$update=$updateObj->updater("users","email='$email',firstname='$firstname',lastname='$lastname',username='$username'","user_id",$user_id);
		if($update==1){
			echo 1;
		}else{
			echo 0;
		}
	}

	//UPDATE PROFILE PICTURE
	if(isset($_FILES['updateProfileImage'])){
		if(!(empty($_FILES["updateProfileImage"]["name"]))){
			$imageName=$_FILES["updateProfileImage"]["name"];
			$imageType=$_FILES["updateProfileImage"]["type"];
			$substr_extension="";
			$type=$_FILES["updateProfileImage"]["type"];


			function isimage(){  
				global $type;
				$extensions=array('image/jpg','image/jpe','image/jpeg','image/jfif','image/png','image/bmp','image/dib','image/gif');
				    if(in_array($type, $extensions)){
				        return true;
				    }
				    else
				    {
				        return false;
				    }
				    
				}

				    if(isimage()){
				    	$size='';
				    	$getSize=$_FILES['updateProfileImage']['size'];
						if($getSize!=null)
						{
						    $tmpDest=$_FILES['updateProfileImage']['tmp_name'];
						    $size=$_FILES['updateProfileImage']['size'];
						    if($size<10000000){
						    	$substr_extension=(string)(substr($type, 6));

								$query=$updateObj->updater("users","photo='photo$user_id.$substr_extension'","user_id",$user_id);

								//check if directory exist
								if (!file_exists("../../../profile_photoes/")) {
								    mkdir("../../../profile_photoes/", 0777,true);
								    chmod("../../../profile_photoes/", 0777);
								} 
								

								$pathto="../../../profile_photoes/photo$user_id.$substr_extension";
    							
								if($query and move_uploaded_file($_FILES['updateProfileImage']['tmp_name'],$pathto)){
									echo "success";
								}else{
									
									//echo $_FILES["updateProfileImage"]["error"];
								}
						    }else{
						    	
								echo "large";
						    }
						}  
				    }else{

				    }				
		}else{
			echo 'select_image';
		}
	}
	if(isset($_POST['refleshProfilePic'])){
		$row=$retrieveObj->retriever("users","*","user_id",$user_id,"fetch");
		//$row=(array)$row;

		echo '<img height="150px" src="../profile_photoes/'.$row->photo.'">';
	}
	if(isset($_POST['refleshSubProfilePic'])){
		$row=$retrieveObj->retriever("users","*","user_id",$user_id,"fetch");
		//$row=(array)$row;

		echo '<img class="rounded-circle shadow" height="30px" src="../profile_photoes/'.$row->photo.'">';
	}

	if(isset($_POST["change_password"])){
		//Update password
		if(isset($_POST['Cpassword'])){
			$Cpassword=$_POST["Cpassword"];
			$Npassword=$_POST["Npassword"];
			$Rpassword=$_POST["Rpassword"];

			$hashPassword=$retrieveObj->retriever("users","password","user_id",$user_id,"fetch");
			$hashPassword=$hashPassword->password;

			
			if(empty($Cpassword) or empty($Npassword) or empty($Rpassword)){
				echo "<p class='alert alert-warning text-center p-2 text-light'> Fill all the fields to continue</p>";
			}else{
				$Cpassword=$_POST["Cpassword"];
				
				
				if(password_verify($Cpassword,$hashPassword)){
					if(password_verify($Npassword,$hashPassword)){
						die("<p class='alert alert-warning p-2 text-light'>Password not changed, New and Current password are equal</p>");
					}

					//
					if($Npassword==$Rpassword){
						$Npassword=password_hash($_POST["Npassword"],PASSWORD_DEFAULT);
						$changePass=$updateObj->updater("users","password='$Npassword'","user_id",$user_id);
						if($changePass==1){
							echo "<p class='alert alert-success text-center p-2 text-light'> password changed successfully!</p>";
						}else{
							echo "<p class='alert alert-danger text-center p-2 text-light'> Failed to change password, try again later</p>";
						}
						
					}else{
						echo "<p class='alert alert-warning text-center p-2 text-light'> passwords do not match!</p>";
					}

				}else{
					echo "<p class='alert alert-danger text-center p-2 text-light'> Your current password is wrong!</p>";

				}
			}
		}	
	}
?>