import { Component, OnInit, ViewChild } from "@angular/core";
import { DatePipe } from "@angular/common";
import {DxDataGridModule,DxButtonModule,DxPieChartComponent, DxChartComponent} from "devextreme-angular";
import { exportDataGrid } from "devextreme/exporter/exceljs/excelExporter";
import { Workbook } from "exceljs";
import { saveAs } from "file-saver";
import { JobmasterService } from "src/app/jobconfiguration/jobmaster/jobmaster.service";
import { ToastrService } from "ngx-toastr";
import { DashboardService } from "src/app/dashboard/dashboard.service";
import { ProjectDefinition } from "src/app/projectconfiguration/projectconfiguration/projectdefinition";
import { ProcesswiseproductionreportserviceService } from "../processwiseproductionreport/processwiseproductionreportservice.service";

import { Router, NavigationExtras, ActivatedRoute } from "@angular/router";
import { getMarkup, exportFromMarkup } from "devextreme/viz/export";
import { DashboardreportService } from "src/app/reports/dashboardreport/dashboardreport.service";
import { CursorError } from "@angular/compiler/src/ml_parser/lexer";
import number_box from "devextreme/ui/number_box";
import { NgxSpinnerService } from 'ngx-spinner';

declare var $: any;

@Component({
  selector: 'app-runtimereport',
  templateUrl: './runtimereport.component.html',
  styleUrls: ['./runtimereport.component.css']
})
export class RuntimereportComponent implements OnInit {
  @ViewChild(DxPieChartComponent, { static: false })
  pieChart: DxPieChartComponent;
  @ViewChild(DxChartComponent, { static: false }) 
  bidentrychart: DxChartComponent;
  @ViewChild(DxChartComponent, { static: false }) 
  qachart: DxChartComponent;
  @ViewChild(DxChartComponent, { static: false }) 
  webcheckchart: DxChartComponent;
  p_mode = "report";
  //p_mode='landing';
  Originallst: any = [];
  Dashboardlst: any = [];
  totatcount: any = [];
  jobProductTypeList: any = [];
  _ddlProjecttypeSettings: {};
  _ddljobSettings: {};
  userid: string = localStorage.getItem("userid");
  _userselectedItems: any[] = [];
  metaall: any[] = [];
  startdate: any;
  enddate: any;
  projectdefinitionid: number;
  joblst: any;
  _jobselectedItems: any = [];
  jobid: any;
  total_source: any;
  invaild: any;
  total_project: any;
  duplicatecount: any;
  project_title: string;
  gettotalvalues: any;
  filterValue: any;
  job_title: any;
  projecthealth_chart: any = [];
  projecthealth_piechart : any = [];
  intervalId
  auto_refresh: boolean = false;
  currentDate: any;
  dateRange:any;
  webcheck_chart: any={};
  fpr_chart: any={};
  bidentryprocess_chart: any={};
  qaprocess_chart:any={};
  runtimeprocesslst : any =[];
  runtimedashboardlst : any = [];
  projecthelathlst : any = [];
  webcheckprocesslst : any =[];
  fprprocesslst : any =[];
  bidentryprocesslst : any=[];
  qaprocesslst: any=[];
 // allprodefidlst: any=[];

  totalnofosource:any;
  sourcesyts:any;
  projectcollected:any;
  projectpublished: any;
  noyieldsources: any;

  today_webcheck: any = [];
  lasthour_webcheck: any = [];
  today_bidentry: any = [];
  lasthour_bidentry: any = [];
  today_fpr: any = [];
  lasthour_fpr: any = [];

  tabs_ : any;
 currTab1:number = 1;				  
  start_tabs: boolean = false;
  button: boolean = true;
  wipchartlst : any[];
  projecthealthhorulytracklst : any=[];
  wipchartlst_webcheck : any[];
  projecthealthhorulytracklst_webcheck : any=[];
  wipchartlst_FPR : any[];
  projecthealthhorulytracklst_FPR : any=[];
  activeTab: number = 1;

  constructor(
    private datepipe: DatePipe,
    private jobmasterservice: JobmasterService,
    private toastr: ToastrService,
    private processwiseproductionreport: ProcesswiseproductionreportserviceService,
    private dashboardservice: DashboardreportService,
    private router: Router,
    private route: ActivatedRoute,
    private SpinnerService: NgxSpinnerService,
  ) {}

  customizeLabel(arg) {
    return `${arg.valueText} (${arg.percentText})`;
  }

  ngOnDestroy(): void {
    //Called once, before the instance is destroyed.
    //Add 'implements OnDestroy' to the class.
    this.Stop_Auto_Refresh();
  }

  ngOnInit(): void {
 const self = this;
    $('#tabs > a').click(function() {
      debugger;
      var tab = $('.tab_' + $(this).attr('tab'));
     // if (tab.length) {
          // Hide active tab & selected style:
          $('.tab_active').removeClass('tab_active');
          $('#tabs .active').removeClass('active');
          // Show clicked tab content
          tab.addClass('tab_active');
          $(this).addClass('active');
          self.getproductionreport_tabwise(self.projectdefinitionid,'tabClick','');
  });
    this.jobid =0;
    this.currentDate = new Date();
    this.dateRange =  this.datepipe.transform(this.currentDate,"yyyy-MM-dd");
    this.startdate = this.datepipe.transform(this.currentDate, "dd-MMM-yyyy");
    this.enddate = this.datepipe.transform(this.currentDate, "dd-MMM-yyyy");   
     this.route.queryParams.subscribe((params) => {
      this.p_mode = params.mode_p;
      this.projectdefinitionid = params.prodefid;
    });
    this._ddljobSettings = {
      singleSelection: true,
      idField: "jobid",
      textField: "title",
      // selectAllText: 'Select All',
      // unSelectAllText: 'UnSelect All',
      itemsShowLimit: 7,
      allowSearchFilter: true,
    };
    this.onload();
  }
  moveNavigation(byX: number): void {
    var navigation= document.getElementsByClassName("tabs")[0];
  navigation.scrollLeft= navigation.scrollLeft + byX;
  }
  opengraph(event, graphName: string): boolean {
    debugger;
    var i, tab_content, tb;
    tab_content = document.getElementsByClassName("tab_content");
    for (i = 0; i < tab_content.length; i++) {
      tab_content[i].style.display = "none";
    }
    tb = document.getElementsByClassName("tb");
    for (i = 0; i < tb.length; i++) {
      tb[i].className = tb[i].className.replace(" active", "");
    }
    document.getElementById(graphName).style.display = "block";
   event.currentTarget.className += " active";
    return false;
  }
  tab_slideshow(){ 
    var currTab = 0;
  // count of all tabs
  var totalTabs = $("#tabs > a").length;
  // function to pass to setInterval
  function cycle() {
      // simulate click on current tab
      $("#tabs > a").eq(currTab).click();
      // increment counter   
      currTab++;
      // reset if we're at the last one
      if (currTab == totalTabs) {
          currTab = 0;
      }
  }
  // starting index
  // go!
  //30 seconds each tab auto play
   this.tabs_ = setInterval(cycle, 30000);
   //this.tabs_ = setInterval(cycle, 10000);
  }

