import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { CommonModule } from '@angular/common';
import { IonicModule } from '@ionic/angular';
import { forkJoin } from 'rxjs';



@Component({
  selector: 'app-poem-detail',
  templateUrl: './poem-detail.page.html',
  styleUrls: ['./poem-detail.page.scss'],
  standalone: true,
  imports: [CommonModule, IonicModule]
})
export class PoemDetailPage implements OnInit {
  bookId: number | null = null; // 初始化为 null
  poemId: number | null = null; // 初始化为 null
  poemContent: string | null = null; // 初始化为 null
  poemTitle: string | null = null;
  title: string | null = null;
  private baseUrl = 'http://localhost:3000/api'; // 后端 API 基础 URL

  constructor(private route: ActivatedRoute, private http: HttpClient, private router: Router) {
  }


  ngOnInit() {
   // 订阅路由参数，以获取传递的诗词ID和书籍ID
   this.route.params.subscribe(params => {
    this.poemId = +params['id']; // 获取传递的诗词ID
    if (this.poemId) {
      // this.getPoemContent(this.poemId); // 获取诗词内容
      // this.getPoemTitle(this.poemId);
      this.loadPoemDetails(this.poemId);
    }

    // 获取传递的书籍ID（假设书籍ID通过其他方式传递）
    const bookIdParam = this.route.snapshot.queryParamMap.get('bookId');
    // const bookIdParam = params['bookId'];
    if (bookIdParam) {
      this.bookId = +bookIdParam;
    }
  });
  }


  loadPoemDetails(poemId: number) {
    // 合并诗词标题和内容的请求
    forkJoin([
      this.http.get<{ id: number; book_id: number; author_id: number; genre_id: number; title: string }>(`${this.baseUrl}/poems/${poemId}`),
      this.http.get<{ content: string }>(`${this.baseUrl}/poem-contents/${poemId}`)
    ]).subscribe(
      ([poemData, content]) => {
        console.log('Received poem data:', poemData); // 添加日志
        console.log('Received content:', content); // 添加日志
        this.poemTitle = poemData.title ;
        this.title = this.poemTitle; // 设置页面标题
        this.poemContent = content.content || '加载失败或无内容';
      },
      error => {
        console.error('Error fetching poem details', error);
      }
    );
  }

    // 返回逻辑
    goBack() {
      if (this.bookId !== null) {
        this.router.navigate(['book-management', this.bookId]); // 导航到对应类型的书籍管理页面
      } else {
        console.error('Book ID is not set');
      }
    }
  }

  
  // loadPoemDetails(poemId: number) {
  //   // 获取诗词标题
  //   const id = poemId;
  //   this.http.get<{ title: string }>(`${this.baseUrl}/poems/${id}`).subscribe(
  //     data => {
  //       this.poemTitle = data.title || '未知标题'; // 设置默认值以防为空
  //       this.title = this.poemTitle; // 设置页面标题
  //     },
  //     error => {
  //       console.error('Error fetching poem title', error);
  //     }
  //   );

  //   // 获取诗词内容
  //   this.http.get<{ content: string }>(`${this.baseUrl}/poem-contents/${poemId}`).subscribe(
  //     data => {
  //       this.poemContent = data.content || '加载失败或无内容'; // 设置默认值以防为空
  //     },
  //     error => {
  //       console.error('Error fetching poem content', error);
  //     }
  //   );
  // }


  // getPoemTitle(poemId: number) {
  //   // const id = poemId;
  //   this.http.get<{ title: string }>(`${this.baseUrl}/poems/${poemId}`).subscribe(data => {
  //     this.poemTitle = data.title; // 保存诗词标题
  //   }, error => {
  //     console.error('Error fetching poem title', error);
  //   });
  // }
  // // 获取诗词内容
  // getPoemContent(poemId: number) {
  //   this.http.get<{ content: string }>(`${this.baseUrl}/poem-contents/${poemId}`).subscribe(data => {
  //     this.poemContent = data.content; // 保存诗词内容
  //   }, error => {
  //     console.error('Error fetching poem content', error);
  //   });
  // }



