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

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

  userId: number;
  subscriptionReportData: Subscription;
  reportData = [];
  pages = [];
  userLoginId: any;
  serviceId: any;

  constructor(private userService: UserService, 
    private authService: AuthServiceService,
    private spinner: NgxSpinnerService, 
    private authSer: AuthServiceService,
    private route: ActivatedRoute) { }

  ngOnInit() {
    this.authSer.showPrintBtn = false;
    this.authSer.showDashboardHeader = true;
    this.authSer.internalHeader = 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;

    this.spinner.show();
    this.userLoginId = localStorage.getItem('userId');
    this.serviceId = localStorage.getItem('serviceId');
    this.userService.getPagesPermetiotns(this.userLoginId, this.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_and_print_report'){
                this.authSer.showPrintBtn = true;
              }
            }
          }else {
            console.log('no users');
          }
        }
      },
      (error) => {
        console.log(error);
      }
    );
    //get profile data
    this.userService.getUserDataProfile();
    this.route.params.subscribe(
      (params:Params) => {
        this.userId = params['reportUserId'];
      }
    );

   this.subscriptionReportData = this.userService.onGetReportData(this.userId).subscribe(
      (responce) => {
        console.log(responce);
        this.reportData.push(responce['data']);
        console.log(this.reportData);
        this.spinner.hide();
      },
      (error) => {
        console.log(error);
      }
    )

  };

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

  onPrint(){
    window.print();
  }

}
