import { Component, OnInit} from '@angular/core';
import { Router } from '@angular/router';
import { LocalStorageService } from 'angular-2-local-storage';
// import { NavBarService } from '../ta/ta.service';
import { NavBarService } from './admin.service';
import { Location } from '@angular/common';
import { AuthService } from '../services/auth.service';
import { environment } from '../../environments/environment.dev';

const adminPages = [
  { title: 'Welcome to Admin', back: '' },
  { title: 'Capability Assessment Configuration', back: 'arrow_back' },
  { title: 'Sandbox Configuration', back: 'arrow_back' },
  { title: 'User Management', back: 'arrow_back' },
  { title: 'Add - User Management', back: 'arrow_back' },
  { title: 'Details - User Management', back: 'arrow_back' },
  { title: 'Roles', back: 'arrow_back' },
  { title: 'Candidate Transfer', back: 'arrow_back' },
  { title: 'Location', back: 'arrow_back' },
  { title: 'Coach Nomination', back: 'arrow_back' },
  { title: 'Skill Sets', back: 'arrow_back' },
  { title: 'Location', back: 'arrow_back' },
  { title: 'Skill Sets', back: 'arrow_back' },
  { title: 'CC Index', back: 'arrow_back'},
  { title: 'Innovation Focal', back: 'arrow_back'}
];

@Component({
  selector: 'app-admin',
  templateUrl: './admin.component.html',
  styleUrls: ['./admin.component.css'],
  providers: [NavBarService]
})
export class AdminComponent implements OnInit {
  currentPageNumber: number;
  roleStatus: boolean;
  currentPageData: { title: string, back: string};
  backLink = '/admin';
  empData: {
    accessToken: any,
    loginType: any,
    fullName: any,
    firstName: any,
    userInfoMstrId: any,
    roleName: any,
    roles: any,
    skills: any,
    location: any
  };
  initials = '';
  aldata: any;
  onlyOneRole: boolean;
  frmCandD:boolean = false;
  imageUrl: string = environment.imageUrl;
  constructor(
    protected localStorageService: LocalStorageService,
    private navBarService: NavBarService,
    private router: Router,
    private location: Location) {
      // this.currentPageData = { title: 'Welcome to Admin', back: 'arrow_back' };
      // this.currentPageData = adminPages[this.currentPageNumber];
    if (window.location.origin !== 'http://localhost:8080') {
      var empData;
      empData = this.localStorageService.get('userInfo');
      this.empData = empData;
      if (empData['roles'].length > 1) {
        this.onlyOneRole = false;
      } else {
        this.onlyOneRole = true;
      }      
      console.log(this.empData,"userInfo Detailssssss")
      if (this.empData['fullName']) {
        this.initials = this.empData['fullName'].replace(/\W*(\w)\w*/g, '$1').toUpperCase();
      }
    } else {
        this.initials = 'LS';
    }
    console.log('initials: ' + this.initials);
    navBarService.currentPage.subscribe((val: number) => {
      this.currentPageNumber = val;
      this.currentPageData = adminPages[this.currentPageNumber];
      // console.log('admin component -- ',this.currentPageNumber);
      // this.currentPageData = { title: 'Welcome to Admin', back: 'arrow_back' };
      // this.currentPageData = adminPages[0];
      if (this.currentPageNumber === 12) {
        this.backLink = '/admin/skills';
        this.currentPageData = adminPages[this.currentPageNumber];
      } else {
          this.backLink = '/admin';
          this.currentPageData = { title: 'Welcome to Admin', back: 'arrow_back' };
      }
      //  this.backLink = this.currentPageData.backLink || '/admin'; 
      console.log('getting page in parent: ' + val);
    });
  }

  ngOnInit() {
    if(this.aldata){
      console.log(JSON.stringify(this.aldata));
    }
  }

  navigateBack() {
    this.location.back();
    this.currentPageData = null;
  }
  changeRole(status){
    this.roleStatus = status;
  }
  closeNavDetails(event){
    this.roleStatus = event;
  }     

  openFeedModal(e){
    console.log(e);
    if(e == true){
      this.frmCandD = e
    }
  }

  closeFdModal(e){
    if(e == false){
      this.frmCandD = e;
    }
  }
}
