
import { Router } from '@angular/router';
import { Component, ElementRef, OnInit, ViewChild,ChangeDetectorRef } from '@angular/core';

import Swiper from 'swiper';
import Parse from 'parse';
import { FilmService } from 'src/modules/server/firm.serve';
import { BookService } from 'src/modules/server/book.serve';
import { MusicService } from 'src/modules/server/music.serve';
import { GameService } from 'src/modules/server/game.serve';
import { OutdoorService } from 'src/modules/server/outdoor.serve';

@Component({
  selector: 'app-tab1',
  templateUrl: 'tab1.page.html',
  styleUrls: ['tab1.page.scss']
})


export class Tab1Page implements OnInit {
 
  
  @ViewChild("slide1Comp") slide1Comp:ElementRef |undefined


  constructor(private cdr: ChangeDetectorRef,
    private filmService: FilmService,
    private router: Router,
    private bookService: BookService,
    private musicService: MusicService,
    private gameService: GameService,
    private outdoorService: OutdoorService,
  ) { }
  


  ngOnInit() {
    // 延迟500毫秒加载
    setTimeout(() => {
      this.initSwiper();
    }, 500);
  }

  // 初始化轮播图
  slide1:Swiper|undefined
  initSwiper(){
    console.log(this.slide1Comp)
    this.slide1 = new Swiper(this.slide1Comp?.nativeElement, {
      loop:true,
      autoplay:{

        delay:200,
      },
      mousewheel: {
        forceToAxis: true,
      },
      pagination:{
        el:".swiper-pagination",
        clickable:true
      }
    });
    setInterval(()=>{
      this.slide1?.slideNext();
    },1000)
  }

  

  filmsearchResults: any[] = [];
  booksearchResults: any[] = [];
  gamesearchResults: any[] = [];
  musicsearchResults: any[] = [];
  outdoorsearchResults: any[] = [];
  searchTerm: string = '';
  searchName: string = '';

  async search(){
    const queryParams = {
      name: this.searchTerm
      
    };
    
    this.filmsearchResults=await this.filmService.searchFilm(queryParams);
    this.booksearchResults=await this.bookService.getBooks(queryParams);
    this.musicsearchResults=await this.musicService.getMusics(queryParams);
    this.gamesearchResults=await this.gameService.getGames(queryParams);
    this.outdoorsearchResults=await this.outdoorService.getOutdoors(queryParams);
    
    const keywords = ['影', '视', '影片']; // 定义多个关键词
    if (keywords.some(keyword => this.searchTerm.toLowerCase().includes(keyword))) {
      this.router.navigate(['/feiwu/fiml']); // 如果包含任何一个关键词，则跳转到“影视”界面
    }
    const keywords2 = ['书', '小说']; // 定义多个关键词
    if (keywords2.some(keyword2 => this.searchTerm.toLowerCase().includes(keyword2))) {
      this.router.navigate(['/feiwu/book']); // 如果包含任何一个关键词，则跳转到“影视”界面
    }
    const keywords3 = ['音乐', '歌', '曲','听']; // 定义多个关键词
    if (keywords3.some(keyword3 => this.searchTerm.toLowerCase().includes(keyword3))) {
      this.router.navigate(['/feiwu/music']); // 如果包含任何一个关键词，则跳转到“影视”界面
    }
    const keywords4 = ['游戏', '玩']; // 定义多个关键词
    if (keywords4.some(keyword4 => this.searchTerm.toLowerCase().includes(keyword4))) {
      this.router.navigate(['/feiwu/game']); // 如果包含任何一个关键词，则跳转到“影视”界面
    }
    const keywords5 = ['运动', '户外', '锻炼','健']; // 定义多个关键词
    if (keywords5.some(keyword5=> this.searchTerm.toLowerCase().includes(keyword5))) {
      this.router.navigate(['/feiwu/outdoor']); // 如果包含任何一个关键词，则跳转到“影视”界面
    }



    
   if(!this.searchTerm)
    {
      this.filmsearchResults = []
      this.booksearchResults = []
      this.gamesearchResults = []
      this.musicsearchResults = []
      this.outdoorsearchResults =[] 
  }

  
  }



  // async search(keyword: string) {
  //   const Film = Parse.Object.extend('Film');
  //   const LQLBook = Parse.Object.extend('LQLBook');
  //   const LQLGame = Parse.Object.extend('LQLGame');
  //   const LQLMusic = Parse.Object.extend('LQLMusic');
  //   const LQLOutdoor = Parse.Object.extend('LQLOutdoor');

  //   const filmQuery = new Parse.Query(Film);
  //   const bookQuery = new Parse.Query(LQLBook);
  //   const gameQuery = new Parse.Query(LQLGame);
  //   const musicQuery = new Parse.Query(LQLMusic);
  //   const outdoorQuery = new Parse.Query(LQLOutdoor);

  //   filmQuery.startsWith('name', keyword);
  //   bookQuery.startsWith('name', keyword);
  //   gameQuery.startsWith('name', keyword);
  //   musicQuery.startsWith('name', keyword);
  //   outdoorQuery.startsWith('name', keyword);

  //   const filmresults = await filmQuery.find();
  //   const bookresults = await bookQuery.find();
  //   const gameresults = await gameQuery.find();
  //   const musicresults = await musicQuery.find();
  //   const outdoorresults = await outdoorQuery.find();

  //   this.filmsearchResults = filmresults.map(filmresult => filmresult.toJSON());
  //   this.booksearchResults = bookresults.map(bookresult => bookresult.toJSON());
  //   this.gamesearchResults = gameresults.map(gameresult => gameresult.toJSON());
  //   this.musicsearchResults = musicresults.map(musicresult => musicresult.toJSON());
  //   this.outdoorsearchResults = outdoorresults.map(outdoorresult => outdoorresult.toJSON());

  
  // if(!this.searchTerm)
  // {
  //   this.filmsearchResults = []
  //   this.booksearchResults = []
  //   this.gamesearchResults = []
  //   this.musicsearchResults = []
  //   this.outdoorsearchResults =[] 
  // }
  //   this.cdr.detectChanges();
  // }

}

function searchContent(event: Event | undefined, CustomEvent: { new <T>(type: string, eventInitDict?: CustomEventInit<T>): CustomEvent<T>; prototype: CustomEvent; }) {
  throw new Error('Function not implemented.');
}








 

