import { Component, HostListener  ,TemplateRef,ViewChild} from '@angular/core';
import { AuthService } from './auth.service';
import { Router } from '@angular/router';
import { LogoutService } from './logout/logout.service';
import { ToastrService } from 'ngx-toastr';
import {  BsModalService, BsModalRef, ModalOptions } from 'ngx-bootstrap/modal';
import { environment } from '../environments/environment';
import { log } from 'console';
import { IdleService } from './idle.service';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  title = 'app';
  private uniqueIdentifier: string;
  @ViewChild("pendinglotinfoTemplate", { static: true })
  pendinglotinfoTemplate: TemplateRef<any>;
  config: ModalOptions = {
    backdrop: "static",
    keyboard: false,
    animated: true,
    class: "modal-dialog-centered",
    ignoreBackdropClick: true,
  };
  pendinglotinfoModalRef: BsModalRef;
  pendinglot:number=0;
  assignlot:number=0;
  userid = localStorage.getItem('userid');
  baseurl: string = environment.serviceUrl;
  modalIsOpen: boolean = false;
  intervalID;
  /**Tab close start**/
  showIdleModal: boolean = false;
  constructor( private modalService: BsModalService,
    private toastr: ToastrService,
    private logoutservices: LogoutService,private idleService: IdleService, private router: Router) {

    /*   this.idleService.onIdleTimeout.subscribe(() => {
        //console.log('Idle timeout event received in component.');
        if(localStorage.getItem("logid") &&  !this.modalIsOpen){
          this.show_pendinglotd();
        }     
      }); */
  }
  /* ngOnInit() {
   // this.idleService.startMonitoring();
  }   */
/*   @HostListener('document:click', ['$event'])
  handleMouseClick(event: MouseEvent) {
    const currentUrl = this.router.url;
     if (currentUrl === '/login') {
      //console.log('Currently on the login page.');
      return; // Don't navigate if on the login page
    }else{
      if(!localStorage.getItem("logid")){
     // if (!localStorage.getItem("sessionexpire") && !sessionStorage.getItem("sessionexpire")) {
        this.router.navigate(['/logout']); // Replace with your actual logout page route
      }
    }

  } */

  closeppop() {
  
    this.logoutservices.PendingLotInfo(this.userid).subscribe(res => {
      if (res.status === 200) {
        this.showIdleModal = false;
        localStorage.setItem('idle', 'false');
        if (this.pendinglotinfoModalRef) {
          this.modalIsOpen = false;
          this.pendinglotinfoModalRef.hide();
        }
        this.router.navigate(['/logout']);
      }
    });

  }
  nopop() {
    debugger;
    this.showIdleModal = false;
    localStorage.setItem('idle', 'false');
    if (this.pendinglotinfoModalRef) {
      this.modalIsOpen = false;
      this.pendinglotinfoModalRef.hide();
    }
  }
  show_pendinglotd(){

   this.userid = localStorage.getItem('userid');

    this.logoutservices.GetUsersPendinginfo(this.userid).subscribe(res => {
      if (res.status === 200) {
        //console.log(JSON.parse(res.body[0])[0].assignlot);
        //console.log(JSON.parse(res.body[0])[0].pendinglot);
        this.pendinglot=JSON.parse(res.body[0])[0].pendinglot;
        this.assignlot=JSON.parse(res.body[0])[0].assignlot;
        this.pendinglotinfoModalRef = this.modalService.show(this.pendinglotinfoTemplate, this.config);
       const aa = document.getElementsByClassName("modal-content");
        aa[0].classList.remove("modal-content");

        
          this.modalIsOpen = true;
      }
    }, err => {
      this.toastr.error('Error while geting user info');
    });

  }
  /**Tab close end**/
  /**Hold functionality start**/
 /* private isPageRefresh = true;
  constructor(private authService: AuthService,
    private logoutservices: LogoutService,
    private route: Router) {
  }
  @HostListener('window:unload', ['$event'])
  unloadHandler(event: Event) {
   if( localStorage.getItem('signoff_status') == '1' ){
   if (!this.isPageRefresh) {
     let pageReloaded = window.performance
           .getEntriesByType("navigation")
           .map((nav) => (nav as any).type)
           .includes("reload");
     const LoggedIn = localStorage.getItem("LoggedIn");
           const logid = localStorage.getItem("logid");
           const userid = localStorage.getItem("userid");
           //localStorage.setItem('signoff_status','0');
           if (LoggedIn == null) {
            
             localStorage.clear();
             sessionStorage.clear();
             this.route.navigate(["/login"]);
           } else {
             this.logoutservices.logout(logid,userid).toPromise().then(
               (res) => {
                 localStorage.setItem('signoff_status','0');
                   localStorage.removeItem("LoggedIn");
                   localStorage.clear();
                   sessionStorage.clear();
                   localStorage.removeItem("LoggedIn");
               },
               (error) => {
               }
             );

   }
 }
 }
}*/
 /**Hold functionality end**/ 
}


