import { Component } from '@angular/core';

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

  uploadedImage: File | null = null;
  static imageUrl: string | null = null;

  constructor() { }

  handleFileInput(event: Event) {
    const target = event.target as HTMLInputElement;
    if (target.files && target.files.length > 0) {
      this.uploadedImage = target.files[0];
      this.previewImage();
    }
  }
  // 在组件中调用 CameraPagePage 类的静态方法获取 imageUrl 的值
  getImageUrl(): string | null {
    return CameraPagePage.imageUrl;
  }
  previewImage() {
    if (this.uploadedImage) {
      const reader = new FileReader();
      reader.readAsDataURL(this.uploadedImage);
      reader.onload = () => {
        CameraPagePage.imageUrl = reader.result as string;
      };
    }
  }

  submitForm() {
    if (this.uploadedImage) {
      const formData = new FormData();
      formData.append('file', this.uploadedImage);
      
      // 使用后端服务将文件上传到指定目录
      // 这里假设有一个名为 uploadImage 的后端服务可以接收文件并保存到指定目录
      // 你可以调用后端API来实现保存图片的功能
      // 例如：this.http.post('http://localhost:3000/uploadImage', formData).subscribe(response => { console.log(response); });
    }
  }

}