import { Component, OnInit, TemplateRef, ViewChild, ViewEncapsulation } from '@angular/core';
import { DashboardService } from './dashboard.service';
import { ToastrService } from 'ngx-toastr';
import { BsModalRef, ModalOptions, BsModalService } from 'ngx-bootstrap';
import { Router, ActivatedRoute } from '@angular/router';
import * as $ from 'jquery'; import { debug } from 'util';
declare let $: any;
import { JobstatusreportComponent } from './jobstatusreport/jobstatusreport.component';
import { NavMenuService } from '../Shared/nav-menu/nav-menu.service';
import { DatePipe } from '@angular/common';
import { environment } from 'src/environments/environment';
@Component({
    selector: 'app-dashboard',
    templateUrl: './dashboard.component.html',
    styleUrls: ['./dashboard.component.css'],
    providers: [JobstatusreportComponent],
    encapsulation: ViewEncapsulation.None
})
export class DashboardComponent implements OnInit {
    @ViewChild(JobstatusreportComponent, { static: false }) childC: JobstatusreportComponent;
    userprojectlists: any;
    responsiveOptions: any;
    searchText = '';
    userprojectlist: any[];
    projectname: string;
    loginuserid: string = localStorage.getItem('userid');
    roleid: string = localStorage.getItem('roleid');
    prdefid: number;
    limitto: number;
    resizeMode = 'OverflowResizer';
    isPushstage = false;
    sMyJob = 'Y';
    sMyJob1 = '3px solid #2b4c81';
    sMyJob2 = '3px solid #ffffff';
    sMyJob3 = '3px solid #ffffff';
    sMyJob4 = '3px solid #ffffff';
    sMyJob5 = '3px solid #ffffff';
    sborder1: string;
    sborder2: string;
    sborder3: string;
    AssignedLotLoading_1 = false;
    AssignedLotLoading_2 = false;
    AssignedLotLoading_3 = true;
    sdashboard_1 = '#f39c12;';
    sdashboard_2 = 'IN PROCESS JOBS';
    joblist_output = [];
    search: any;
    url1: any;
    volumelist = [];
    SupplementList = [];
    jsonObj_st = [];
    swapeboardval: number;
    jobload_flag: boolean;
    dtitle = 'Title';
    uniqueStagename_st = [];
    projectjoblist: any;
    wkb_book_type: any;
    joblist_filter: any;
    joblist = [];
    jobstatuslist = [];
    currentjobno: any;
    jobFilterlist: any[];
    loadtype: string;

    jwt_token: string;
    uniqueStagename = [];
    uniqueFilename = [];
    jsonObj = [];
    onlineUsers: any;

    uniqueFilename_st: any;
    uniqueStagename_st_temp: any;

    tempVal: any;

    selectedproject = '';
    jobid: number;
    jobstatus: string;
    list: any = [];
    viewJobStatusReport = false;
    viewMyJobs = false;
    viewJobAssignment = false;
    viewMailBox = false;
    supplementlotids: any = [];
    loadheaders: any = [];
    cebmfiles: any = [];
    joblist_filter1: any = [];
    stagelist: any = [];
    volumeFilterlist: any = [];
    SupplementList_output: any = [];
    testing:any =[];
    selected_user:any;
    pass_currentjobid:any;
    selecteduserid:any;
    job_report_var = false;
    test: any = "3";
    admin_dropdown = false;
    user_list:any=[];
    temp_joblist:any;
    _ddljobSettings: any;
    jobtitle:any=[];
    org_job_list:any=[];
    ireflect: string = environment.ireflect;
    vlexjobSearch: string = '';
    filteredsearchjob: any[] = [];
    
    constructor(
        private dashboardservice: DashboardService,
        private toastr: ToastrService,
        private router: Router,
        private jobStatusObj: JobstatusreportComponent,
        private route: ActivatedRoute,
        private navmenuservice: NavMenuService,
        private datepipe :DatePipe

    ) {
        this.responsiveOptions = [
            {
                breakpoint: '1024px',
                numVisible: 3,
                numScroll: 3
            },
            {
                breakpoint: '768px',
                numVisible: 2,
                numScroll: 2
            },
            {
                breakpoint: '560px',
                numVisible: 1,
                numScroll: 1
            }
        ];
        // this.url1 = $routeParams.id;
    }

