import { NgxSpinnerService } from 'ngx-spinner';
import { HttpClient } from '@angular/common/http';
import { AuthServiceService } from './../../../shared/auth-service.service';
import { Component, OnInit } from '@angular/core';
import { ToastrService } from 'ngx-toastr';

@Component({
  selector: 'app-report-current-employee-officer',
  templateUrl: './report-current-employee-officer.component.html',
  styleUrls: ['./report-current-employee-officer.component.css']
})
export class ReportCurrentEmployeeOfficerComponent implements OnInit {

  constructor(private authSer: AuthServiceService, 
    private toastr: ToastrService,
    private spinner: NgxSpinnerService,
    private http: HttpClient) { }

  adminstrations = [];
  departments = [];
  adminstraion_officers = [];
  department_officers = [];
  adminId: number = null;
  departId: number = null;
  defaultAdminstrationValue: string = ' ';
  defaultdepartmentValue: string = '';

  ngOnInit() {

    this.spinner.show();

    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', '61');


    this.http.get(this.authSer.pathApi + '/page_list/3/1/1000/all').subscribe(
      (responce) => {
        this.adminstrations = responce['adminstrations'];
        this.spinner.hide();
      },
      (error) => {
        console.log(error);
      }
    )
  }


  getAdminstrationsData(event) {
    this.adminId = event.target.value;
    console.log(this.adminId);
    this.getDepartments(this.adminId);
  }

  getDepartmentsData(event) {
    this.departId = event.target.value;
    console.log(this.departId);
  }

  getDepartments(adminId) {
    this.http.get(this.authSer.pathApi + '/page_list/4/1/1000/' + adminId).subscribe(
      (response) => {
        console.log(response);
        this.departments = response['departments'];
      },
      (error) => {
        console.log(error);
      }
    )
  }


  getReport() {
    
    if(this.adminId == null) {
      this.toastr.warning('قم بإختيار إدراه اولا !'); 
    } else {
      this.http.get(this.authSer.pathApi + '/report_current_employees_officer/' + this.adminId + '/' + (this.departId ? this.departId : 'all') ).subscribe(
        (response) => {
          console.log(response);
          this.adminstraion_officers = response['adminstraion_officers'];
          this.department_officers = response['department_officers'];
        },
        (error) => {
          console.log(error);
        }
      )
    }
  }

  //print function 
  onPrint() {
    window.print();
  }

}
