import { Component, OnInit } from '@angular/core';
import { NavController, LoadingController } from '@ionic/angular';
import { ActivatedRoute } from '@angular/router';
import { EnvService } from 'src/app/services/env.service';
import { AuthService } from 'src/app/services/auth.service';
import { IchambaService } from 'src/app/services/ichamba.service';
import { TranslateService } from '@ngx-translate/core';
import { LanguageService } from 'src/app/services/language.service';
import { AlertService } from 'src/app/services/alert.service';

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

  private loading: HTMLIonLoadingElement | null = null;
  contract_id: number | null = null;
  comment: any;

  constructor(
    private authService: AuthService,
    private navCtrl: NavController,
    private alertService: AlertService,
    private ichambaService: IchambaService,
    private translateService: TranslateService,
    private languageService: LanguageService,
    private activatedRoute: ActivatedRoute,
    private loadingCtrl: LoadingController,
  ) { }

  ngOnInit() {
      this.contract_id = Number(this.activatedRoute.snapshot.paramMap.get('contract_id'));
  }

  reportservice() {
      if (this.comment) {
        this.loadingCtrl.create().then((overlay) => {
          this.loading = overlay;
          this.loading.present();
        });
        this.ichambaService.reportContract(this.contract_id!, this.comment).subscribe(
          (data: any) => {
            if (this.loading) this.loading.dismiss();
            this.alertService.presentToast(this.translateService.instant('alerts.report_send'));
            this.navCtrl.navigateRoot('/dashboard');
          }, (error: any) => {
            if (this.loading) this.loading.dismiss();
            this.alertService.presentToast("Por favor contacte a soporte técnico, Estatus:" + error['status']);
          });
      } else {
        this.alertService.presentToast(this.translateService.instant('alerts.report_nocomment'));
      }
  }

}
