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-services',
  templateUrl: './services.component.html',
  styleUrls: ['./sercices.component.css']
})

export class ServicesComponent implements OnInit {

  idUser: number; //id user
  myInnerHeight = window.innerHeight;
  services = [];
  pages = [];
  userType: number = null; //visitor or employee

  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
      this.authService.checkExternalFooter = false;
      this.authService.arabicTemplate = true; //to make footer is arabic all time
      //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();
      this.userType = parseInt(localStorage.getItem('userType')); 

      //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('servicesss',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 || dataService.id == 2) {
      //خدمه اداره المحتوي + خدمه اداره الصفحه الخارجيه
      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 || this.pages[0].id == 30) {
            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 || this.pages[0].id == 31) {
            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 || this.pages[0] == 19) {
            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.pages[0].id == 20) {
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/newsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 16 || this.pages[0].id == 29) {
            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.pages[0].id == 18) {
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/mainMenuList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 21) {
            console.log('/service/' + this.idUser + '/' + dataService.id + '/barEventsList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/barEventsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 22) {
            console.log('/service/' + this.idUser + '/' + dataService.id + '/advertisingList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/advertisingList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 32) {
            console.log('/service/' + this.idUser + '/' + dataService.id + '/contactUsList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/contactUsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 23) {
            console.log('/service/' + this.idUser + '/' + dataService.id + '/visitingTimesList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/visitingTimesList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 25) {
            console.log('/service/' + this.idUser + '/' + dataService.id + '/eventCalendarsList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/eventCalendarsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 27) {
            console.log('/service/' + this.idUser + '/' + dataService.id + '/externalAchievementsList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/externalAchievementsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 26) {
            console.log('/service/' + this.idUser + '/' + dataService.id + '/statisticsList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/statisticsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 28) {
            console.log('/service/' + this.idUser + '/' + dataService.id + '/map/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/map/' + this.pages[0].id]);
          }
          this.spinner.hide();
        },
        (error) => {console.log(error)}
      );
    }  else if(dataService.id == 3 && this.userType == 0) {
      
      this.userservice.getPagesPermetiotns(this.idUser, dataService.id).subscribe(
        (responce) => {
          console.log(responce);
          this.pages = responce['pages'];
          if(this.pages[0].id == 33) {
            console.log('/service/' + this.idUser + '/' + dataService.id + '/supervisorList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/supervisorList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 34) {
            console.log('/service/' + this.idUser + '/' + dataService.id + '/absorpationList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/absorpationList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 35) {
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/reviewTrainerData/' + this.pages[0].id]);
          } else if(this.pages[0].id == 36) {
            this.router.navigate(['service/' + this.idUser + '/' + dataService.id + '/reviewSectionForTrainee/' + this.pages[0].id ]);
          } else if(this.pages[0].id == 37) {
            this.router.navigate(['service/' + this.idUser + '/' + dataService.id + '/printCardTraineeList/' + this.pages[0].id ]);
          } else if(this.pages[0].id == 38){
            this.router.navigate(['service/' + this.idUser + '/' + dataService.id + '/registrationTraineeList/' + this.pages[0].id ]);
          } else if(this.pages[0].id == 39) {
            this.router.navigate(['service/' + this.idUser + '/' + dataService.id + '/finalAccerditationList/' + this.pages[0].id ]);
          } else if(this.pages[0].id == 40) {
            console.log('trainnngg data !');
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/trainnerList/' + this.pages[0].id]);
          }
        },
        (error) => {
          console.log(error);
        }
      );
      
     } else if(dataService.id == 3 && this.userType == 1) {
       //visitor life cycle
       console.log('trainingServices/' + this.idUser + '/' + dataService.id);
       this.router.navigate(['trainingServices/' + this.idUser + '/' + dataService.id]);
     } else if(dataService.id == 4) {
       //خدمه الحركه
       this.userservice.getPagesPermetiotns(this.idUser, dataService.id).subscribe(
        (responce) => {
          console.log(responce);
          this.pages = responce['pages'];
          if(this.pages[0].id == 43) {
            console.log('/service/' + this.idUser + '/' + dataService.id + '/vehicleList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/vehicleList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 44) {
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/vehicleModelList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 45) {
            this.router.navigate(['/service/' + this.idUser + '/' + dataService.id + '/registrationVehicleList/' + this.pages[0].id]);
          }
        },
        (error) => {
          console.log(error);
        }
      );
     }
  }


}