  stop(){
    if(this.button === false){
    clearInterval(this.tabs_);
    this.button = true;
    }else{
      this.button = false;
      this.tab_slideshow();
    }
  }
  
  onload() { 
    this._ddlProjecttypeSettings = {
      singleSelection: false,
      idField: "projectdefinitionid",
      textField: "project_subtitle",
      // selectAllText: 'Select All',
      // unSelectAllText: 'UnSelect All',
      itemsShowLimit: 7,
      allowSearchFilter: true,
    };
    this.jobmasterservice.getJobProductType(this.userid).subscribe((res) => {
      if (
        res !== undefined &&
        res !== null &&
        res[0] !== undefined &&
        res[0] !== null
      ) {
        if (JSON.parse(res[0]).length > 0) {
          this.jobProductTypeList = JSON.parse(res[0]);
          // this._userselectedItems = this.jobProductTypeList.filter(
          //   (x) =>
          //     x.projectdefinitionid ==
          //     this.jobProductTypeList[0].projectdefinitionid
          // );
          let item = [];
          this.jobProductTypeList.forEach(element => {
            item.push(element);
          });
          this._userselectedItems = item;
          this.projectdefinitionid =
            this.jobProductTypeList[0].projectdefinitionid;
            this.project_title = this.jobProductTypeList[0].project_subtitle;
          // this.getproductionreport(this.projectdefinitionid,'onload','')
			 this.getproductionreport_tabwise(this.projectdefinitionid,'onload','')

        }
      }
    });
  }

  onrepdateValueChange(value: Date): void {
    this._jobselectedItems = [];
    this.Dashboardlst = [];
    this.startdate = this.datepipe.transform(this.dateRange, "dd-MMM-yyyy");
    this.enddate = this.datepipe.transform(this.dateRange, "dd-MMM-yyyy");
    if (
      this._userselectedItems.length != 0 &&
      this.startdate != null &&
      this.startdate != undefined &&
      this.startdate != "" &&
      this.enddate != null &&
      this.enddate != "" &&
      this.enddate != undefined &&
      this.jobid != null &&
      this.jobid != undefined
    ) {
      if(this._jobselectedItems.length !=0){
        this.Stop_Auto_Refresh();
        }
     // this.show_tab_1();
    // this.getproductionreport(this.projectdefinitionid,'date','');
	    this.getproductionreport_tabwise(this.projectdefinitionid,'date','');

    } else if (
      this._userselectedItems.length != 0 &&
      this.startdate != null &&
      this.startdate != undefined &&
      this.startdate != "" &&
      this.enddate != null &&
      this.enddate != "" &&
      this.enddate != undefined
    ) {
      if(this._jobselectedItems.length !=0){
        this.Stop_Auto_Refresh();
        }
      //this.show_tab_1();
      // this.getproductionreport(this.projectdefinitionid,'date','');
	   this.getproductionreport_tabwise(this.projectdefinitionid,'date','');

    }
  }

  onItemSelect(item: any, mode) {
    if (mode === "prodefid") {
      this.projectdefinitionid = item.projectdefinitionid;
      this.project_title = item.project_subtitle;
      if (
        this._userselectedItems.length !=0 &&
        this.startdate != null &&
        this.startdate != undefined &&
        this.startdate != "" &&
        this.enddate != null &&
        this.enddate != "" &&
        this.enddate != undefined &&
        this.jobid != null &&
        this.jobid != undefined
      ) {
        this.Stop_Auto_Refresh();
      this.show_tab_1();
      // this.getproductionreport(this.projectdefinitionid,'prodefid','');
		  this.getproductionreport_tabwise(this.projectdefinitionid,'prodefid','');
		
      } else if (
        this._userselectedItems.length !=0 &&
        this.startdate != null &&
        this.startdate != undefined &&
        this.startdate != "" &&
        this.enddate != null &&
        this.enddate != "" &&
        this.enddate != undefined
      ) {
        this.Stop_Auto_Refresh();
        this.show_tab_1();
     //   this.getproductionreport(this.projectdefinitionid,'prodefid','');
		 this.getproductionreport_tabwise(this.projectdefinitionid,'prodefid','');

      }
      this.totatcount = [];
    } else if (mode === "job") {
      this.totatcount = [];
      this.job_title = item.title;
      this.jobid = item.jobid;
      if (
        this.projectdefinitionid != 0 &&
        this.startdate != null &&
        this.startdate != undefined &&
        this.startdate != "" &&
        this.enddate != null &&
        this.enddate != "" &&
        this.enddate != undefined &&
        this.jobid != null &&
        this.jobid != undefined
      ) {
        this.Stop_Auto_Refresh();
      this.show_tab_1();
     //  this.getproductionreport(this.projectdefinitionid,'job','');
	    this.getproductionreport_tabwise(this.projectdefinitionid,'job','');
      }
    }
  }

