import { Component } from '@angular/core';
import { Camera, CameraResultType, CameraSource } from '@capacitor/camera';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
import { NavController } from '@ionic/angular';

@Component({
  selector: 'app-tab2',
  templateUrl: 'tab2.page.html',
  styleUrls: ['tab2.page.scss']
  
})
export class Tab2Page {
  selectedType: string;
  help: any = { title: '', content: '' };
  product: any = { name: '', amount: null, time: '', location: '' };
  event: any = { name: '', participants: '', startTime: '', duration: '', location: '', remarks: '' };
  lostFound: any = { itemName: '', status: '', location: '', time: '', remarks: '' };
  submitted: boolean = false;
  submittedData: any = null;
  productImage: File|undefined;
  lostFoundImage: File|undefined;
  productImageUrl: string|undefined;
  lostFoundImageUrl: string|undefined;

  setProductImageUrl(url: string) {
    this.productImageUrl = url;
  }

  // Example method to set lostFound image URL
  setLostFoundImageUrl(url: string) {
    this.lostFoundImageUrl = url;
  }
  // Function to handle product image selection
  onProductImageSelected(event: any) {
    this.productImage = event.target.files[0];
  }

  // Function to handle lostFound image selection
  onLostFoundImageSelected(event: any) {
    this.lostFoundImage = event.target.files[0];
  }
  constructor() {
    this.selectedType = 'help'; // 默认选择求助类型
  }

  onChangeActivityType() {
    // Reset form data when type changes
    this.clearFormData();
    this.submitted = false;
    this.submittedData = null;
  }
  
  submitForm() {
    // Save submitted data and display it
    this.submitted = true;
    switch (this.selectedType) {
      case 'help':
        this.submittedData = { ...this.help };
        break;
      case 'product':
        this.submittedData = { ...this.product };
        break;
      case 'event':
        this.submittedData = { ...this.event };
        break;
      case 'lostFound':
        this.submittedData = { ...this.lostFound };
        break;
      default:
        this.submittedData = null;
    }
    this.clearFormData();
  }

  clearFormData() {
    // Reset all form data
    this.help = { title: '', content: '' };
    this.product = { name: '', amount: null, time: '', location: '' };
    this.event = { name: '', participants: '', startTime: '', duration: '', location: '', remarks: '' };
    this.lostFound = { itemName: '', status: '', location: '', time: '', remarks: '' };
  }
  
}
