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

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

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

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

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

  submitReply(item: any) {
    if (this.replyContent.trim() !== '') {
      const reply = { content: this.replyContent, sender: '小序', floor: 1 }; // 可以根据实际情况设置回复人,初始化楼层号
      this.dataService.addReplyToTrade(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);
  }

}