  onItemDeSelect(item: any, mode) {
    if (mode === "job") {
      this.Dashboardlst = [];
      this.p_mode = "report";
      this.totatcount = [];
      this.Stop_Auto_Refresh();
    }else if (mode =='prodefid'){
      if(this._userselectedItems.length == 0){
        this.Dashboardlst = [];
        this.Originallst = [];
        this.totatcount = [];
        this.runtimeprocesslst=[];
        this.runtimedashboardlst=[];
        this.webcheckprocesslst =[];
        this.bidentryprocesslst =[];
        this.qaprocesslst = [];
        this.projecthealthhorulytracklst =[];
        this.wipchartlst = [];
        this.projecthealthhorulytracklst_webcheck =[];
       this.wipchartlst_webcheck =[];
       this.wipchartlst_FPR=[];
       this.projecthealthhorulytracklst_FPR=[];
       this.show_tab_1();
          this.Stop_Auto_Refresh();
      }else{
     //  this.getproductionreport(this.projectdefinitionid,'deselect','');
		  this.getproductionreport_tabwise(this.projectdefinitionid,'deselect','');
      }
    }
  }
  show_tab_1(){
    let i = [1,2,3,4,5,6,7,8,9,10,11,12,13]
    i.forEach(element => {
      debugger;
      let tab_id = 'tab_'+element;
      let tab_content_id = 'tab_content '+tab_id;
      let tab_class =  document.getElementById(tab_id).className;
      if(tab_class =='tb active' && element != 1){
        document.getElementById(tab_id).className = "tb";
        document.getElementById(tab_content_id).className =  tab_content_id;
        
        document.getElementById("tab_1").className = "tb active";
        document.getElementById('tab_content tab_1').className = "tab_content tab_1 tab_active";
      }
    });
  }
 /* show_tab_1(){
    let i = [1,2,3,4]
    i.forEach(element => {
      debugger;
      let tab_id = 'tab_'+element;
      let tab_content_id = 'tab_content '+tab_id;
      let tab_class =  document.getElementById(tab_id).className;
      if(tab_class =='tb active' && element != 1){
        document.getElementById(tab_id).className = "tb";
        //document.getElementById(tab_content_id).className =  tab_content_id;
          var tabs = document.querySelectorAll('.tabs');
        document.getElementById("tab_1").className = "tb active";
        this.activeTab = 1;
        //document.getElementById('tab_content tab_1').className = "tab_content tab_1 tab_active";
        //document.getElementById('tab_content tab_1').className = 'tab_content tab_1 tab_active';
 
      }
    });
  } 
 show_tab_1() {
    let i = [1, 2, 3, 4];
    i.forEach(element => {
      debugger;
      let tab_id = 'tab_' + element;
      let tab_content_id = 'tab_content_tab_' + element;
  
      let tab = document.getElementById(tab_id);
      let tab_content = document.getElementById(tab_content_id);
      //var tabs = document.querySelectorAll('.tab');
      if (tab && tab_content) {    
        tab.className = "tb active";
        tab_content.className = "tab_content_tab_" + element;  
          tab.className = "tb active";
          tab_content.className = "tab_content_tab_" + 1 + " tab_active";
      
         
        
      }
    });
  }*/
  getproductionreport_tabwise(prodefid,mode,value) {
    debugger;
    this.SpinnerService.show();
    const inputdata = [];
    let multiple_projectid:string = "";
    if(mode == 'selectall'){
      value.forEach(
        element => {
          if(multiple_projectid == ""){
            multiple_projectid = element.projectdefinitionid.toString();
          }else{
            multiple_projectid =multiple_projectid + ','+element.projectdefinitionid.toString();
          }
      });
    }else{
      this._userselectedItems.forEach(
        element => {
          if(multiple_projectid == ""){
            multiple_projectid = element.projectdefinitionid.toString();
          }else{
            multiple_projectid =multiple_projectid + ','+element.projectdefinitionid.toString();
          }
      });
    }
    for (var i = 1; i <= 13; i++) {
      let tab_id = "tab_"+i
      if(document.getElementById(tab_id).className =='tb active') {
        this.currTab1 = i;
        break;
    }
    }
    var navigation1= document.getElementsByClassName("tabs")[0];
    if(this.currTab1==1){
      navigation1.scrollLeft= navigation1.scrollLeft - 1890;
    }else{
          navigation1.scrollLeft= navigation1.scrollLeft + 150;
    }
    inputdata.push({
      p_projectdefinitionid: prodefid,
      p_startdate: this.startdate,
      p_enddate: this.enddate,
      p_jobid: this.jobid,
      mode_report: "dashboard",
      tab_num:this.currTab1,
      multipleprojectid_p:multiple_projectid
    });
    
    this.dashboardservice.GetDashboardReport(inputdata[0]).subscribe((res) => {
     this.SpinnerService.hide();
      if (res.body != null && res.body != undefined) {  
        let resarr = [];
        if(this.currTab1 == 1) {
        this.projecthealthhorulytracklst_webcheck =[];
        if(res.body.runtimehourlylst_WC != null && res.body.runtimehourlylst_WC != undefined &&  res.body.runtimehourlylst_WC != ''){
          resarr = JSON.parse(res.body.runtimehourlylst_WC);       
          resarr.forEach((v) => {
            this.projecthealthhorulytracklst_webcheck.push(v); 
          });
        }
      }else if(this.currTab1 == 2) {
        this.projecthealthhorulytracklst =[];
        if(res.body.runtimehourlylst_BE != null && res.body.runtimehourlylst_BE != undefined &&  res.body.runtimehourlylst_BE != ''){
          resarr = JSON.parse(res.body.runtimehourlylst_BE);       
          resarr.forEach((v) => {
            this.projecthealthhorulytracklst.push(v);
          });
        }
      }else if(this.currTab1 == 3) {
        this.projecthealthhorulytracklst_FPR=[];
        if(res.body.runtimehourlylst_FPR != null && res.body.runtimehourlylst_FPR != undefined &&  res.body.runtimehourlylst_FPR != ''){
          resarr = JSON.parse(res.body.runtimehourlylst_FPR);       
          resarr.forEach((v) => {
            this.projecthealthhorulytracklst_FPR.push(v);
          });
        }
      }else if(this.currTab1 == 4){
        this.projecthealth_chart = [];
        this.projecthealth_piechart=[];
        if(res.body.runtimedashboardlst != null && res.body.runtimedashboardlst != undefined &&  res.body.runtimedashboardlst != ''){
          resarr = res.body.runtimedashboardlst;
          resarr.forEach((val) => {
            this.runtimedashboardlst.push(JSON.parse(val));
          });
          this.projecthealth_chart = [
            {
              title: "Total No.of Source",
              value: this.runtimedashboardlst[0].TotalLots == null ? 0:this.runtimedashboardlst[0].TotalLots,
            },
            {
              title: "Sources YTS",
              value: this.runtimedashboardlst[0].wc_yts_count == null ? 0:this.runtimedashboardlst[0].wc_yts_count,
            },
            {
              title: "Projects Collected",
              value: this.runtimedashboardlst[0].wc_complete == null ? 0:this.runtimedashboardlst[0].wc_complete,
            },
            {
              title: "Projects Published",
              value: this.runtimedashboardlst[0].DispatchComplete == null ? 0: this.runtimedashboardlst[0].DispatchComplete,
            }, {
              title: "No Yield Sources",
              value: this.runtimedashboardlst[0].InvalidLots == null ? 0: this.runtimedashboardlst[0].InvalidLots,
            },
          ];
          this.projecthealth_piechart = [
           {
              title: "Sources YTS",
              value: this.runtimedashboardlst[0].wc_yts_count == null ? 0:this.runtimedashboardlst[0].wc_yts_count,
            },
            {
              title: "Projects Collected",
              value:this.runtimedashboardlst[0].wc_complete == null ? 0:this.runtimedashboardlst[0].wc_complete,
            },
            {
              title: "Projects Published",
              value: this.runtimedashboardlst[0].DispatchComplete == null ? 0: this.runtimedashboardlst[0].DispatchComplete,
            }, {
              title: "No Yield Sources",
              value: this.runtimedashboardlst[0].InvalidLots== null ? 0: this.runtimedashboardlst[0].InvalidLots,
            },
          ];
        }
      }
      else if(this.currTab1 == 5){
        this.webcheck_chart =[];this.runtimeprocesslst=[];
        if(res.body.runtimeprocesslst != null && res.body.runtimeprocesslst != undefined &&  res.body.runtimeprocesslst != ''){
          resarr = res.body.runtimeprocesslst;
          resarr.forEach((val) => {
            this.runtimeprocesslst.push(JSON.parse(val));
          });
        
        this.webcheck_chart =[
          {
            title:'Projects Collected',
            value:Number(this.runtimeprocesslst[0].Completed),
          },{
            title:'No Yield Sources',
            value:Number(this.runtimeprocesslst[0].Bypassed)+ Number(this.runtimeprocesslst[0].invalidlots)
          },{
            title:'Sources YTS',
            value:Number(this.runtimeprocesslst[0].YTS)
          },{
            title:'Sources Visited',
            value:Number(this.runtimeprocesslst[0].source_visted)
          }
        ];
      }
      }
      else if(this.currTab1 == 6){
        this.fpr_chart =[];this.runtimeprocesslst=[];
        if(res.body.runtimeprocesslst != null && res.body.runtimeprocesslst != undefined &&  res.body.runtimeprocesslst != ''){
          resarr = res.body.runtimeprocesslst;
          resarr.forEach((val) => {
            this.runtimeprocesslst.push(JSON.parse(val));
          });
        
          this.fpr_chart =[
            {
              title:'Projects Collected',
              value:Number(this.runtimeprocesslst[0].Completed== null ? 0: this.runtimeprocesslst[0].Completed),
            },{
              title:'No Yield Sources',
              value:Number(Number(this.runtimeprocesslst[0].Bypassed) + Number(this.runtimeprocesslst[0].invalidlots)== null ? 0: Number(this.runtimeprocesslst[0].Bypassed) + Number(this.runtimeprocesslst[0].invalidlots))
            },{
              title:'Sources YTS',
              value:Number(this.runtimeprocesslst[0].YTS== null ? 0: this.runtimeprocesslst[0].YTS)
            },{
              title:'Sources Visited',
              value:Number(this.runtimeprocesslst[0].source_visted== null ? 0: this.runtimeprocesslst[0].source_visted)
            }
          ];
      }
      }
      else if(this.currTab1 == 7){
        this.bidentryprocess_chart =[];
        this.qaprocess_chart =[];
        this.bidentryprocesslst =[];
        this.qaprocesslst = [];
        this.runtimeprocesslst=[];
        if(res.body.runtimeprocesslst != null && res.body.runtimeprocesslst != undefined &&  res.body.runtimeprocesslst != ''){
          resarr = res.body.runtimeprocesslst;
          resarr.forEach((val) => {
            this.runtimeprocesslst.push(JSON.parse(val));
          });
          this.bidentryprocesslst =[{
            bidentryyts:0,
            bidentrycompleted:0
          }];
          this.qaprocesslst=[{
            qacompleted:0,
            awip:0
          }];
          this.runtimeprocesslst.forEach(element => {
            if(element.Process ==='Bid Entry'){
              this.bidentryprocesslst[0].bidentryyts = element.YTS;
              this.bidentryprocesslst[0].bidentrycompleted = element.Completed;
            }
            if(element.Process === 'QA'){
              this.qaprocesslst[0].qacompleted = element.Completed;
              this.qaprocesslst[0].awip = element.WIP;
            }
          });
          this.bidentryprocess_chart =[
            {
              title:'YTS',
            value:Number(this.bidentryprocesslst[0].bidentryyts)
          },{
            title:'Completed',
            value:Number(this.bidentryprocesslst[0].bidentrycompleted)
          }];
          this.qaprocess_chart =[
            {
              title:'Completed',
              value:Number(this.qaprocesslst[0].qacompleted)
            },{
              title:'WIP',
              value:Number(this.qaprocesslst[0].awip)
            }
          ];
      }
      }
      else if(this.currTab1 == 8){
        this.today_webcheck=[];this.lasthour_webcheck=[];
        let resarr=res.body[0];
        resarr.forEach(val => { 
          this.today_webcheck.push(JSON.parse(val));        
        });
        let resarr1=res.body[1];
        resarr1.forEach(val => { 
          this.lasthour_webcheck.push(JSON.parse(val));
        });
      }
      else if(this.currTab1 == 9){      						      
        this.today_bidentry=[];this.lasthour_bidentry=[];
        let resarr=res.body[0];
        resarr.forEach(val => { 
          this.today_bidentry.push(JSON.parse(val));
        });
        let resarr1=res.body[1];
        resarr1.forEach(val => { 
          this.lasthour_bidentry.push(JSON.parse(val));
        });
      }
      else if(this.currTab1 == 10){ 
        this.today_fpr=[];this.lasthour_fpr=[];
        let resarr=res.body[0];
        resarr.forEach(val => { 
          this.today_fpr.push(JSON.parse(val));
        });
        let resarr1=res.body[1];
        resarr1.forEach(val => { 
          this.lasthour_fpr.push(JSON.parse(val));
        });
      }
      else if(this.currTab1 == 11){
        this.wipchartlst_webcheck =[];
        if(res.body.wipchartlst != null && res.body.wipchartlst != undefined &&  res.body.wipchartlst != ''){
          resarr =JSON.parse(res.body.wipchartlst);
          resarr.forEach((v) => {
            this.wipchartlst_webcheck.push(v);
          });
        }
      }
      else if(this.currTab1 == 12){
        this.wipchartlst = [];
        if(res.body.wipchartlst != null && res.body.wipchartlst != undefined &&  res.body.wipchartlst != ''){
          resarr =JSON.parse(res.body.wipchartlst);
          resarr.forEach((v) => {
            this.wipchartlst.push(v);
          });
        }
      }
      else if(this.currTab1 == 13){
        this.wipchartlst_FPR=[];
        if(res.body.wipchartlst != null && res.body.wipchartlst != undefined &&  res.body.wipchartlst != ''){
          resarr =JSON.parse(res.body.wipchartlst);
          resarr.forEach((v) => {
            this.wipchartlst_FPR.push(v);
          });
        }
      }
      if(this.auto_refresh === false){
          this.Auto_Refresh();
        }
      }
    }, err => { 
      this.SpinnerService.hide();
      this.toastr.error(err); 
    });

   /* if(this.currTab1 == 3) {
      this.getruntimeuserperformancereport();
    }
    else {
    inputdata.push({
      p_projectdefinitionid: prodefid,
      p_startdate: this.startdate,
      p_enddate: this.enddate,
      p_jobid: this.jobid,
      mode_report: "dashboard",
      tab_num:this.currTab1,
      multipleprojectid_p:multiple_projectid
    });
    
    this.dashboardservice.GetDashboardReport(inputdata[0]).subscribe((res) => {
     this.SpinnerService.hide();
      if (res.body != null && res.body != undefined) {        
        let resarr = [];
        this.Dashboardlst = [];
        this.Originallst = [];
        this.totatcount = [];
        this.runtimeprocesslst=[];
        this.runtimedashboardlst=[];
        this.webcheckprocesslst =[];
        this.bidentryprocesslst =[];
        this.qaprocesslst = [];
        this.projecthealthhorulytracklst =[];
        this.wipchartlst = [];
        this.projecthealthhorulytracklst_webcheck =[];
       this.wipchartlst_webcheck =[];
       this.wipchartlst_FPR=[];
       this.projecthealthhorulytracklst_FPR=[];
      if(this.currTab1 == 1) {
        //projecthealthhorulytracklst_webcheck
        if(res.body.runtimeprocesslst != null && res.body.runtimeprocesslst != undefined &&  res.body.runtimeprocesslst != ''){
          resarr = res.body.runtimeprocesslst;
          resarr.forEach((val) => {
            this.runtimeprocesslst.push(JSON.parse(val));
          });
        }
        if(res.body.runtimedashboardlst != null && res.body.runtimedashboardlst != undefined &&  res.body.runtimedashboardlst != ''){
          resarr = res.body.runtimedashboardlst;
          resarr.forEach((val) => {
            this.runtimedashboardlst.push(JSON.parse(val));
          });
        }
        if(res.body.runtimehourlylst != null && res.body.runtimehourlylst != undefined &&  res.body.runtimehourlylst != ''){
          resarr = JSON.parse(res.body.runtimehourlylst);       
          resarr.forEach((v) => {
            if(v.stage=="Web Check"){
              this.projecthealthhorulytracklst_webcheck.push(v);           
            } else  if(v.stage=="Bid Entry"){
            this.projecthealthhorulytracklst.push(v)
          } else  if(v.stage=="FPR"){
            this.projecthealthhorulytracklst_FPR.push(v);
          }
          });
        }
        this.projecthelathlst =[ {
          totalsource : this.runtimedashboardlst[0].TotalLots,
          sourcesyts: 0,
          project_collected : 0,
          project_published: this.runtimedashboardlst[0].DispatchComplete,
          noyieldsource: this.runtimedashboardlst[0].InvalidLots
        }];
        this.projecthealth_chart = [
          {
            title: "Total No.of Source",
            value: this.projecthelathlst[0].totalsource == null ? 0:this.projecthelathlst[0].totalsource,
          },
          {
            title: "Sources YTS",
            value: this.projecthelathlst[0].sourcesyts == null ? 0:this.projecthelathlst[0].sourcesyts,
          },
          {
            title: "Projects Collected",
            value: this.projecthelathlst[0].project_collected== null ? 0:this.projecthelathlst[0].project_collected,
          },
          {
            title: "Projects Published",
            value: this.projecthelathlst[0].project_published== null ? 0: this.projecthelathlst[0].project_published,
          }, {
            title: "No Yield Sources",
            value: this.projecthelathlst[0].noyieldsource== null ? 0: this.projecthelathlst[0].noyieldsource,
          },
        ];
        this.projecthealth_piechart = [
         {
            title: "Sources YTS",
            value: this.projecthelathlst[0].sourcesyts == null ? 0:this.projecthelathlst[0].sourcesyts,
          },
          {
            title: "Projects Collected",
            value: this.projecthelathlst[0].project_collected== null ? 0:this.projecthelathlst[0].project_collected,
          },
          {
            title: "Projects Published",
            value: this.projecthelathlst[0].project_published== null ? 0: this.projecthelathlst[0].project_published,
          }, {
            title: "No Yield Sources",
            value: this.projecthelathlst[0].noyieldsource== null ? 0: this.projecthelathlst[0].noyieldsource,
          },
        ];
      }
      else if(this.currTab1 == 2) {
        if(res.body.runtimeprocesslst != null && res.body.runtimeprocesslst != undefined &&  res.body.runtimeprocesslst != ''){
          resarr = res.body.runtimeprocesslst;
          resarr.forEach((val) => {
            this.runtimeprocesslst.push(JSON.parse(val));
          });
        }
        if(res.body.runtimedashboardlst != null && res.body.runtimedashboardlst != undefined &&  res.body.runtimedashboardlst != ''){
          resarr = res.body.runtimedashboardlst;
          resarr.forEach((val) => {
            this.runtimedashboardlst.push(JSON.parse(val));
          });
        }
        this.webcheckprocesslst = [{
          sourcesvisit : 0,
          sourceyts:0,
          noyieldsource:0,
          projectcollected:0
        }];
        this.fprprocesslst = [{
          sourcesvisit : 0,
          sourceyts:0,
          noyieldsource:0,
          projectcollected:0
        }];
        this.bidentryprocesslst =[{
          bidentryyts:0,
          bidentrycompleted:0
        }];
        this.qaprocesslst=[{
          qacompleted:0,
          awip:0
        }];
        this.runtimeprocesslst.forEach(element => {
          if(element.Process === 'Web Check'){
            this.projecthelathlst[0].sourcesyts = element.YTS;
            this.projecthelathlst[0].project_collected =  element.Completed; 
            this.webcheckprocesslst[0].sourceyts = element.YTS;
            this.webcheckprocesslst[0].noyieldsource = Number(element.Bypassed)
             + Number(this.runtimedashboardlst[0].InvalidLots); 
            this.webcheckprocesslst[0].sourcesvisit = element.source_visted;
            this.webcheckprocesslst[0].projectcollected = element.Completed;
          }
          if(element.Process === 'FPR'){
            this.fprprocesslst[0].sourceyts = element.YTS;
            this.fprprocesslst[0].noyieldsource = Number(element.Bypassed)
             + Number(this.runtimedashboardlst[0].InvalidLots); 
            this.fprprocesslst[0].sourcesvisit = element.source_visted;
            this.fprprocesslst[0].projectcollected = element.Completed;
          }
          if(element.Process ==='Bid Entry'){
            this.bidentryprocesslst[0].bidentryyts = element.YTS;
            this.bidentryprocesslst[0].bidentrycompleted = element.Completed;
          }
          if(element.Process === 'QA'){
            this.qaprocesslst[0].qacompleted = element.Completed;
            this.qaprocesslst[0].awip = element.WIP;
          }
        });
        this.webcheck_chart =[
          {
            title:'Projects Collected',
            value:Number(this.webcheckprocesslst[0].projectcollected),
          },{
            title:'No Yield Sources',
            value:Number(this.webcheckprocesslst[0].noyieldsource)
          },{
            title:'Sources YTS',
            value:Number(this.webcheckprocesslst[0].sourceyts)
          },{
            title:'Sources Visited',
            value:Number(this.webcheckprocesslst[0].sourcesvisit)
          }
        ];
        this.fpr_chart =[
          {
            title:'Projects Collected',
            value:Number(this.fprprocesslst[0].projectcollected== null ? 0: this.fprprocesslst[0].projectcollected),
          },{
            title:'No Yield Sources',
            value:Number(this.fprprocesslst[0].noyieldsource== null ? 0: this.fprprocesslst[0].noyieldsource)
          },{
            title:'Sources YTS',
            value:Number(this.fprprocesslst[0].sourceyts== null ? 0: this.fprprocesslst[0].sourceyts)
          },{
            title:'Sources Visited',
            value:Number(this.fprprocesslst[0].sourcesvisit== null ? 0: this.fprprocesslst[0].sourcesvisit)
          }
        ];
        this.bidentryprocess_chart =[
          {
            title:'YTS',
          value:Number(this.bidentryprocesslst[0].bidentryyts)
        },{
          title:'Completed',
          value:Number(this.bidentryprocesslst[0].bidentrycompleted)
        }];
        this.qaprocess_chart =[
          {
            title:'Completed',
            value:Number(this.qaprocesslst[0].qacompleted)
          },{
            title:'WIP',
            value:Number(this.qaprocesslst[0].awip)
          }
        ];
      }      
      else if(this.currTab1 == 4) {
        if(res.body.wipchartlst != null && res.body.wipchartlst != undefined &&  res.body.wipchartlst != ''){
          debugger;
          resarr =JSON.parse(res.body.wipchartlst);
          debugger;
          resarr.forEach((v) => {
            if(v.stage=="Web Check"){
              this.wipchartlst_webcheck.push(v);
            } else  if(v.stage=="Bid Entry"){
            this.wipchartlst.push(v)
          } else  if(v.stage=="FPR"){
            this.wipchartlst_FPR.push(v);
          }
          });
        }
      }
      if(this.auto_refresh === false){
        this.Auto_Refresh();
      }
      }
    }, err => { 
      this.SpinnerService.hide();
      this.toastr.error(err); });
    }*/
  }

  
  getproductionreport(prodefid,mode,value) {
    debugger;
    this.SpinnerService.show();
    const inputdata = [];
    let multiple_projectid:string = "";
    if(mode == 'selectall'){
      value.forEach(
        element => {
          if(multiple_projectid == ""){
            multiple_projectid = element.projectdefinitionid.toString();
          }else{
            multiple_projectid =multiple_projectid + ','+element.projectdefinitionid.toString();
          }
      });
    }else{
      this._userselectedItems.forEach(
        element => {
          if(multiple_projectid == ""){
            multiple_projectid = element.projectdefinitionid.toString();
          }else{
            multiple_projectid =multiple_projectid + ','+element.projectdefinitionid.toString();
          }
      });
    }

    inputdata.push({
      p_projectdefinitionid: prodefid,
      p_startdate: this.startdate,
      p_enddate: this.enddate,
      p_jobid: this.jobid,
      mode_report: "dashboard",
      multipleprojectid_p:multiple_projectid
    });
    this.dashboardservice.GetDashboardReport(inputdata[0]).subscribe((res) => {
     this.SpinnerService.hide();
      if (res.body != null && res.body != undefined) {
        let resarr = [];
        this.Dashboardlst = [];
        this.Originallst = [];
        this.totatcount = [];
        this.runtimeprocesslst=[];
        this.runtimedashboardlst=[];
        this.webcheckprocesslst =[];
        this.bidentryprocesslst =[];
        this.qaprocesslst = [];
        this.projecthealthhorulytracklst =[];
        this.wipchartlst = [];
        this.projecthealthhorulytracklst_webcheck =[];
       this.wipchartlst_webcheck =[];
       this.wipchartlst_FPR=[];
       this.projecthealthhorulytracklst_FPR=[];

        if(res.body.runtimeprocesslst != null && res.body.runtimeprocesslst != undefined &&  res.body.runtimeprocesslst != ''){
        resarr = res.body.runtimeprocesslst;
        resarr.forEach((val) => {
          this.runtimeprocesslst.push(JSON.parse(val));
        });
      }
        if(res.body.runtimedashboardlst != null && res.body.runtimedashboardlst != undefined &&  res.body.runtimedashboardlst != ''){
        resarr = res.body.runtimedashboardlst;
        resarr.forEach((val) => {
          this.runtimedashboardlst.push(JSON.parse(val));
        });
      }
        if(res.body.runtimehourlylst != null && res.body.runtimehourlylst != undefined &&  res.body.runtimehourlylst != ''){
          resarr = JSON.parse(res.body.runtimehourlylst);       
          resarr.forEach((v) => {
            if(v.stage=="Web Check"){
              this.projecthealthhorulytracklst_webcheck.push(v);           
            } else  if(v.stage=="Bid Entry"){
            this.projecthealthhorulytracklst.push(v)
          } else  if(v.stage=="FPR"){
            this.projecthealthhorulytracklst_FPR.push(v);
          }
          });
        }
        if(res.body.wipchartlst != null && res.body.wipchartlst != undefined &&  res.body.wipchartlst != ''){
          resarr =JSON.parse(res.body.wipchartlst);
          resarr.forEach((v) => {
            if(v.stage=="Web Check"){
              this.wipchartlst_webcheck.push(v);
            } else  if(v.stage=="Bid Entry"){
            this.wipchartlst.push(v)
          } else  if(v.stage=="FPR"){
            this.wipchartlst_FPR.push(v);
          }
          });
        }

            this.projecthelathlst =[ {
              totalsource : this.runtimedashboardlst[0].TotalLots,
              sourcesyts: 0,
              project_collected : 0,
              project_published: this.runtimedashboardlst[0].DispatchComplete,
              noyieldsource: this.runtimedashboardlst[0].InvalidLots
            }];
            this.webcheckprocesslst = [{
              sourcesvisit : 0,
              sourceyts:0,
              noyieldsource:0,
              projectcollected:0
            }];
            this.fprprocesslst = [{
              sourcesvisit : 0,
              sourceyts:0,
              noyieldsource:0,
              projectcollected:0
            }];
            this.bidentryprocesslst =[{
              bidentryyts:0,
              bidentrycompleted:0
            }];
            this.qaprocesslst=[{
              qacompleted:0,
              awip:0
            }];
            this.runtimeprocesslst.forEach(element => {
              if(element.Process === 'Web Check'){
                this.projecthelathlst[0].sourcesyts = element.YTS;
                this.projecthelathlst[0].project_collected =  element.Completed; 
                this.webcheckprocesslst[0].sourceyts = element.YTS;
                this.webcheckprocesslst[0].noyieldsource = Number(element.Bypassed)
                 + Number(this.runtimedashboardlst[0].InvalidLots); 
                this.webcheckprocesslst[0].sourcesvisit = element.source_visted;
                this.webcheckprocesslst[0].projectcollected = element.Completed;
              }
              if(element.Process === 'FPR'){
                this.fprprocesslst[0].sourceyts = element.YTS;
                this.fprprocesslst[0].noyieldsource = Number(element.Bypassed)
                 + Number(this.runtimedashboardlst[0].InvalidLots); 
                this.fprprocesslst[0].sourcesvisit = element.source_visted;
                this.fprprocesslst[0].projectcollected = element.Completed;
              }
              if(element.Process ==='Bid Entry'){
                this.bidentryprocesslst[0].bidentryyts = element.YTS;
                this.bidentryprocesslst[0].bidentrycompleted = element.Completed;
              }
              if(element.Process === 'QA'){
                this.qaprocesslst[0].qacompleted = element.Completed;
                this.qaprocesslst[0].awip = element.WIP;
              }
            });
        this.projecthealth_chart = [
          {
            title: "Total No.of Source",
            value: this.projecthelathlst[0].totalsource == null ? 0:this.projecthelathlst[0].totalsource,
          },
          {
            title: "Sources YTS",
            value: this.projecthelathlst[0].sourcesyts == null ? 0:this.projecthelathlst[0].sourcesyts,
          },
          {
            title: "Projects Collected",
            value: this.projecthelathlst[0].project_collected== null ? 0:this.projecthelathlst[0].project_collected,
          },
          {
            title: "Projects Published",
            value: this.projecthelathlst[0].project_published== null ? 0: this.projecthelathlst[0].project_published,
          }, {
            title: "No Yield Sources",
            value: this.projecthelathlst[0].noyieldsource== null ? 0: this.projecthelathlst[0].noyieldsource,
          },
        ];
        this.projecthealth_piechart = [
         {
            title: "Sources YTS",
            value: this.projecthelathlst[0].sourcesyts == null ? 0:this.projecthelathlst[0].sourcesyts,
          },
          {
            title: "Projects Collected",
            value: this.projecthelathlst[0].project_collected== null ? 0:this.projecthelathlst[0].project_collected,
          },
          {
            title: "Projects Published",
            value: this.projecthelathlst[0].project_published== null ? 0: this.projecthelathlst[0].project_published,
          }, {
            title: "No Yield Sources",
            value: this.projecthelathlst[0].noyieldsource== null ? 0: this.projecthelathlst[0].noyieldsource,
          },
        ];
        this.webcheck_chart =[
          {
            title:'Projects Collected',
            value:Number(this.webcheckprocesslst[0].projectcollected),
          },{
            title:'No Yield Sources',
            value:Number(this.webcheckprocesslst[0].noyieldsource)
          },{
            title:'Sources YTS',
            value:Number(this.webcheckprocesslst[0].sourceyts)
          },{
            title:'Sources Visited',
            value:Number(this.webcheckprocesslst[0].sourcesvisit)
          }
        ];
        this.fpr_chart =[
          {
            title:'Projects Collected',
            value:Number(this.fprprocesslst[0].projectcollected== null ? 0: this.fprprocesslst[0].projectcollected),
          },{
            title:'No Yield Sources',
            value:Number(this.fprprocesslst[0].noyieldsource== null ? 0: this.fprprocesslst[0].noyieldsource)
          },{
            title:'Sources YTS',
            value:Number(this.fprprocesslst[0].sourceyts== null ? 0: this.fprprocesslst[0].sourceyts)
          },{
            title:'Sources Visited',
            value:Number(this.fprprocesslst[0].sourcesvisit== null ? 0: this.fprprocesslst[0].sourcesvisit)
          }
        ];
        this.bidentryprocess_chart =[
          {
            title:'YTS',
          value:Number(this.bidentryprocesslst[0].bidentryyts)
        },{
          title:'Completed',
          value:Number(this.bidentryprocesslst[0].bidentrycompleted)
        }];

        this.qaprocess_chart =[
          {
            title:'Completed',
            value:Number(this.qaprocesslst[0].qacompleted)
          },{
            title:'WIP',
            value:Number(this.qaprocesslst[0].awip)
          }
        ];
        
        this.getruntimeuserperformancereport();

        if(this.auto_refresh === false){
          this.Auto_Refresh();
        }
      }
    }, err => { 
      this.SpinnerService.hide();
      this.toastr.error(err); });
  }
  customizeTooltip(arg: any) {
    if(arg.seriesName == "Series 1"){
      return {
        text: `${arg.argument}-${arg.value},${arg.percentText}`,
      };
    }else{
      return {
        text: `${arg.argument}-${arg.value}`,
      };
    }
   
  }
  loadjob(prodefid) {
    const input = [];
    input.push({
      p_projectdefinitionid: prodefid,
      p_startdate: this.startdate,
      p_enddate: this.enddate,
    });

    this.processwiseproductionreport.GetJobTitle(input[0]).subscribe((res) => {
    //  debugger;
      if (res.body != null && res.body != undefined) {
        this.joblst = [];
        this._jobselectedItems = [];
        this.Dashboardlst = [];
        this.joblst = JSON.parse(res.body);
      }
    });
  }
  onexport(e) {
    //this.dataGrid.instance.getDataSource().items();
    const workbook = new Workbook();
    const worksheet = workbook.addWorksheet("Dashboard Report");
    console.log(this.project_title);
    var projecttitle = this.project_title;
    var totallots = this.total_source;
    var totalproj = this.total_project;
    var invalid = this.invaild;
    var duplicate = this.duplicatecount;
    exportDataGrid({
      component: e.instance,
      worksheet,
      showBorders: true,
      autoFilterEnabled: true,
      rowAlternationEnabled: true,
      columnAutoWidth: true,
      topLeftCell: { row: 9, column: 1 },
      customizeCell(options) {
       // debugger;
        const { gridCell } = options;
        const { excelCell } = options;
        if (gridCell.rowType == "header") {
         // debugger;
          excelCell.fill = {
            type: "pattern",
            pattern: "solid",
            fgColor: { argb: "2b4c81" },
          };
          excelCell.font = { color: { argb: "FFFFFF" }, bold: true };
        }
        if (gridCell.rowType === "data") {
          //debugger;
          if (excelCell.fullAddress.row % 2 === 0) {
            excelCell.fill = {
              type: "pattern",
              pattern: "solid",
              fgColor: { argb: "dbedf2" },
            };
          }
        }
        excelCell.border = {
          top: { style: "thin" },
          left: { style: "thin" },
          bottom: { style: "thin" },
          right: { style: "thin" },
          color: { argb: "000000" },
        };
      },
    })
      .then((cellRange) => {
        // header
        const headerRow = worksheet.getRow(2);
        headerRow.height = 30;
        worksheet.mergeCells(2, 1, 2, 7);

        headerRow.getCell(1).value = projecttitle;
        headerRow.getCell(1).font = {
          name: "Segoe UI Light",
          size: 22,
          bold: true,
        };
        headerRow.getCell(1).alignment = {
          horizontal: "center",
          wrapText: true,
        };

        var rowstart = 4;
        for (var i = 0; i < 4; i++) {
          const row = worksheet.getRow(rowstart);
          if (i == 0) {
            row.getCell(1).value = "Total No. of Sources Created";
            row.getCell(1).alignment = { wrapText: true };
            row.getCell(2).value = totallots;
          } else if (i == 1) {
            row.getCell(1).value = "Total No. of Projects Collected";
            row.getCell(1).alignment = { wrapText: true };
            row.getCell(2).value = totalproj;
          } else if (i == 2) {
            row.getCell(1).value = "Invalid Record Count";
            row.getCell(1).alignment = { wrapText: true };
            row.getCell(2).value = invalid;
          } else if (i == 3) {
            row.getCell(1).value = "Duplicate Count";
            row.getCell(1).alignment = { wrapText: true };
            row.getCell(2).value = duplicate;
          }
          row.getCell(1).border = {
            top: { style: "thin" },
            left: { style: "thin" },
            bottom: { style: "thin" },
            right: { style: "thin" },
          };
          row.getCell(2).border = {
            top: { style: "thin" },
            left: { style: "thin" },
            bottom: { style: "thin" },
            right: { style: "thin" },
          };
          rowstart++;
        }
      })
      .then(() => {
        var currentdate = new Date();
        var datetime =
          currentdate.getDate() +
          "-" +
          (currentdate.getMonth() + 1) +
          "-" +
          currentdate.getFullYear() +
          "_" +
          currentdate.getHours() +
          "" +
          currentdate.getMinutes() +
          "" +
          currentdate.getSeconds();
        workbook.xlsx.writeBuffer().then((buffer) => {
          saveAs(
            new Blob([buffer], { type: "application/octet-stream" }),
            "Dashboardreport_" + datetime + ".xlsx"
          );
        });
      });
    e.instance.cancel = true;
  }