    ngOnInit(): void {
        if (localStorage.getItem('prdefid') !== undefined && localStorage.getItem('prdefid') !== null &&
            localStorage.getItem('prdefid') !== '') {
            this.prdefid = +localStorage.getItem('prdefid');
        } else {
            this.prdefid = 0;
        }
        // localStorage.setItem('jobid', '');
        this.currentjobno = 0;
        this.search = '';
       // this.getLoadType();
        this.LoadUserProjectStaus(); //tab date for project details
        this.loadStatus('Y');
        this.sMyJob = 'Y';
        this.jobload_flag = true;
        this.swapeboardval = 1;
        this.selecteduserid =0;
        this.job_report_var = false;
        this.loadExisitingRoleList();
        this._ddljobSettings = {
            singleSelection: true,
             // singleSelection: false,
            idField: 'jobid',
            textField: 'title',
            // selectAllText: 'Select All',
            // unSelectAllText: 'UnSelect All',
            // itemsShowLimit: 7,
            allowSearchFilter: true,
            //customFilter: this.customSearch.bind(this) 
          };
         // console.log(this._ddljobSettings);
          
    }
    getLoadType() {
        const inputvalue = [];
        inputvalue.push({ userid: + this.loginuserid });
        this.dashboardservice.getLoadType(inputvalue[0]).subscribe(res => {
            //console.log(res.body)
            this.loadtype = res.body.loadtype;
        });
    }

    LoadUserProjectStaus() {
        debugger;
        const inputvalue = [];
        inputvalue.push({ userid: + this.loginuserid });
        this.dashboardservice.getUserProjectStatusDetails(inputvalue[0]).subscribe(res => {
            if (res !== undefined && res !== null && res.body[0] !== undefined && res.body[0] !== null) {
                if (JSON.parse(res.body[0]).length > 0) {
                    this.userprojectlist = JSON.parse(res.body[0]);
                    console.log('projectlist',this.userprojectlist);
                    let project_definitionid = this.userprojectlist[0].project_definitionid;
                    const project_subtitle = this.userprojectlist[0].project_subtitle;
                    if (localStorage.getItem('prdefid') !== undefined && localStorage.getItem('prdefid') !== null &&
                        localStorage.getItem('prdefid') !== '') {
                        this.LoadProjectJobDetails(+localStorage.getItem('prdefid'), localStorage.getItem('projectname'));
                    } else {
                        this.LoadProjectJobDetails(project_definitionid, project_subtitle);
                    }
                 /*    this.route.params.subscribe(params => {
                        console.log(params);
                        let project_definitionid = +params['projectId'] ? +params['projectId'] : 0;
                        if (project_definitionid > 0) {
                            const project = this.userprojectlist.filter(u => u.project_definitionid === project_definitionid);
                            if (localStorage.getItem('prdefid') !== undefined && localStorage.getItem('prdefid') !== null &&
                                localStorage.getItem('prdefid') !== '') {
                                this.LoadProjectJobDetails(+localStorage.getItem('prdefid'), localStorage.getItem('projectname'));
                            } else {
                                this.LoadProjectJobDetails(project_definitionid, project[0].project_subtitle);
                            }
                        } else {
                            project_definitionid = this.userprojectlist[0].project_definitionid;
                            const project_subtitle = this.userprojectlist[0].project_subtitle;
                            if (localStorage.getItem('prdefid') !== undefined && localStorage.getItem('prdefid') !== null &&
                                localStorage.getItem('prdefid') !== '') {
                                this.LoadProjectJobDetails(+localStorage.getItem('prdefid'), localStorage.getItem('projectname'));
                            } else {
                                this.LoadProjectJobDetails(project_definitionid, project_subtitle);
                            }
                        }
                    }); */
                }
            }
        });
    }

    fn_LoadProjectJobDetails(projectdid, projectname) {
        this.prdefid = projectdid;
        this.limitto = 5;
        localStorage.setItem('prdefid', '');
        localStorage.setItem('prdefid', this.prdefid.toString());
        // if (projectname === '') {

        //  }
    }
    LoadProjectJobDetails(projectdid, projectname) {
        debugger;
        // localStorage.setItem('jobid', '');
        this.AssignedLotLoading_2 = false;        
        // this.loadtype = $('#loadtype').text();
        //this.loadtype = 'dashboard';
       // if (this.loadtype === 'dashboard') {
            this.fn_LoadJoblistDetails(projectdid);
            this.prdefid = projectdid;
            this.call_userslst(this.prdefid);
            this.projectname = projectname;
            if (this.prdefid !== +localStorage.getItem('prdefid')) {
                localStorage.setItem('jobid', '');
                localStorage.setItem('jobtitle', '');
            }
             localStorage.setItem('prdefid', projectdid);
             localStorage.setItem('projectname', projectname);
            //this.fn_LoadProjectJobDetails(projectdid, projectname);
            this.search = '';
        //}
         /* else {
            this.fn_LoadProjectJobDetails(projectdid, projectname);
            this.loadTeamMemberJobList(projectdid);
        } */
    }


