import { Component, OnInit } from '@angular/core';
import { DataImporter } from '../import-book-data';
import { CloudObject, CloudQuery } from 'src/lib/ncloud';
import { IonCardSubtitle, NavController } from '@ionic/angular/standalone';
import { DatePipe } from '@angular/common';
import { 
  IonHeader, IonToolbar, IonTitle, IonContent, IonList, IonListHeader, 
  IonLabel, IonItem, IonIcon, IonCard, IonCardHeader, IonCardTitle, 
  IonCardContent, IonGrid, IonRow, IonCol, IonBadge, IonFab, IonFabButton,
  IonButtons, IonBackButton, IonButton, IonSearchbar
} from '@ionic/angular/standalone';
import { addIcons } from 'ionicons';
import { 
  documentTextOutline, documentOutline, bookOutline, cloudDownloadOutline,
  searchOutline, chevronForwardOutline
} from 'ionicons/icons';


@Component({
  selector: 'app-page-book-list',
  templateUrl: './page-book-list.component.html',
  styleUrls: ['./page-book-list.component.scss'],
  standalone: true,
  imports: [
    IonHeader, IonToolbar, IonTitle, IonContent, IonList, IonListHeader, 
    IonLabel, IonItem, IonIcon, IonCard, IonCardHeader, IonCardTitle, 
    IonCardContent, IonGrid, IonRow, IonCol, IonBadge, IonFab, IonFabButton,
    IonButtons, IonCardSubtitle, IonBackButton, IonButton, IonSearchbar,
    DatePipe
  ]})
export class PageBookListComponent  implements OnInit {

  // 分类颜色数组
  categoryColors = [
    '#3A5FE5', // 蓝色
    '#00C4A1', // 青色
    '#FF7043', // 橙色
    '#9C27B0', // 紫色
    '#FF4D4F', // 红色
    '#4CAF50', // 绿色
    '#607D8B', // 灰色
    '#FFC107'  // 黄色
  ];

  constructor(
    private navCtrl: NavController
  ) {
    addIcons({ 
      documentTextOutline, 
      documentOutline, 
      bookOutline, 
      cloudDownloadOutline,
      searchOutline,
      chevronForwardOutline
    });
  }

   ngOnInit() {
    this.loadBookData();
  }

  categorieList: Array<CloudObject> = [];
  documentList: Array<CloudObject> = [];
  knowledgeList: Array<CloudObject> = [];

  async loadBookData() {
    try {
      let query1 = new CloudQuery("DocumentCategory");
      this.categorieList = await query1.find();
      
      let query2 = new CloudQuery("LegalDocument");
      this.documentList = await query2.find();
      
      let query3 = new CloudQuery("LegalKnowledge");
      this.knowledgeList = await query3.find();
    } catch (error) {
      console.error('加载数据失败:', error);
    }
  }

  goBook(book: CloudObject) {
    this.navCtrl.navigateForward(['tabs', 'demo', 'book', 'display', book?.id]);
  }

  importData() {
    DataImporter.executeFullImport();
  }

  // 获取分类颜色
  getCategoryColor(index: number): string {
    return this.categoryColors[index % this.categoryColors.length];
  }

  // 根据文档类型获取颜色
  getDocTypeColor(docType: string): string {
    const typeMap: Record<string, string> = {
      '合同': 'secondary',
      '诉讼': 'danger',
      '协议': 'success',
      '通知': 'warning',
      '申请': 'tertiary'
    };
    return typeMap[docType] || 'primary';
  }

  // 查看文书详情
  viewDocument(document: CloudObject) {
    this.navCtrl.navigateForward(['/document-detail', document.id]);
  }

  // 查看法律知识详情
  viewKnowledge(knowledge: CloudObject) {
    this.navCtrl.navigateForward(['/knowledge-detail', knowledge.id]);
  }

}
