import { Component } from '@angular/core';
import { NavMenuService } from './nav-menu.service';
import { User } from '../../masters/user/user';
import { UseraddService } from '../../masters/useradd/useradd.service';
import { DomSanitizer } from '@angular/platform-browser';


@Component({
  selector: 'app-nav-menu',
  templateUrl: './nav-menu.component.html',
  styleUrls: ['./nav-menu.component.css']
})
export class NavMenuComponent {
  isExpanded = false;
  // local storage variables
  public userid = localStorage.getItem('userid');
  imagepath: any;
  user: User;
  users: User[];
  searchText: string;
  public username = localStorage.getItem('username');

  // common variables
  list: any = [];
  constructor(
    private navmenuservice: NavMenuService,
    private useraddservice: UseraddService,
    private _sanitizer: DomSanitizer
    ) { }

    ngOnInit() {
      debugger;
    this.navmenuservice.getRoleMappedMenu(+this.userid).subscribe(res => {
      if (res !== undefined && res != null && res.length > 0) {
        res.forEach(i => {
          if (i !== undefined) {
            this.list.push(JSON.parse(i));
          }
        });
      }

else{
  debugger;
  this.toggle();
}

    });

    this.useraddservice.GetById(+this.userid).subscribe(res => {
      debugger;
      this.users = JSON.parse(res[0].j);
      this.user = this.users.find(u => u.userid === +this.userid);
      // this.user = res;
      if (this.user.profileimagepath != null) {
        debugger;
        this.useraddservice.GetImage(this.user.profileimagepath).subscribe( res1 => {
          if (res1.type !== 'text/plain') {
            const objectURL = 'data:image/jpeg;base64,' + res1.image;
            this.imagepath = this._sanitizer.bypassSecurityTrustUrl(objectURL);
          }
        }, error => { });
      } else {
        this.imagepath = 'assets/images/img.jpg';
      }
    }, error => { });
  }

  collapse() {
    this.isExpanded = false;
  }

  toggle() {
    this.isExpanded = !this.isExpanded;
  }
}
