import { Component, OnInit } from '@angular/core';
import { AuthService } from '../../../../services/auth.service';
import { FormBuilder, FormGroup, Validators, AbstractControl } from '@angular/forms';
import { ToastsManager } from 'ng2-toastr/ng2-toastr'; 
import { environment } from '../../../../../environments/environment.dev';

@Component({
  selector: 'app-job-requirement',
  templateUrl: './job-requirement.component.html',
  styleUrls: ['./job-requirement.component.scss']
})
export class JobRequirementComponent implements OnInit {

  jrList: boolean = true;
  img_path = environment.imageUrl+'/assets/img/Taflow/';
  UpdateBtn:boolean = false
 
  jobRequirments = [];
  errMsg = '';
  jrTilesData: any;
  jrTilesIndex: number;
  addNewJobRequirement: boolean;
  jrEditDet: any;
  idToDelete: number;
  deletedIdFromMstr: number;
  deletedFromMstrName: string;
  dialogData: any;

  constructor( private AuthService: AuthService, private toastr: ToastsManager) { 
    this.getJobRequirements();
  }

  ngOnInit() {
    this.dialogData = { title: 'Job Requirement Delete Confirmation', text: 'Would you like to delete this Job Requirement  from the list?' };
  }

  getJobRequirements(){
    this.AuthService.getAllJobRequirements().then(resp => {
      this.jobRequirments = resp.listResponse;
      // console.log(this.jobRequirments);
    }).catch(err => {
      console.log(err)
    })
  }

  addJobRequirement(){
    this.jrList = false;
    this.addNewJobRequirement = true;
  }

  formCancelled(e) {
    this.jrList = true;
    this.getJobRequirements();
  }

  editJobrequirement(e){
    this.jrList = false;
    this.addNewJobRequirement = false;
    this.jrEditDet = e;
  }

  deleteJobrequirement(e){
    console.log('Deleting item: ', JSON.stringify(e));
    if (e.isDelete) {
      document.getElementById("delJrConfModal").click();
      //this.confirmDelete
    }
    this.idToDelete = e.id;
    this.deletedIdFromMstr = e.skillTrackMstrId;
    this.deletedFromMstrName = e.skillTrackName;
  }
  confirmJrDelete() {
    console.log(this.idToDelete);
    this.AuthService.getDeleteJobRequirementId(this.idToDelete).subscribe((resp:any) => {
      resp =  JSON.parse(resp._body);
      if (resp.status == 'error') {
        this.toastr.error(resp.message);
      } else {
        this.toastr.success(resp.successMessage);
        this.getJobRequirements();
        // this.viewDetailAssment(this.deletedIdFromMstr, this.deletedFromMstrName)
      }
    })
  }
}