  onExporting(e) {
    const workbook = new Workbook();
    const worksheet = workbook.addWorksheet("Dashboard Report");

    exportDataGrid({
      component: e.component,
      worksheet,
      showBorders: true,
      autoFilterEnabled: true,
      rowAlternationEnabled: true,
      columnAutoWidth: true,
      customizeCell(options) {
        const { gridCell } = options;
        const { excelCell } = options;

        if (gridCell.rowType == "header") {
          excelCell.fill = {
            type: "pattern",
            pattern: "solid",
            fgColor: { argb: "2b4c81" },
          };
          excelCell.font = { color: { argb: "FFFFFF" }, bold: true };
        }
        if (gridCell.rowType === "header" || gridCell.rowType === "data") {
          if (excelCell.fullAddress.row % 2 === 0) {
            excelCell.fill = {
              type: "pattern",
              pattern: "solid",
              fgColor: { argb: "dbedf2" },
            };
          }
        }
        excelCell.border = {
          top: { style: "thin" },
          left: { style: "thin" },
          bottom: { style: "thin" },
          right: { style: "thin" },
          color: { argb: "000000" },
        };
      },
    }).then(() => {
      var currentdate = new Date();
      var datetime =
        currentdate.getDate() +
        "-" +
        (currentdate.getMonth() + 1) +
        "-" +
        currentdate.getFullYear() +
        "_" +
        currentdate.getHours() +
        "" +
        currentdate.getMinutes() +
        "" +
        currentdate.getSeconds();
      workbook.xlsx.writeBuffer().then((buffer) => {
        saveAs(
          new Blob([buffer], { type: "application/octet-stream" }),
          "Dashboardreport_" + datetime + ".xlsx"
        );
      });
    });
    e.cancel = true;
  }
  reportclick(data) {
    const queryParams = {};
    const navigationExtras: NavigationExtras = {
      queryParams,
      state: {
        workflowid: this.projectdefinitionid,
        fromdate: this.startdate,
        todate: this.enddate,
        jobid: this.jobid,
        mode: "dashboard",
        job_title: this.job_title,
      },
    };
    if (data == "1") {
      this.router.navigate(
        ["/reports/productiondashboardstagewisereport"],
        navigationExtras
      );
    } else if (data == "2") {
      this.router.navigate(
        ["/reports/userwiseproductionlogreport"],
        navigationExtras
      );
    } else if (data == "3") {
      this.router.navigate(
        ["/reports/processwiseproductionreport"],
        navigationExtras
      );
    } else if (data == "4") {
      this.router.navigate(
        ["/reports/userwiseproductionreport"],
        navigationExtras
      );
    }
  }
  pointClickHandler(e) {
    this.toggleVisibility(e.target);
  }
  legendClickHandler(e) {
    const arg = e.target;
    const item = e.component.getAllSeries()[0].getPointsByArg(arg)[0];
    this.toggleVisibility(item);
  }
  toggleVisibility(item) {
    if (item.isVisible()) {
      item.hide();
    } else {
      item.show();
    }
  }
  exportChart(e) {
    this.pieChart.instance.exportTo("Exported Chart", "PNG");
    let sample: any;
    sample = this.pieChart.onExporting;
  }

