import { Component, inject, OnInit } from '@angular/core';
import { User } from '../models/user';
import { Book } from '../models/book';
import { BookService } from '../services/book.service';
import { FormsModule } from '@angular/forms';
import { DatePipe } from '@angular/common';

@Component({
  selector: 'app-user',
  standalone: true,
  imports: [FormsModule, DatePipe],
  templateUrl: './user.component.html',
  styleUrl: './user.component.css'
})
export class UserComponent implements OnInit {
  ngOnInit(): void {
    let u = localStorage.getItem("loggedUser")
    if(u){
      this.user = JSON.parse(u)
    }

    this.bookService.getAllBooks().subscribe(data=>{
      this.allBooks = data;
    })
  }

  user: User = new User()

  allBooks: Book[] = []

  private bookService = inject(BookService)

  likeAll(){
    for(let i=0; i<this.allBooks.length; i++){
      if(this.allBooks[i].toLike){
        this.bookService.likeBook(this.allBooks[i]).subscribe(data=>{
          this.ngOnInit()
        })
      }
    }
  }

  selectedBookName = ""

  getSelected(){
    alert(this.allSelected)
  }

  allSelected: [] = []

}