    loadTeamMemberJobList(prid) {
        this.AssignedLotLoading_1 = false;
        // var getTeamMemberJobList1 = DashboardService.getStagelist(prid);
        // getTeamMemberJobList1.then(function (d) {
        // 	this.project_subtitle = d.data[0].project_subtitle;
        // 	if (d.data[0].wfdid != '') {
        // 		this.stagelist = d.data;
        // 		this.stagelist[0].selectstage = 'true';
        // 		loadjobs(this.stagelist[0].wfdid);
        // 		loadUsers(this.stagelist[0].wfdid);
        // 		this.workflowid = this.stagelist[0].wfdid;
        // 	}
        // }, function (error) {
        // 	toastr["error"]("error in loading team member details", "Message", toastr.options.positionclass = 'toast-top-right');
        // });
    }
    async loadStatus(sStatus) {
        if (sStatus === 'Y') {
            this.AssignedLotLoading_3 = false;
            this.sborder1 = '#e2e2e2';
            this.sborder2 = '#fff';
            this.sborder3 = '#fff';
            this.sMyJob1 = '3px solid #2b4c81';
            this.sMyJob2 = '3px solid #ffffff';
            this.sMyJob3 = '3px solid #ffffff';
            this.sMyJob4 = '3px solid #ffffff';
            this.sMyJob5 = '3px solid #ffffff';
            this.sMyJob = 'Y';
            this.search = '';
            // this.userprojectlist = [];
            if (this.prdefid === 0) {
                const resData = await this.loadUserProjectList();
                if (resData !== undefined && resData !== null && resData[0] !== undefined && resData[0] !== null
                    && JSON.parse(resData).length > 0) {
                    this.userprojectlist = [];
                    this.userprojectlist = JSON.parse(resData);
                    console.log('userprojectlist',this.userprojectlist);
                    this.prdefid = this.userprojectlist[0].project_definitionid;
                    this.projectname = this.userprojectlist[0].project_subtitle;
                    this.jobtitle = this.userprojectlist[0].jobtitle;
                    this.fn_LoadJoblistDetails(this.prdefid);//load job details and set local storage
                    this.call_userslst(this.prdefid);
                } else {
                    this.toastr.error('No projects get mapped to the user');
                }
            } 
            /* else {
                this.fn_LoadJoblistDetails(this.prdefid);
                this.call_userslst(this.prdefid);
            } */
        } else if (sStatus === 'N') {
            this.sMyJob1 = '3px solid #ffffff';
            this.sMyJob2 = '3px solid #2b4c81';
            this.sMyJob3 = '3px solid #ffffff';
            this.sMyJob5 = '3px solid #ffffff';

            this.sborder1 = '#fff';
            this.sborder2 = '#e2e2e2';
            this.sborder3 = '#fff';
            this.sMyJob = 'N';
            this.sMyJob4 = '3px solid #ffffff';
            debugger;
            this.search = '';
           if (this.search === '') {
                if (this.joblist_output.length > 0) {
                    this.search = '';
                    this.search = this.joblist_output[0].title;
                } else {
                    this.search = '';
                }
            }
            const filestatus = this.userprojectlist.filter(k => k.project_definitionid === this.prdefid);
            if (filestatus !== undefined && filestatus !== null && filestatus.length > 0) {
                this.selectedproject = filestatus[0].project_subtitle;
            } else {
                this.selectedproject = this.prdefid.toString();
            }


        } else if (sStatus === 'N1') {
            this.sMyJob1 = '3px solid #ffffff';
            this.sMyJob2 = '3px solid #ffffff';
            this.sMyJob3 = '3px solid #2b4c81';
            this.sMyJob4 = '3px solid #ffffff';
            this.sMyJob5 = '3px solid #ffffff';
            this.sborder1 = '#fff';
            this.sborder2 = '#e2e2e2';
            this.sborder3 = '#fff';
            this.sMyJob = 'N';
            // fn_LoadProjectJobDetails(this.prdefid, '');
        } else if (sStatus === 'Z') {
            this.sMyJob1 = '3px solid #ffffff';
            this.sMyJob2 = '3px solid #ffffff';
            this.sMyJob3 = '3px solid #2b4c81';
            this.sMyJob4 = '3px solid #ffffff';
            this.sMyJob5 = '3px solid #ffffff';

            this.sborder1 = '#fff';
            this.sborder2 = '#fff';
            this.sborder3 = '#e2e2e2';
            this.sMyJob = 'Z';
        } else if (sStatus === 'A') {
            this.sMyJob1 = '3px solid #ffffff';
            this.sMyJob2 = '3px solid #ffffff';
            this.sMyJob3 = '3px solid #ffffff';
            this.sMyJob4 = '3px solid #2b4c81';
            this.sMyJob5 = '3px solid #ffffff';

            this.sborder1 = '#fff';
            this.sborder2 = '#fff';
            this.sborder3 = '#e2e2e2';
            this.sMyJob = 'A';
            // this.router.navigate(['/dashboard/jobassignment'], { queryParams: { projectdefid: this.prdefid } });
        }else if(sStatus === 'E'){
             this.sMyJob1 = '3px solid #ffffff';
            this.sMyJob2 = '3px solid #ffffff';
            this.sMyJob3 = '3px solid #ffffff';
            this.sMyJob4 = '3px solid #ffffff';
            this.sMyJob5 = '3px solid #2b4c81';

            this.sborder1 = '#fff';
            this.sborder2 = '#fff';
            this.sborder3 = '#e2e2e2';
            this.sMyJob = 'E';
        }
    }

