import { ReportService } from './../../shared/report.service';
import { AuthServiceService } from './../../shared/auth-service.service';
import { ActivatedRoute, Router, Params } from '@angular/router';
import { Component, OnInit } from '@angular/core';
import { UserService } from '../../shared/user.service';
import { NgxSpinnerService } from 'ngx-spinner';

@Component({
  selector: 'app-reports-page',
  templateUrl: './reports-page.component.html',
  styleUrls: ['./reports-page.component.css']
})

export class ReportsPageComponent implements OnInit {

  constructor(private route: ActivatedRoute,
      private router:Router,
      private reportService: ReportService,
      private userService: UserService,
      private spineer: NgxSpinnerService,
      private authSer: AuthServiceService) { }
  
  pages = [];
  userLoginId: number;
  serviceId: number;

  ngOnInit() {

    this.spineer.show();
    this.authSer.showReportBtn = false;
    //show / hide notification search in header
    this.authSer.notificationLogin = true;
    this.authSer.showSearchHeader = false;
    this.authSer.showHeaderLogin = false;
    this.authSer.showHeaderDashBoard = true;
    this.authSer.showDashboardHeader = true;
    this.authSer.internalHeader = false;
    localStorage.setItem('pageIdActive', '5');
    this.route.parent.params.subscribe(
      (params: Params) => {
        this.reportService.userId = params['userID'];
        this.userLoginId = params['userID'];
        localStorage.setItem('userId', this.reportService.userId);
        this.reportService.serviceId = params['serviceID'];
        this.serviceId = params['serviceID'];
        localStorage.setItem('serviceId', this.reportService.serviceId);
      }
    );

    this.userService.getPagesPermetiotns(this.reportService.userId, this.reportService.serviceId).subscribe(
      (responce)=>{
        this.pages = responce['pages'];
        console.log(this.pages);            
        for(let i = 0; i< this.pages.length; i++) {
          if(this.pages[i].name == 'التقارير') {
            for(let j = 0; j < this.pages[i].permissions.length; j++) {
              if(this.pages[i].permissions[j].name == 'show_report'){
                this.authSer.showReportBtn = true;
              }
            }
          }else {
            console.log('no users');
          }
        }

        this.spineer.hide();
      },
      (error) => {
        console.log(error);
      }
    );
  }

  userReport() {
    console.log('service/' + this.userLoginId + '/' + this.serviceId + '/reportList/reportPermissions');
    this.router.navigate(['/service/' + this.userLoginId + '/' + this.serviceId + '/reportList/reportPermissions']);
  }

  userInRolesReport() {
    console.log('service/' + this.userLoginId + '/' + this.serviceId + '/reportList/reportPages');
    this.router.navigate(['/service/' + this.userLoginId + '/' + this.serviceId + '/reportList/reportPages']);
  }

  permissionInUsersReport() {
    console.log('service/' + this.userLoginId + '/' + this.serviceId + '/reportList/reportUsers');
    this.router.navigate(['/service/' + this.userLoginId + '/' + this.serviceId + '/reportList/reportUsers']);
  }

}
