import { CommonModule } from '@angular/common';
import { Component, Input, OnInit } from '@angular/core';
import { ActivatedRoute, ParamMap } from '@angular/router';
import { IonCard, IonContent, IonLabel, NavController } from '@ionic/angular/standalone';
import { addIcons } from 'ionicons';
import { arrowBackOutline } from 'ionicons/icons';
import { result } from 'lodash';
import { map, switchMap } from 'rxjs';
import { CustomHeaderComponent } from 'src/lib/component/custom-header/custom-header.component';
import { CloudObject, CloudQuery } from 'src/lib/ncloud';
addIcons({ arrowBackOutline });
@Component({
  selector: 'app-generate-result',
  templateUrl: './generate-result.component.html',
  styleUrls: ['./generate-result.component.scss'],
  standalone: true,
  imports: [IonContent, CustomHeaderComponent, IonCard, IonLabel, CommonModule],
})

export class GenerateResultComponent implements OnInit {
  @Input() objectId: string | null = '';
  history: CloudObject | undefined;
  jsonData: Record<string, any> = {};
  keys: Array<string> = [];
  constructor(private navCtrl: NavController, private route: ActivatedRoute) { }
  ngOnInit() {
    this.route.paramMap.pipe(
      switchMap((params: ParamMap) => this.route.queryParamMap.pipe(
        map(() => params.get('resultId'))
      ))
    ).subscribe(resultId => {
      this.objectId = resultId;
      // 现在您可以使用 this.objectId
    });
    console.log("正在加载历史记录：" + this.objectId)
    this.getGenerateResult();
  }
  goBack() {
    this.navCtrl.back();
  }
  async getGenerateResult() {
    let query = new CloudQuery('GenerateResult');
    query.equalTo('objectId', this.objectId);
    let result = await query.find();
    this.history = result[0];
    console.log(this.history);
    this.jsonData = JSON.parse(this.history?.get('content'));
    console.log(this.jsonData);
    this.keys = Object.keys(this.jsonData['schemeList']);
    console.log(this.keys);
  }
}