    call_userslst(project_definitionid){
        debugger;
    /*     console.log(project_definitionid);
        debugger;
        let value:any = [];
        this.dashboardservice.Getusers(project_definitionid).subscribe(res => {
            if (res !== undefined && res !== null && res.body !== undefined && res.body !== null) {
                debugger;
                console.log('call_userslst',res);
               this.testing = JSON.parse(res.body[0]);
               this.user_list = this.testing;
            this.selecteduserid = this.testing[0].userid;
            this.selected_user =this.testing[0].userid;
           value =this.testing.filter(i => i.userid === Number(this.loginuserid)); */
            this.job_report_var =true;
            this.selecteduserid =this.loginuserid;
            this.selected_user=this.loginuserid;
             if(parseInt(this.roleid) === 1 || parseInt(this.roleid)  ===3){
                this.admin_dropdown =true;
             }else{
                this.admin_dropdown = false;
                this.testing = null;
             }
        /*     } else {
                this.toastr.info('No data found!');
            }
        }, error => {
            this.toastr.error('Error while getting data');
        }); */
    }

    fn_LoadJoblistDetails(projectdefinitionid) {
        debugger;
        this.joblist = [];
        const inputvalue = [];
        inputvalue.push({ projectdefinitionid: + projectdefinitionid });
        this.dashboardservice.getJobNo(inputvalue[0]).subscribe(res => {
            if (res !== undefined && res !== null && res.body[0] !== undefined && res.body[0] !== null) {
                if (JSON.parse(res.body[0]).length > 0) {
                    //console.log('loadjoblist',JSON.parse(res.body[0]));//status R 
                    this.joblist_filter = JSON.parse(res.body[0]);
                    this.temp_joblist = JSON.parse(res.body[0]);
                    this.loadjobs_basedflag(); //set local storage and variable and load stage details
                } else {
                    this.toastr.info('No data found!');
                    this.joblist = this.joblist_filter = [];
                }
            } else {
                this.toastr.info('No data found!');
                this.joblist = this.joblist_filter = [];
            }

        }, error => {
            this.toastr.error('Error while getting data');
        });
    }
    filterJobList() {
    const term = this.vlexjobSearch.toLowerCase();
    this.filteredsearchjob = this.joblist.filter(j =>
        j.title.toLowerCase().includes(term) ||
        j.searchtext.toLowerCase().includes(term)
    );
}
    loadjobs_basedflag() {
        debugger;
        if (this.joblist_filter.length > 0) {
            this.joblist_output = [];
            let myObj = localStorage.getItem('jobid');
            this.joblist = [];this.org_job_list=[];
            this.joblist_filter.forEach(obj => {
                this.joblist.push(obj);
            });
            console.log(this.joblist);            
            this.filteredsearchjob = [...this.joblist];
           /*   let sid = 0;
            //  let sbool = false;
            this.joblist_filter.forEach(obj => {
                if (this.jobload_flag === true) {
                    if (obj.jobstatus !== 'C') {
                        if (this.joblist_filter[sid] !== undefined) {
                            this.joblist.push(this.joblist_filter[sid]);
                        }
                    }
                } else {
                    if (obj.jobstatus === 'C') {
                        if (this.joblist_filter[sid] !== undefined) {
                            this.joblist.push(this.joblist_filter[sid]);
                        }
                    }
                    if (obj.jobstatus !== 'C') {
                        if (this.joblist_filter[sid] !== undefined) {
                            this.joblist.push(this.joblist_filter[sid]);
                        }
                    }
                }
                sid = sid + 1;
            }); */
            if (myObj === null) { myObj = ''; }
            if (myObj !== '') {
                this.currentjobno = +myObj;
                const col = this.joblist.filter(i => i.jobid === +myObj);
                if (col !== undefined && col.length > 0) {
                    this.joblist_output.push(col[0]);
                    localStorage.setItem('search', this.joblist_output[0].title);
                }else{
                    localStorage.setItem('search', this.joblist[0].title);
                }
                this.currentjobno = +myObj;

            } else if (this.joblist.length > 0) {
                const col = this.joblist[0];
                this.joblist_output.push(col);
                if (this.currentjobno) {
                    this.currentjobno = this.joblist_output[0].jobid;
                    localStorage.setItem('jobid', this.joblist_output[0].jobid);
                    localStorage.setItem('search', this.joblist_output[0].title);
                } else {
                    this.currentjobno = this.joblist_output[0].jobid;
                    localStorage.setItem('jobid', this.joblist_output[0].jobid);
                    localStorage.setItem('search', this.joblist_output[0].title);
                }
                this.search=this.joblist_output[0].title;
            }
            if (localStorage.getItem('search') !== undefined && localStorage.getItem('search') !== null &&
                localStorage.getItem('search') !== '') {
                this.selectstagedetails(this.currentjobno, localStorage.getItem('search')); //load lot details
                this.jobtitle=this.joblist.filter(x => x.jobid == this.currentjobno);

            }
            else {
                this.selectstagedetails(this.currentjobno, ''); //load lot details
                this.jobtitle=this.joblist.filter(x => x.jobid == this.currentjobno);
            }
           this.org_job_list=this.joblist; 
        } else {
            this.jobstatuslist = [];
            this.jobFilterlist = [];
        }
    }



