import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { Component, OnInit } from '@angular/core';
import { IonHeader, IonToolbar, IonTitle, IonContent, IonList, IonItem, IonButton, IonBackButton, IonAlert, IonMenuToggle, IonSelect, IonSelectOption, IonLabel, IonTextarea, IonIcon, IonInput, IonCard } from '@ionic/angular/standalone';
import { ExploreContainerComponent } from '../explore-container/explore-container.component';
import { FormBuilder } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { CloudObject } from 'src/lib/ncloud';
import { AlertController } from '@ionic/angular';

@Component({
  selector: 'app - feedback',
  templateUrl: './feedback.component.html',
  styleUrls: ['./feedback.component.scss'],
  standalone: true,
  imports: [
    IonHeader, IonContent, IonToolbar, IonTitle, ExploreContainerComponent,
    IonButton, IonBackButton, IonMenuToggle, IonSelect, IonSelectOption, IonLabel,
    IonTextarea, IonIcon, IonInput, IonCard,
    IonList, IonItem, IonAlert,
    FormsModule,
    ReactiveFormsModule, CommonModule
  ]
})
export class FeedbackComponent implements OnInit {
  selectedCategory = '';
  feedbackText = '';
  categories = ['功能问题', '经济纠纷问题', '用户体验', '其他'];
  fileObject: File | null = null;
  userPointer: string | null = null;

  constructor(private fb: FormBuilder, private alertController: AlertController) { }

  onFileChange(event: Event) {
    // 实现文件选择逻辑，并更新fileObject
  }

  submitFeedback() {
    this.showSuccessAlert();
  }

  private showSuccessAlert() {
    this.alertController.create({
      header: '提交成功！',
      buttons: [{
        text: '确定',
        handler: () => {
          this.uploadDataToDatabase();
        }
      }]
    }).then(alert => alert.present());
  }

  private uploadDataToDatabase() {
    const feedback = new CloudObject('TravelFeedback');
    feedback.set({
      classfy: this.selectedCategory,
      problem: this.feedbackText,
      file: this.fileObject,
      username: this.userPointer
    });

    feedback.save().then(() => {
      console.log('Feedback submitted successfully');
      // 处理保存成功后的逻辑
    }).catch(error => {
      console.error('Error submitting feedback:', error);
      // 处理保存失败后的逻辑
    });
  }

  ngOnInit() { }
}