import {Component, OnInit, ViewChild} from '@angular/core';
import {VehiculeService} from "../services/vehicule.service";
import {ActivatedRoute} from "@angular/router";
import {EntretienService} from "../services/entretien.service";
import {SinistreService} from "../services/sinistre.service";
import {PretvehiculeService} from "../services/pretvehicule.service";
import {MatDialog} from "@angular/material/dialog";
import {OptionsService} from "../services/options.service";
import {KeycloakService} from "keycloak-angular";
import {TrajetService} from "../services/trajet.service";
import {MatPaginator} from "@angular/material/paginator";
import {DriverService} from "../services/driver.service";
import {ToastrService} from "ngx-toastr";
import {FormBuilder, FormControl, FormGroup, Validators} from "@angular/forms";

@Component({
  selector: 'app-vehiculesdetails',
  templateUrl: './vehiculesdetails.component.html',
  styleUrls: ['./vehiculesdetails.component.scss'],
})
export class VehiculesdetailsComponent implements OnInit {
  @ViewChild(MatPaginator) paginator!: MatPaginator;
  id = this.route.snapshot.params['id'];
  vehicle: any;
  entretiens: any;
  prets: any;
  vehicules: any;
  sinistres: any;
  pretInfosConducteur: any
  options: any;
  username: string | undefined;
  drivers: any;
  trajets: any;
  email: any;
  isAuthenticated: boolean = false;
  submitted: boolean = false;
  vehicles: any;
  driverdetail: any;
  sinistresDisplayedColumns: string[] = ['Type', 'Date', 'Description', 'montant'];
  optionsDisplayedColumns: string[] = ['Type'];
  trajetsDisplayedColumns: string[] = ['Adresse_Depart', 'Adresse_Arrivee', 'Km_depart', 'Km_arrivee', 'Prix_carburant', "type_carburant", "Date_depart", "Date_arrivee", "Statut"];
  entretiensDisplayedColumns: string[] = ['Type', 'Date', 'Description', 'Montant', 'Statut'];
  pretsDisplayedColumns: string[] = ['NameDriver', 'DateDebut', 'DateFin', 'Statut'];
  vehicleForm: FormGroup = this.fb.group({
    Id: new FormControl('', [Validators.required]),
    IdConducteur: new FormControl('', [Validators.required]),
  });



  constructor(
                  private toastr: ToastrService, private keycloackService: KeycloakService, public vehiculeService: VehiculeService, public driverService: DriverService, public trajetService: TrajetService, private route: ActivatedRoute, public entretienService: EntretienService, public sinistreService: SinistreService, public pretVehiculeService: PretvehiculeService, public optionsService: OptionsService,
                  public dialog: MatDialog, public fb: FormBuilder) {

  }


  ngOnInit() {
    this.GetDetailVehicule();
    this.GetDetailEntretien();
    this.GetDetailSinistre();
    this.GetInfosConducteurPret();
    this.GetInfosGeneralPret();
    this.GetdetailsOption();
    this.GetDetailTrajet();
    this.GetDetailDriver();
    this.GetAllVehicles();
    this.GetAllDrivers();
    //this.nameuserkeycloak();
  }

  /*nameuserkeycloak() {
    this.keycloackService.isLoggedIn().then(isAuthenticated => {
      this.isAuthenticated = isAuthenticated;
      this.keycloackService.loadUserProfile().then(async (userProfile) => {
        this.username = userProfile.username;
      });
    });
  }*/

  GetDetailTrajet() {
    this.trajetService.getDetailsTrajet(this.id).subscribe((res: any) => {
      this.trajets = res;
      console.log(this.trajets);
    })
  }


  GetDetailVehicule() {
    this.vehiculeService.detailsVehicle(this.id).subscribe((res: any) => {
      this.vehicules = [res];
      console.log(this.vehicules);
    })
  }

  GetAllDrivers() {
    this.driverService.getDrivers().subscribe((res: any) => {
      this.drivers = res;
      console.log(this.drivers);
    })
  }


  GetDetailEntretien() {
    this.entretienService.getEntretien(this.id).subscribe((res: any) => {
      this.entretiens = [res];
      console.log(this.entretiens);
    })
  }

  GetDetailSinistre() {
    this.sinistreService.getDetailsSinistre(this.id).subscribe((res: any) => {
      this.sinistres = [res];
      console.log(this.sinistres);
    })
  }

  GetInfosConducteurPret() {
    this.pretVehiculeService.getDetailsPretVehicule(this.id).subscribe((res: any) => {
      this.pretInfosConducteur = [res];
      console.log(this.pretInfosConducteur);
    })
  }

  GetInfosGeneralPret() {
    this.pretVehiculeService.getDetailsPretVehicule(this.id).subscribe((res: any) => {
      this.prets = [res];
      console.log(this.prets);
    })

  }

  GetdetailsOption() {
    this.optionsService.detailOptions(this.id).subscribe((res: any) => {
      this.options = res;
      console.log(this.options);
    })
  }

  GetAllVehicles() {
    this.vehiculeService.getVehicles().subscribe((res: any) => {
      this.vehicles = res;
      console.log(this.vehicles);
    })
  }

  GetDetailDriver() {
    this.vehiculeService.getDetailVehicle(this.id).subscribe((data: any) => {
      this.driverdetail = data;
      console.log(this.driverdetail);
    })
  }


  onSubmit() {
    this.vehicleForm.setValue({
      Id: this.vehicleForm.value.Id,
      IdConducteur: this.vehicleForm.value.IdConducteur
    });
    this.vehiculeService.putVehicle(this.vehicleForm.value).subscribe(
      {
        next: () => {
          this.toastr.success('Conducteur modifié avec succès');
          this.resetForm();
        },
        error: () => {
          this.toastr.error('Erreur lors de la modification du conducteur');
        }
      }
    );
  }
  private resetForm() {
    this.submitted = false;
    this.vehicleForm.reset();
    this.vehicleForm.setValue({
      Id: '',
      IdConducteur: ''
    });
  }
}

 avec ce code je veux ajouter l'option pour ajouter un IdConducteur si il n'y est pas créer