    // need this function to get jobload_flag value
    onjoblistload(sval) {
        debugger;
        if (sval === 'green;') {
            this.sdashboard_1 = '#f39c12;';
             this.sdashboard_2 = 'IN PROCESS JOBS';
        } else {
            this.sdashboard_1 = 'green;'; 
            this.sdashboard_2 = 'COMPLETED JOBS';
        }
        if (sval === 'green;') {
            this.jobload_flag = true;
        } else {
            this.jobload_flag = false;
        }
        this.loadjobs_basedflag();

    }

    ready_to_yts() { }
    showerror(input) { }
    TriggerApp(input) { }
    backword(input) { }
    DashboardMove(input) { }

  
    loadJobAssignment() {
        this.router.navigate(['/dashboard/jobassignment']);
    }
    async loadUserProjectList() {
        const inputvalue = [];
        inputvalue.push({ userid: + this.loginuserid });
        return await this.dashboardservice.getUserProjectStatusDetails(inputvalue[0]).toPromise().then(res => res.body);
    }

    onJoblistSwipe() {
        this.currentjobno = this.currentjobno;
        this.jobload_flag = this.jobload_flag;
        if ((this.swapeboardval === undefined) || (this.swapeboardval === 1)) {
            this.swapeboardval = 2;
        } else {
            this.swapeboardval = 1;
        }
    }
    pageRefresh() {
        const typecheck = typeof this.currentjobno;
        if (typecheck === 'string') {
            this.currentjobno = +this.currentjobno;
        } else {
            this.currentjobno = this.currentjobno.toString();
        }
    }

