import { Component, Input, OnInit } from '@angular/core';
import { ChildActivationEnd, Router } from '@angular/router';
import { DemandeService } from 'src/app/services/demande/demande-service.service';
import { DemandesStageDTO } from 'src/gs-api/src';

@Component({
  selector: 'app-details-mvt',
  templateUrl: './details-mvt.component.html',
  styleUrls: ['./details-mvt.component.scss']
})
export class DetailsMvtComponent implements OnInit {
  selectedIdToDelete ? = -1;
  demande!:DemandesStageDTO
  constructor(private router:Router,  private demandeService: DemandeService) { }

  ngOnInit(): void {
    this.demande = this.demandeService.getDemande()!;
   
  }import { Injectable } from '@angular/core';
import { DemandesStageDTO } from 'src/gs-api/src';

@Injectable({
  providedIn: 'root'
})
export class DemandeService {
  demande: DemandesStageDTO | null = null;
  constructor() { }
  saveDemande(demande: DemandesStageDTO) {
    this.demande = demande;
  }

  getDemande(): DemandesStageDTO | null {
    return this.demande;
  }
}import { Component, EventEmitter, OnInit, Output } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { DemandeService } from 'src/app/services/demande/demande-service.service';
import { LinkedinAuthService } from 'src/app/services/linkeidn-auth/linkedin-auth.service';
import { DemandeAPIService, DemandesStageDTO, ProjetDeStageDTO, StagiaireDTO, UtilisateurDTO } from 'src/gs-api/src';

@Component({
  selector: 'app-form-application',
  templateUrl: './form-application.component.html',
  styleUrls: ['./form-application.component.scss']
})
export class FormApplicationComponent implements OnInit {
demande!:DemandesStageDTO;
 linkedInUser = {
    id: '',
    firstName: '',
    lastName: '',

  }


stagiaire:StagiaireDTO={
 id:10
}
  constructor( private demandeService: DemandeService,private linkedAuthService:LinkedinAuthService ,private router:Router,private route:ActivatedRoute, private demande_service: DemandeAPIService) { }
  projets!: ProjetDeStageDTO;
  selectedProjet: ProjetDeStageDTO | null = null;

  setSelectedProjet(projet: ProjetDeStageDTO): void {
    this.selectedProjet = projet;
  }
  ngOnInit(): void {
    this.route.queryParams.subscribe((params) => {
      console.log('StagiairesComponent ngOnInit', params);
      if (params['code']) {
        this.linkedAuthService.exchangeCodeForToken(params['code']);
      }
    });
    this.linkedInUser.id = localStorage.getItem('linkedInId')!;
    this.linkedInUser.firstName = localStorage.getItem('linkedInFirstName')!;
    this.linkedInUser.lastName = localStorage.getItem('linkedInLastName')!;
  //  this.linkedInUser.email = localStorage.getItem('linkedInEmail')!;
 console.log(this.linkedInUser.id)
    this.route.queryParamMap.subscribe((params) => {
      const projetNom = params.get('projetNom');
      const projetDescription = params.get('projetDescription');
      const projetId = params.get('projetId');

      console.log(projetId)
      this.selectedProjet = {
        id: parseInt(projetId!, 10),
        titre: projetNom!,
        description: projetDescription!
      };
      console.log(this.selectedProjet)
      this.stagiaire = {id:this.stagiaire.id};
        console.log(this.stagiaire)
        // Initialize the stagiaire object
      this.demande = { stagiaire: this.stagiaire, projet_stage: this.selectedProjet };
    });
  }

  cancelClick(): void {

    this.router.navigate(['offre']);

}
  enregistrerDemande(): void {
    const id = {

      stagiaireId: this.stagiaire.id,
      projetId: this.selectedProjet!.id
    };


    this.demande.id = id;
    console.log(this.demande.id)
    this.demande.stagiaire = this.stagiaire;

    this.stagiaire.nom = `${this.linkedInUser.firstName}`
    this.stagiaire.prenom=`${this.linkedInUser.lastName}`;
    console.log(this.stagiaire.nom)
  //  this.stagiaire.email = this.linkedInUser.email;
    if (this.selectedProjet !== null) {
      this.demande.projet_stage = this.selectedProjet;

    }
   this.demande_service.save10(this.demande).subscribe(demandes => {
    if (demandes.id !== null) { // add a null check for the id property
      this.demande.id = demandes.id;
      this.demandeService.saveDemande(this.demande);
      console.log('enregistrerDemande() method called successfully');

     // this.router.navigate(['offre']);

    } else {
      console.log("The id is null.");
    }
  }, error => {
    console.log(error)
  });}
navigate(){
  this.router.navigate(['offre']);
}
}

 why in console i have all attributes of demandeStage after enregistrerDemande are null or undefined on ngonit