import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AuthService } from '../auth/services/auth.service';
import { ModuleSystem } from '../models/module.model';

@Component({
  selector: 'app-menu-ecosistema',
  templateUrl: './menu-ecosistema.component.html',
  styleUrls: ['./menu-ecosistema.component.scss']
})
export class MenuEcosistemaComponent implements OnInit {

  modules: ModuleSystem[];
  moduleSale: boolean = true;
  modulePartner: boolean = true;
  moduleAdmin = true;

  constructor(
    private readonly router: Router,
    private readonly authService: AuthService) { }



  ngOnInit(): void {
    this.authService.getModules(this.authService.getUser())
      .subscribe(m => {
        this.modules = m;
        this.modulesUser();
    });
  }

  modulesUser() {
    console.log(JSON.stringify(this.modules));
    let module = this.modules.find(m => m.moduleId == 1) ;
    if ( module === null ){
      this.moduleSale = false;
    } else {
      console.log(JSON.stringify(module));
      this.moduleSale = module.access == "S";
    }
    module = this.modules.find(m => m.moduleId == 2) ;
    if ( module === undefined ){
      this.modulePartner = false;
    } else {
      console.log(JSON.stringify(module));
      this.modulePartner = module.access == "S";
    }
    module = this.modules.find(m => m.moduleId == 3) ;
    this.moduleAdmin = false;
    if ( module === undefined || module == null ){
      this.moduleAdmin = false;
    } else {
      console.log(JSON.stringify(module));
      this.moduleAdmin = module.access == "S";
    }
  }

  openModule(url: string) {
    this.router.navigate([url]);
    // window.open(url, '_blank');
  }

}