    loadExisitingRoleList() {
        this.navmenuservice.getRoleMappedMenuDashboard(+this.loginuserid).subscribe(res => {
            if (res !== undefined && res != null && res.length > 0) {
                res.forEach(i => {
                    if (i !== undefined) {
                        this.list.push(JSON.parse(i));
                    }
                });

                if (this.list.filter(li => li.name === 'Job Status Report').length > 0) {
                    const JobStatusReport = this.list.filter(li => li.name === 'Job Status Report');
                    const btnToEnable = JSON.parse(JobStatusReport[0].actionrights);
                    if (btnToEnable.length > 0) {
                        btnToEnable.forEach(d => {
                            if (d !== undefined) {
                                if (d === 2) {
                                    this.viewJobStatusReport = true;

                                }
                            }
                        });
                    }
                }

                if (this.list.filter(li => li.name === 'My Jobs').length > 0) {
                    debugger;
                    const JobStatusReport = this.list.filter(li => li.name === 'My Jobs');
                    const btnToEnable = JSON.parse(JobStatusReport[0].actionrights);
                    if (btnToEnable.length > 0) {
                        btnToEnable.forEach(d => {
                            if (d !== undefined) {
                                if (d === 2) {
                                    this.viewMyJobs = true;
                                }
                            }
                        });
                    }
                }
                if (this.list.filter(li => li.name === 'Job Assignment').length > 0) {
                    const JobStatusReport = this.list.filter(li => li.name === 'Job Assignment');
                    const btnToEnable = JSON.parse(JobStatusReport[0].actionrights);
                    if (btnToEnable.length > 0) {
                        btnToEnable.forEach(d => {
                            if (d !== undefined) {
                                if (d === 2) {
                                    this.viewJobAssignment = true;
                                }
                            }
                        });
                    }
                }

                if (this.list.filter(li => li.name === 'Mail Box').length > 0) {
                    const JobStatusReport = this.list.filter(li => li.name === 'Mail Box');
                    const btnToEnable = JSON.parse(JobStatusReport[0].actionrights);
                    if (btnToEnable.length > 0) {
                        btnToEnable.forEach(d => {
                            if (d !== undefined) {
                                if (d === 2) {
                                    this.viewMailBox = true;
                                }
                            }
                        });
                    }
                }
            }
        });
    }
    selectstagedetails_1(eve_selectuser){
        debugger;
        this.selected_user =  eve_selectuser;
        this.selecteduserid =  eve_selectuser;
        const temp_joblist = this.temp_joblist;
        this.joblist = temp_joblist.filter(x=>x.createdby === Number(this.selected_user));
        /* if (localStorage.getItem('search') !== undefined && localStorage.getItem('search') !== null &&
                localStorage.getItem('search') !== '') {
                    //gokul pinned
                this.selectstagedetails(this.currentjobno, localStorage.getItem('search'));
            }
            else {
                this.selectstagedetails(this.currentjobno, '');
            } */
    }

    selectstagedetails(eve, search) {
        debugger;
        localStorage.setItem('jobid', '');
        //const currentjobno = +eve ? eve : +eve.target.value;
        let currentjobno;
        if(eve===0){
             currentjobno = 0;
        }else{
             currentjobno = +eve ? eve : +eve.jobid;
        }
        
        // this.currentjobno = +eve ? eve : +eve.target.value;
       // this.search = search ? search : eve.target.selectedOptions[0].text;
       this.search = search ? search : eve.title;
        localStorage.setItem('search', this.search);
        localStorage.setItem('jobid', currentjobno);
        this.loadheaders = [];
        this.cebmfiles = [];
        if (this.joblist_output.length > 0) {
            this.dashboardservice.GetJobStatusreport(currentjobno).subscribe(res => {
                if (res.body !== undefined && res.body !== null) {
                    if (res.body.chapdet.length > 0) {
                        this.joblist_filter1 = res.body;
                        //console.log('this.joblist_filter1=', JSON.stringify(this.joblist_filter1));
                        if (this.joblist_filter1.stagedet !== undefined) {
                            this.stagelist = this.joblist_filter1.stagedet;
                            this.jobstatuslist = this.joblist_filter1.chapdet;
                            this.jobFilterlist = this.joblist_filter1.chapdet;
                           // this.volumelist = this.joblist_filter1.volumedet;
                            //this.SupplementList = this.joblist_filter1.supplementdet;
                            this.currentjobno = currentjobno;
                        } else {
                            this.jobstatuslist = [];
                            this.stagelist = [];
                            this.jobFilterlist = [];
                           // this.SupplementList = [];
                        }
                    } else {
                        this.currentjobno = currentjobno;
                        this.sMyJob = 'Y';
                        this.viewJobStatusReport = true;
                        this.toastr.info('No data found!');
                        //this.joblist = this.joblist_filter = [];
                    }
                } else {
                    this.toastr.info('No data found!');
                    this.joblist = this.joblist_filter = [];
                }
            }, error => {
                this.jobstatuslist = [];
                this.stagelist = [];
                this.jobFilterlist = [];
                //this.volumelist = [];
               // this.volumeFilterlist = [];
               // this.SupplementList = [];
                this.toastr.error('Error in loading job details');

            });

        } else {
            this.jobstatuslist = [];
            this.stagelist = [];
            this.jobFilterlist = [];
            //this.volumelist = [];
            //this.volumeFilterlist = [];
            //this.SupplementList = [];
        }

    }

