import { Component, OnInit } from '@angular/core';
import { JobmasterService } from 'src/app/jobconfiguration/jobmaster/jobmaster.service';
import { ToastrService } from 'ngx-toastr';
import { NgxSpinnerService } from 'ngx-spinner';
import{ManualarchivejobService} from 'src/app/jobconfiguration/manualarchivejob/manualarchievejob.service'
@Component({
  selector: 'app-manualarchivejob',
  templateUrl: './manualarchivejob.component.html',
  styleUrls: ['./manualarchivejob.component.css']
})
export class ManualarchivejobComponent implements OnInit {
  jobProductTypeList: any[];
  _ddlProjecttypeSettings: {};
  projectdefinitionid:number;
  jobdetails_list:any[]=[];
  userid: string = localStorage.getItem('userid');
  p = 1; 
  itemsPerPage = 10;
  public searchText: string;
  jobselexist =false;
  _userselectedItems: any[] = [];
  constructor( private jobmasterservice: JobmasterService,
    private toastr: ToastrService,
    private SpinnerService: NgxSpinnerService,
    private manualarchieve :ManualarchivejobService
    ) { }

  ngOnInit(): void {
    this.loadProductType();
  }
  loadProductType() {
    this._ddlProjecttypeSettings = {
        singleSelection: true,
        idField: 'projectdefinitionid',
        textField: 'project_subtitle',
        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.projectdefinitionid=this.jobProductTypeList[0].projectdefinitionid;
                 this._userselectedItems=this.jobProductTypeList.filter(x=>x.projectdefinitionid == this.jobProductTypeList[0].projectdefinitionid);
                 this.getjobdetails(this.projectdefinitionid);
                
                }
        }
    }, error => { this.toastr.error('Error while getting Workflow data'); });
}
onItemSelect(item: any) {
this.projectdefinitionid=item.projectdefinitionid;
this.getjobdetails(this.projectdefinitionid);
}
onItemDeSelect(item: any) {
  this.jobdetails_list = [];
}
getjobdetails(pdid){
  this.SpinnerService.show();
  this.jobdetails_list = [];
  const input=[];
  input.push({
    p_projectdefinitionid: pdid,
    userid:Number(this.userid)
  });
  this.manualarchieve.GetJobTitle(input[0]).subscribe(res=>{
    this.SpinnerService.hide();
    if(res.body!=null && res.body != undefined && res.body[0] != undefined && res.body[0]!=null){
    let result=JSON.parse(res.body);
    result.forEach(element => {
      element.insert_p=0;
      this.jobdetails_list.push(element);
    });
  }
}, error => {  this.SpinnerService.hide();this.toastr.error('Error while getting job data'); });

}
selectjob(data,evt){
  var checked_type=evt.target.checked;
  var cheked;
  if(checked_type) {cheked=1;}
  else {cheked=0; }
if(checked_type){
  var sel_job_count=0;
  this.jobdetails_list.forEach(element => { 
    if(element.insert_p ===1){
       sel_job_count= sel_job_count+1;
    }
    });
    if(sel_job_count >= 10){
      this.jobselexist=true;
      this.toastr.error('Kindly select minimum 10 jobs.');
      return;
    }
  var index=this.jobdetails_list.findIndex(p => p.jobid == data.jobid);
  this.jobdetails_list[index]['insert_p']=cheked;
}else{
  var index=this.jobdetails_list.findIndex(p => p.jobid == data.jobid);
  if(this.jobdetails_list[index]['insert_p']==1){
    this.jobdetails_list[index]['insert_p']=0;
  }else{
    this.jobselexist=false; 
  }
}
}
  archieve_job(){
let selected_job='';
this.jobdetails_list.forEach(element => {
  if(element.insert_p==1){
if(selected_job == ''){
  selected_job=element.jobid.toString();
}else{
  selected_job=selected_job+","+element.jobid.toString();
}
}
});
if(selected_job==''){
  this.toastr.error('Kindly select atleast one job.');
  return;
}
this.SpinnerService.show();
const input=[];
input.push({
  jobid: selected_job,
  option:'manual'
});
this.manualarchieve.Jobarchieve(input[0]).subscribe(res=>{
  this.SpinnerService.hide();
  if(res.body[0].result=="Done"){ 
    this.toastr.success('Successfully archived selected jobs');
    this.getjobdetails(this.projectdefinitionid);
  }else{
  this.toastr.error('Error while doing this operation');
  }
}, error => {  this.SpinnerService.hide();this.toastr.error('Error while doing this operation'); });
  }
}
