import { Component, OnInit } from '@angular/core';
import { NavController } from '@ionic/angular';
import { Router } from '@angular/router';
import { DataService } from './data.service';

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

  publishedItems: any[] = [
    { category: '求助',
      route: '/tab11', 
      title: '求助！高数',
      headImage:"/assets/images/start.png",
      publisher: '小田' ,
      timestamp:new Date('2022-05-06T10:30:00')
    },
    { category: '二手交易', 
      route: '/tab12', 
      title: 'ipad9二手出售', 
      headImage:"/assets/images/play.png", 
      publisher: '燕燕',
      timestamp:new Date('2022-03-16'),
      amount:'1000'
    },
    { category: '失物招领', 
      route: '/tab13', 
      title: '李小珊学生证', 
      headImage:"/assets/images/rgb.png", 
      publisher: '小万' ,
      timestamp:new Date('2022-08-06T09:31:25')
    },
    { category: '活动', 
      route: '/tab14', 
      title: '英语六级讲座', 
      headImage:"/assets/images/black.png", 
      publisher: '耶耶' ,
      timestamp:new Date('2022-03-01T22:13:21')
    }
  ];

  displayedItems: any[] = [];
  selectedCategory: string = '求助'; // 默认选中“求助”

  constructor(private dataService: DataService, private router: Router) {
    //this.displayedItems = this.publishedItems;// 默认展示所有已发布信息
    this.showPublishedItems(this.selectedCategory);// 默认展示“求助”类别的已发布信息
  }

  navigateToDetail(item: any) {
    // 在这里处理跳转到详情页面的逻辑，可以传递 item 信息到详情页面
    //console.log('跳转到详情页面', item);
    this.router.navigate([item.route], { state: { title: item.title, publisher: item.publisher , image:item.image} });
  }

  showPublishedItems(category: string) {
    // 根据点击的顶部导航按钮，展示相应类型的已发布信息
    this.displayedItems = this.publishedItems.filter(item => item.category === category);
    this.selectedCategory = category; // 更新选中的类别
  }


}
