import { Component, Input } from '@angular/core';
import { UserDomainService } from '../user-domain-details/service/user-domain.service';
import { ActivatedRoute, Router } from '@angular/router';
import { lastValueFrom } from 'rxjs';
import { HttpStatusCode } from '@angular/common/http';
import { DomainService } from '../rgnt-domain/service/domain.service';

@Component({
    selector: 'app-payment-submission',
    templateUrl: './payment-submission.component.html',
    styleUrls: ['./payment-submission.component.css'],
    standalone: false
})
export class PaymentSubmissionComponent {

  userMailId: string = localStorage.getItem('email');
    currentOnboardingDomain: any = null;
   @Input() isLoading: boolean = false; 
    paramValue: number | null = null;
    isLoadingData:boolean;

  
    async ngOnInit() {
       const paramValue = this.acRoute.snapshot.queryParamMap.get('domainId'); // Extract query param
    ////console.log('Query Param:', paramValue);

    if (paramValue && !isNaN(+paramValue)) {  // Ensure it's a number
      this.paramValue = +paramValue;  // Convert to number
      ////console.log('Parsed Query Param:', this.paramValue);
    } else {
      ////console.log('Invalid or missing query param');
    }
   await  this.getDomainApplicationDetails(this.paramValue);
    }
  
    constructor(private domainService: UserDomainService,
      private router: Router,private acRoute: ActivatedRoute,
      private usrDomainService: DomainService
      
    ) { 
      // Constructor
    }
  
    async getDomainApplicationDetails(domainId: number) {
      this.isLoadingData = true;
      await lastValueFrom(this.usrDomainService.getDomainByDomainId(domainId)).then(
        (response) => {
         this.currentOnboardingDomain = response.body;
         this.isLoadingData = false;
         ////console.log(this.currentOnboardingDomain)
        },
        error => {
          if(error.status === HttpStatusCode.Unauthorized){
            this.navigateToSessionTimeout();
          }
        }
      );  
  
    }
  
    navigateToSessionTimeout() {
      this.router.navigate(['/session-timeout']);
    }

    currentDate: Date = new Date();
    
    trackApplication() {
      this.router.navigate(['/applications']);
    }

}
