import { Component, OnInit } from "@angular/core";
import { UserService } from "../../shared/user.service";
import { Router } from "@angular/router";
import { NgForm } from "@angular/forms";

@Component({
  selector: "app-navbarlogout",
  templateUrl: "./navbarlogout.component.html",
  styleUrls: ["./navbarlogout.component.css"]
})
export class NavbarlogoutComponent implements OnInit {
  isCollapsedD = false;
  isCollapsedM = false;
  isCollapsedS = false;
  userDetails;
  currentURL;
  constructor(public userService: UserService, private router: Router) {
    this.isCollapsedM = true;
    this.isCollapsedS = true;
    this.currentURL = this.router.url;
  }

  //This function gets called when the page loads
  ngOnInit() {
    this.userService.getUserProfile().subscribe(
      res => {
        this.userDetails = res["user"];
      },
      err => {}
    );
  }

  //This function allows the user to logout from their profile page
  //Only used in the profile components
  onLogout() {
    this.userService.deleteToken();
    this.router.navigate(["/login"]);
  }

  /**
   *
   * This function gets called when a user submits a search
   * It will redirect to the displayresults page with the search query entered
   */
  openSearchPage(form: NgForm) {
    window.location.href =
      "./displayresults?search=" + form.value.search + "&page=1";
  }
}