    SupplementClick() {
        if (this.SupplementList.length) {
            //  $('#cmbvolume .helperButton').trigger('click');
            this.dashboardservice.GetSupplementlotdetails(this.SupplementList[0].batchid).subscribe(res => {
                if (res !== undefined && res !== null && res.body[0] !== undefined && res.body[0] !== null) {
                    if (JSON.parse(res.body[0]).length > 0) {
                        this.joblist_filter1 = JSON.parse(res.body[0]);
                        this.jobFilterlist = [];
                        const supplementlotids = [];
                        this.joblist_filter1.forEach(element => {
                            //console.log(Object.values(element));
                            supplementlotids.push(element.lotid);
                        });
                        this.supplementlotids = supplementlotids;
                        //console.log('this.joblist_filter1 SupplementClick=', this.joblist_filter1);
                        //console.log('this.joblist_filter1 supplementlotids=', this.supplementlotids);
                        if (this.joblist_filter1.length > 0) {
                            this.jobstatuslist.forEach(val => {
                                const col = val.statusdetails.filter(item => {
                                    return this.joblist_filter1.includes(item.lotid);
                                });
                                if ((col !== undefined) && (col.length > 0)) {
                                    this.jobFilterlist.push(val);
                                }
                            });
                        }
                    }
                }
            }, error => {
                this.toastr.error('Error in loading job details');
            });
        } else { this.jobFilterlist = this.jobstatuslist; }
    }

    onJobDeSelect(evt){
        //this.toastr.info('No data found!');
        //this.joblist = 
        this.joblist_filter = [];

    }
    /* ondateValueChange(value: Date): void {
       var startdate = value[0];
       var enddate = value[1];
      // let current_date =this.datepipe.transform(date, 'dd-MMM-yyyy');
       console.log("start="+this.datepipe.transform(value[0], 'dd-MMM-yyyy'))
       console.log("end="+this.datepipe.transform(value[1], 'dd-MMM-yyyy'))
    } */

    ondateValueChange(value: Date): void {
        var startDate = new Date(value[0]);
        var endDate = new Date(value[1]);
        var result = this.org_job_list.filter(a => {
        var date = new Date(a.createddate);
         return (date >= startDate && date <= endDate);
       });
       if(result.length>0){
         this.joblist=result;   
         this.selectstagedetails(this.joblist[0].jobid, this.joblist[0].title);
         this.jobtitle= this.joblist.filter(x => x.jobid == this.joblist[0].jobid);
       }else{
        this.jobtitle='';
       // this.toastr.info('No data found!');
        this.selectstagedetails(1, '');
       // this.joblist = this.joblist_filter = [];
       }
     }

     Navigatereport(){
        var params={mode_p:'landing',prodefid:+localStorage.getItem('prdefid') };
        window.open( '/reports/runtimereport', '_blank');
       // window.open( `/customer/edit/${customer_id_param}`, '_blank');
       // this.toastr.error('hi');
        //this.router.navigate(['/reports/runtimereport'],
        // { queryParams: {mode_p:'landing',prodefid:+localStorage.getItem('prdefid') } });

        // this.router.navigate(['/reports/runtimereport'],
        // { queryParams: {mode_p:'landing',prodefid:+localStorage.getItem('prdefid') } }).then(result => {  window.open(link, '_blank'); });
     }
}
