import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { BaseComponent } from '../base/base.component';
import { AuthenticationService } from '../_services/authentication.service';
import { UserService } from '../_services';

@Component({
    moduleId: module.id.toString(),
    templateUrl: 'admin.component.html',
    styleUrls: ['admin.component.scss']
})

export class AdminComponent extends BaseComponent implements OnInit {
    public user: any;
    public userData: any;

    // Toggle Menu
    public _toggleMenu = false;

    constructor(
        private auth: AuthenticationService,
        private userService: UserService,
        private router: Router
    ) {
        super();
    }

    ngOnInit() {
        this.user = localStorage.getItem('user');
        this.getUserData();
    }

    getUserData(){
        this.userService.getByEmail(this.user).subscribe((user: any) => {
            this.userData = user;
        }, err => this.handleError(err));
    }

    logout() {
        this.auth.logout();
        this.router.navigate(['/login']);
    }

    toggleMenu($event: any) {
        this._toggleMenu = !this._toggleMenu;
    }
    
    private handleError(error: any): Promise<any> {
        this.auth.logout();
        this.router.navigate(['/login']);
        return Promise.reject(error.message || error);
    }

}