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

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

export class ReportListComponent implements OnInit {

  constructor(private route: ActivatedRoute,
    private userSer: UserService,
    private router: Router,
    private authSer: AuthServiceService,
    private toastr: ToastrService,
    private reportService:ReportService,
    private spinner: NgxSpinnerService,
    private http: HttpClient) { }

  reportType: string;
  permissionsTable:boolean = false;
  pagesTable:boolean = false;
  name:string = '';
  usersList = [];
  rolesList= [];
  roleValue:number;
  serviceId: number;
  userLoginId:number;
 
  typeRoleReport:string //to determine the type 0f report of role

  ngOnInit() {

    this.userSer.getUserDataProfile();
    //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.route.parent.params.subscribe(
      (params:Params) => {
          this.userLoginId = params['userID'];
          this.serviceId = params['serviceID'];
        }
      );

    this.route.params.subscribe(
      (params: Params) => {
        this.reportType = params['reportName'];
        if(this.reportType == 'reportPermissions') {
          this.permissionsTable = true;
        } else if(this.reportType == 'reportPages') {
          this.typeRoleReport = 'reportPages';
          this.spinner.show();
          this.pagesTable = true;
          this.http.get(this.authSer.pathApi + '/roles_list').subscribe(
            (responce) => {
              console.log(responce);
              this.rolesList = responce['roles'];
              console.log(this.rolesList);
              this.spinner.hide();
            }
          )
        } else if(this.reportType == "reportUsers") {
          this.spinner.show();
          this.typeRoleReport = 'reportUsers';
          this.pagesTable = true;
          this.http.get(this.authSer.pathApi + '/roles_list').subscribe(
            (responce) => {
              console.log(responce);
              this.rolesList = responce['roles'];
              console.log(this.rolesList);
              this.spinner.hide();
            }
          );
        }
      }
    );
    
  }




  onSearchName() {
    this.spinner.show();
    console.log(this.name);
    this.http.get(this.authSer.pathApi + '/find_user/' + this.name).subscribe(
      (responce) => {
        console.log(responce);
        if(responce['users'].length == 0) {
          this.toastr.warning('لا يوجد مستخدمين بهذا الاسم');
        } else {
          this.usersList = responce['users'];
          console.log(this.usersList);
        }

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

  onGetRoles(roleVal) {
    console.log(roleVal);
    this.roleValue = roleVal;
  }

  //on get user  report
  onGetReport(user_id) {
    this.router.navigate(['userreport/' + user_id]);
  }

  getRoleReport() {
    if(this.typeRoleReport == 'reportPages') {
      this.router.navigate(['service/' + this.userLoginId + '/' + this.serviceId + '/roleReport/' + this.roleValue]);
    } else if(this.typeRoleReport == 'reportUsers') {
      this.router.navigate(['service/' + this.userLoginId + '/' + this.serviceId + '/userInRoleReport/' + this.roleValue]);
    }
  }
}
