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

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

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

  searchData: string = '';
  showTableData: boolean = false;
  vehicleData = [];
  typeReport:string = '';
  typeReportPage:string = '';

  ngOnInit() {
    this.route.params.subscribe(
      (params: Params) => {
        this.typeReport = params['typeReport'];
        if(params['typeReport'] == 'Movement') {
          this.typeReportPage = 'تقارير حركه المركبات';
          localStorage.setItem('pageIdActive', '49');
          this.searchData = '';
          this.vehicleData = [];
        } else if(params['typeReport'] == 'General') {
          this.typeReportPage = 'تقرير عام عن المركبه';
          localStorage.setItem('pageIdActive', '50');
          this.searchData = '';
          this.vehicleData = [];
        }
      }
    );
  }

  onGetData() {
    this.vehicleData = [];
    if(this.typeReport == 'Movement') {
      this.http.get(this.authSer.pathApi + '/report_vehicle_movement/' + this.searchData).subscribe(
        (responce) => {
          console.log(responce);
          this.vehicleData.push(responce['vehicle']);
          console.log(this.vehicleData);
          if(this.vehicleData[0] == null) {
            this.toastr.warning('لا يوجد بيانات لهذا المركبه');
            this.showTableData = false;
          } else {
            this.showTableData = true;
          }
          
        },
        (error) => {
          console.log(error);
          this.toastr.error('خطأ في البحث ، حاول ثانيه');
        }
      );
    } else if(this.typeReport == 'General') {
      this.http.get(this.authSer.pathApi + '/report_general_on_vehicle/' + this.searchData).subscribe(
        (responce) => {
          console.log(responce);
          this.vehicleData.push(responce['vehicle']);
          console.log(this.vehicleData);
          if(this.vehicleData[0] == null) {
            this.toastr.warning('لا يوجد بيانات لهذا المركبه');
            this.showTableData = false;
          } else {
            this.showTableData = true;
          }
          
        },
        (error) => {
          console.log(error);
          this.toastr.error('خطأ في البحث ، حاول ثانيه');
        }
      );
    } else {

    }
  };

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

}