  // gokul 06-07-23
// auto refresh
Auto_Refresh(){
  //one minute = 60000
  //two minute= 120000
  //five minute = 300000
  this.auto_refresh = true;
  // this.intervalId = setInterval(()=>this.getproductionreport(this.projectdefinitionid,'autorefresh',''), 200000);
  this.intervalId = setInterval(()=>this.getproductionreport_tabwise(this.projectdefinitionid,'autorefresh',''), 300000);
}

Stop_Auto_Refresh(){
  this.auto_refresh = false;
  clearInterval(this.intervalId);
}

export_barchart(mode) {
  if(mode ==='bidentry'){
    this.bidentrychart.instance.exportTo('Example', 'png');

  }else if(mode ==='qa'){
    this.qachart.instance.exportTo('Example', 'png');

  }else if(mode ==='webcheck'){
  this.webcheckchart.instance.exportTo('Example', 'png');
  }
}

getruntimeuserperformancereport(){
  const input = [];
  let  multiple_prodefid :string = "";
  this._userselectedItems.forEach(element => {
    if(multiple_prodefid == ""){
      multiple_prodefid = element.projectdefinitionid.toString();
    }else{
      multiple_prodefid =multiple_prodefid + ','+element.projectdefinitionid.toString();
    }
  });
  input.push({p_startdate:this.startdate,p_enddate:this.enddate,multipleprojectid_p : multiple_prodefid});
  this.dashboardservice.Getruntimeuserperformancereport(input[0]).subscribe(res => {
 this.SpinnerService.hide();							 
this.today_webcheck=[];this.today_bidentry=[];this.lasthour_webcheck=[];this.lasthour_bidentry=[];
let resarr=res.body[0];
resarr.forEach(val => { 
  if(JSON.parse(val).stage=="Web Check"){
    this.today_webcheck.push(JSON.parse(val));
  }else  if(JSON.parse(val).stage=="Bid Entry"){
    this.today_bidentry.push(JSON.parse(val));
  }
  else  if(JSON.parse(val).stage=="FPR"){
    this.today_fpr.push(JSON.parse(val));
  }
});
let resarr1=res.body[1];
resarr1.forEach(val => { 
  if(JSON.parse(val).stage=="Web Check"){
    this.lasthour_webcheck.push(JSON.parse(val));
  }else  if(JSON.parse(val).stage=="Bid Entry"){
    this.lasthour_bidentry.push(JSON.parse(val));
  }
  else  if(JSON.parse(val).stage=="FPR"){
    this.lasthour_fpr.push(JSON.parse(val));
  }
});
		   if(this.auto_refresh === false){
          this.Auto_Refresh();
        }	 
  }, err => { this.toastr.error(err); });
}

onSelectAll(item){
  if(this.jobProductTypeList.length != 0){
   //  this.getproductionreport(this.projectdefinitionid,'selectall',item);
   this.getproductionreport_tabwise(this.projectdefinitionid,'selectall',item);
  }
}
onDeSelectAll(item){
  if(item.length == 0){
    this.Dashboardlst = [];
    this.Originallst = [];
    this.totatcount = [];
    this.runtimeprocesslst=[];
    this.runtimedashboardlst=[];
    this.webcheckprocesslst =[];
    this.bidentryprocesslst =[];
    this.qaprocesslst = [];
    this.projecthealthhorulytracklst =[];
    this.wipchartlst = [];
    this.projecthealthhorulytracklst_webcheck =[];
   this.wipchartlst_webcheck =[];
   this.wipchartlst_FPR=[];
   this.projecthealthhorulytracklst_FPR=[];
   this.show_tab_1();
      this.Stop_Auto_Refresh();
  }
}

}
