import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import * as Parse from 'parse';

interface Item {
  name: string;
  description: string;
  imgUrl: string;
 detailId: string;
}

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
  items: Item[] = [];
  currentContent: string = 'module-one';

  constructor(private router: Router) {}

  ngOnInit() {
    this.getData();
  }

  getData() {
    (Parse as any).serverURL = 'https://web2023.fmode.cn/parse';
    Parse.initialize('dev');

    let query: Parse.Query;
    if (this.currentContent === 'module-one') {
      query = new Parse.Query('LcmModuleOne');
    } else if (this.currentContent === 'module-two') {
      query = new Parse.Query('LcmModuleTwo');
    } else if (this.currentContent === 'module-three') {
      query = new Parse.Query('LcmModuleThree');
    } else {
      return; // 如果没有匹配的模块，退出方法
    }

    query.find()
      .then((results) => {
        this.items = results.map((result) => ({
          name: result.get('name'),
          description: result.get('description'),
          imgUrl: result.get('imgUrl'),
         detailId: result.get('detailId')
        }));
        console.log(this.items);
      })
      .catch((error) => {
        console.error('Error retrieving data from Parse:', error);
      });
  }

  selectItem(item: Item) {
    localStorage.setItem('selectedItem', JSON.stringify(item)); // 存储选定的项目到localStorage
    console.log(this.selectItem);
    this.router.navigate(['/page-item-detail', item.detailId]); // 导航到详情页，并传递选定的项目ID作为参数
  console.log(this.router.navigate)
  }

  showContent(content: string) {
    this.currentContent = content;
    this.getData();
  }
}
