import { HttpClient } from '@angular/common/http';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { AuthServiceService } from './../../shared/auth-service.service';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { UserService } from '../../shared/user.service';
import { Subscription } from 'rxjs';
import { NgxSpinnerService } from 'ngx-spinner';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit, OnDestroy {

  idUser: number; //id user
  myInnerHeight = window.innerHeight;
  services = [];
  pages = [];

  subscriptionUSer: Subscription;
  constructor(private authService: AuthServiceService, 
    private userservice: UserService, 
    private route: ActivatedRoute, 
    private http: HttpClient, private router: Router,
    private spinner: NgxSpinnerService) { }

  ngOnInit() {
    this.spinner.show();//spinner
    //show / hide notification search in header
    this.authService.notificationLogin = true;
    this.authService.showSearchHeader = false;
    this.authService.showHeaderLogin = false;
    this.authService.showHeaderDashBoard = true;
    this.authService.showDashboardHeader = true;
    this.authService.internalHeader = false;

    console.log(this.authService.isAuthenticated());
    //get profile data
    this.userservice.getUserDataProfile();
    
    //catch id user
    this.subscriptionUSer =  this.route.params.subscribe(
        (params: Params) => {
          this.idUser = params['id'];
        }
      );

      //get rules of users
      this.userservice.getServicesData(this.idUser).subscribe(
        (responce) => { 
          this.services = responce['services'];
          console.log(this.services);
          this.spinner.hide();
        },
        (error) => {console.log(error)}
      );
  }

  getDataService(dataService){

    console.log('dataServiccce', dataService);
    console.log(dataService.id);
    console.log(this.idUser);

    if(dataService.id == 1) {
      //خدمه الصلاحيات
      this.userservice.getPagesPermetiotns(this.idUser, dataService.id).subscribe(
        (responce) => {
          console.log(responce);
        this.pages = responce['pages'];
        //this.pages[0].active = 1;
          console.log(this.pages);
          if(this.pages[0].id == 1) {
            console.log('/service/' + this.idUser + '/' + dataService.id + '/users/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/users/' + this.pages[0].id]);
          }else if(this.pages[0].id == 2) {
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/roles/' + this.pages[0].id]);
          } else if(this.pages[0].id == 3) {
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/departments/' + this.pages[0].id]);
          } else if(this.pages[0].id == 4) {
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/sections/' + this.pages[0].id]);
          } else if(this.pages[0].id == 5) {
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/reports/' + this.pages[0].id]);
          }
          this.spinner.hide();
        },
        (error) => {console.log(error)}
      );

    } else if(dataService.id == 6) {
      //خدمه اداره المحتوي
      this.userservice.getPagesPermetiotns(this.idUser, dataService.id).subscribe(
        (responce) => {
          console.log(responce);
          this.pages = responce['pages'];
          console.log(this.pages);
          if(this.pages[0].id == 6) {
            console.log('/service/' + this.idUser + '/' + dataService.id + '/hospitalsList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/hospitalsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 10) {
            console.log('/service/' + this.idUser + '/' + dataService.id + '/mangementList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/mangementList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 8){
            console.log('/service/' + this.idUser + '/' + dataService.id + '/eventsList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/eventsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 9) {
            console.log('/service/' + this.idUser + '/' + dataService.id + '/joinUs/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/joinUs/' + this.pages[0].id]);
          } else if(this.pages[0].id == 7) {
            console.log('/service/' + this.idUser + '/' + dataService.id + '/externalsList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/externalsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 11) {
            console.log('/service/' + this.idUser + '/' + dataService.id + '/achievementsList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/achievementsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 12) {
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/tabs/' + this.pages[0].id]);
          } else if(this.pages[0].id == 13) {
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/newsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 16) {
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/footerList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 14) {
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/internalServicesList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 15){
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/lecturesList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 17) {
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/mainMenuList/' + this.pages[0].id]);
          }
          this.spinner.hide();
        },
        (error) => {console.log(error)}
      );
    }
   
   
  }

  ngOnDestroy() {
    this.subscriptionUSer.unsubscribe();
  }

}
