import { Router } from '@angular/router';
import { Component, OnInit, inject } from '@angular/core';

import { BreadcrumbService } from '../breadcrumb.service';
import { LogoutChildComponent } from './logoutChild.component';
import { NgbModal, NgbModalOptions } from '@ng-bootstrap/ng-bootstrap';

@Component({
  template: '',
  standalone: true,
  selector: 'app-logout',
})
export class LogoutComponent implements OnInit
{
  private readonly router = inject(Router);
  private readonly modalService = inject(NgbModal);

  public submitted: boolean = false;

  constructor()
  {
    const breadcrumbService = inject(BreadcrumbService);
    breadcrumbService.setItems([{ label: 'Logout', routerLink: ['/logout'] }]);
  }

  ngOnInit()
  {
    const modalOptions: NgbModalOptions =
    {
      centered: true,
      backdrop: 'static',
      windowClass: 'misc-width',
      backdropClass: 'customBackdrop'
    };

    const modalRef = this.modalService.open(LogoutChildComponent, modalOptions);
    modalRef.result.then((resp) =>
    {
      if (resp && resp === true)
      {
        this.router.navigate(['/login']);
      }
      else
      {
        this.router.navigate(['/dashboard']);
      }
    },
      () =>
      {
        this.router.navigate(['/dashboard']);
      }
    );
  }
}
