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

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

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

    vehicle_types: any[] =[];
    vehicle_models: any[]=[];
    vehiclesData: any[] = [];
    vehicleTypeId: number;
    vehicleModelId:number;
    showTableData:boolean = false;

  ngOnInit() {
    
    localStorage.setItem('pageIdActive', '48');


    //get vehicles types 
    this.http.get(this.authSer.pathApi + '/get_vehicle_types_list').subscribe(
      (response) => {
        console.log(response);
        this.vehicle_types = response['types'];
        
      },
      (error) => {
        console.log(error);
      });

  }


  //change type vehicle 
  typeVehicle(event) {
    console.log(event.target.value);
    this.vehicleTypeId = event.target.value;
    this.http.get(this.authSer.pathApi + '/get_vehicle_models_by_type_id/' + this.vehicleTypeId).subscribe(
      (responce) => {
        console.log('models vehicle', responce);
        this.vehicle_models = responce['models'];
        if(this.vehicle_models.length == 0) {
          this.toastr.warning('لا يوجد موديلات لهذا النوع حاليا ');
        }
      },
      (error) => {
        console.log(error);
      }
    );
  };

  //get report when change model
  getReport(event) {
    this.spinner.show();
    console.log(event.target.value);
    this.vehicleModelId = event.target.value;
    this.http.get(this.authSer.pathApi + '/report_existing_vehicles/' + this.vehicleTypeId + '/' + this.vehicleModelId + '/all').subscribe(
      (responce) => {
        console.log('report data ', responce);
        this.vehiclesData = responce['vehicles'];
        if(this.vehiclesData.length > 0) {
          this.showTableData = true;
        } else {
          this.showTableData = false;
        }
        this.spinner.hide();
      },
      (error) => {
        console.log(error);
      }
    );
  }

  //fillter data 
  getFillter(event) {
    console.log(event.target.value);
    this.spinner.show();
    this.http.get(this.authSer.pathApi 
      + '/report_existing_vehicles/' 
      + this.vehicleTypeId + '/' 
      + this.vehicleModelId 
      + '/' + event.target.value).subscribe(
      (responce) => {
        this.vehiclesData = [];
        console.log(responce);
        this.vehiclesData = responce['vehicles'];
        if(this.vehiclesData.length == 0) {
          this.toastr.warning('لا يوجد بيانات لعرضها حاليا');
        }
        if(this.vehiclesData.length > 0) {
          this.showTableData = true;
        } else {
          this.showTableData = false;
        }
        this.spinner.hide();
      },
      (error) => {
        console.log(error);
      }
    )
  }

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


}
