import { Component, OnInit } from '@angular/core';
import { DataService } from '../data.service';
import { Title } from '@angular/platform-browser';

@Component({
  selector: 'app-tab13',
  templateUrl: './tab13.page.html',
  styleUrls: ['./tab13.page.scss'],
})
export class Tab13Page implements OnInit {

  replyContent: string = " ";
  lostFoundItems: any[] = [];

  constructor(private dataService: DataService,private titleService: Title) {}

  ngOnInit() {
    this.lostFoundItems = this.dataService.getLostFoundItems();
    // 假设helpItems是从DataService中获取的求助信息数组，确保数据已经被加载
    if (this.lostFoundItems.length > 0) {
      this.titleService.setTitle(this.lostFoundItems[0].title);
    }
  }

  submitReply(item: any) {
    if (this.replyContent.trim() !== '') {
      const reply = { content: this.replyContent, sender:"小序", floor: "" }; // 可以根据实际情况设置回复人,初始化楼层号
      this.dataService.addReplyToLostFoundItems(item.id, reply);
      this.replyContent = ''; // 清空回复框
    }
  }

  onFileChange(event: any, item: any) {
    const file = event.target.files[0];
    const reader = new FileReader();
    reader.onload = (e: any) => {
      item.image = e.target.result;
    };
    reader.readAsDataURL(file);
  }

}
