import { Component } from '@angular/core';
import { User } from './models/user';
import { UserService } from './services/user.service';
import { Router } from '@angular/router';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';

  constructor(private servis: UserService, private router: Router){}

  allUsers: User[] = []

  getAllUsers(){
    this.servis.getAllUsers().subscribe(
      data=>{
        this.allUsers = data;
      }
    )

  }

  username: string = "";
  password: string = "";
  message: string = "";

  login(){
    this.servis.login(this.username, this.password).subscribe(
      data=>{
        if(data==null){
          this.message = "error"
        }
        else{
          this.message = ""
          localStorage.setItem("logged", JSON.stringify(data))
          if(data.type=="kupac"){
            //this.router.navigate(['user', {param1: "hello"}])
            this.router.navigate(['user'])
          }
          else{
            this.router.navigate(['admin'])
          }
        }
      }
    )
  }
}
