import { Component, Input, OnChanges, OnInit, SimpleChanges, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { DialogService } from 'ng2-bootstrap-modal';
import { NgxSpinnerService } from 'ngx-spinner';
import { ToastrService } from 'ngx-toastr';
import { DashboardService } from '../dashboard.service';
import { DatePipe } from '@angular/common';
@Component({
  selector: 'app-recorderrorinfo',
  templateUrl: './recorderrorinfo.component.html',
  styleUrls: ['./recorderrorinfo.component.css']
})
export class RecorderrorinfoComponent implements OnInit,OnChanges {

  @Input() jobid: number;
  @Input() userid: number;
  @Input() admin_rights: boolean;
  @Input() projectid: number;
  p = 1; 
  public searchText: string;
  operation:any;
  itemsPerPage = 10;
  key = 'project_subtitle';
  reverse = false;
  actionRights: any; 
  login_userid: string = localStorage.getItem("userid");

  recorderrorlst: any=[];
  constructor(    private SpinnerService: NgxSpinnerService,
    private dashboardservice: DashboardService,
    private dialogService: DialogService,
    private toastr: ToastrService,
    private router: Router,
    private datepipe :DatePipe) { }
    startdate:any;
    enddate:any;	
    dateRange: any = [new Date(new Date().setDate(new Date().getDate() ))
      ,new Date(new Date().setDate(new Date().getDate()))];
  ngOnChanges(changes: SimpleChanges): void {
    this.recorderrorlst = []; 
    this.searchText = '';
    var todaydate=new Date();
    this.startdate = this.datepipe.transform(todaydate, 'dd-MMM-yyyy');
    this.enddate = this.datepipe.transform(todaydate, 'dd-MMM-yyyy');
    this.onload();
  }

  ngOnInit(): void {
  }

  onload(){
    this.SpinnerService.show();
    console.log(this.userid);
    this.recorderrorlst = [];
    const formData = new FormData();
    formData.append("userid",this.login_userid);
    formData.append("projectid",this.projectid.toString());
    formData.append("mode", "dashboard_report");
    formData.append("p_startdate", this.startdate);
    formData.append("p_enddate", this.enddate);
    this.dashboardservice.recorderror(formData).subscribe(
      (res) => {
        debugger;
      this.SpinnerService.hide();
        if (res.body != "" && res.body != undefined && res.body != null) {
          this.recorderrorlst = JSON.parse(res.body);
        }
      },
      (error) => {
        this.SpinnerService.hide();
        this.toastr.error("Error while getting Record Error list.");
      }
    );
  }
  onrepdateValueChange(value: Date): void {
    this.startdate = this.datepipe.transform(value[0], 'dd-MMM-yyyy');
    this.enddate = this.datepipe.transform(value[1], 'dd-MMM-yyyy');
    if(this.startdate !=null && this.startdate !=undefined &&
      this.startdate !='' && this.enddate !=null && this.enddate !=''  && this.enddate !=undefined ){
        this.onload();
    }
  }
  sort(key) {
    this.key = key;
    this.reverse = !this.reverse;
  }
}
