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

@Component({
  selector: 'app-candidate-transfer',
  templateUrl: './candidate-transfer.component.html',
  styleUrls: ['./candidate-transfer.component.css']
})
export class CandidateTransferComponent implements OnInit {

  searchForm: FormGroup;
  img_path = environment.imageUrl+'/assets/img/admin/';
  expandImg = this.img_path +  'right_arrow.png';
  closeImg = this.img_path +  'exit_icon.png';
  selectedFilter = 2;
  searchBtnEnabled = false;
  public result: any;
  expendDiv = false
  candidateDetails : any

  @Input() locationId: string ='' ;
  user = {
    fullName: 'Tom Potter',  email: 'dahlia_ruecker@hotmail.com',
    src: this.img_path + 'profileSample.png', candidateId: '456592',
    location: 'New Jersey'
  };

  prefs = [
    {name: 'Option 1'},
    {name: 'Option 2'}
  ];

  isRow : any;
  errMsg = '';
  locations = [];
  candidateList = [];
  currentPageData: { title: string, back: string};

  constructor(private fb: FormBuilder,
              private navBarService: NavBarService,
              private AuthService: AuthService,
              private toastr: ToastsManager) {

                this.currentPageData =   { title: 'Candidate Transfer', back: 'arrow_back' };
    this.getAllLocation();

  }

  ngOnInit() {
    this.errMsg = ''
    this.navBarService.setCurrentPage(7);
    this.searchForm = this.fb.group({
      firstName: ['', Validators.required],
      lastName: ['', Validators.required],
      email: ['', Validators.required],
      employeeNo: ['', Validators.required]
    });
  }

  applyFilter(event, newValue) {
    console.log(newValue);
    this.selectedFilter = newValue;
  }

  over(index){

    this.isRow = index
  }


  onSubmit(value: string): void {

   // this.searchBtnEnabled = true;
    console.log('you submitted value: ', value);

    this.errMsg = ''
    if (value['firstName'] == '' && value['lastName'] == '' && value['email'] == '' && value['employeeNo'] == '' ){
      this.errMsg = "Please enter valid details";
    }else {
      this.AuthService.adminSearchUser(value).then(resp => {

        console.log(resp, 'response', resp.status)

        if (resp.status == 'error') {

          console.log(resp.message)
        }
        else {
          if (resp.error == null) {

            this.candidateList = resp.userList
            console.log(this.candidateList)
            this.searchBtnEnabled = true

          }
          else {

          }
        }


      }).catch(err => {
        console.log(err, "Error")

      })
    }
  }

  nominate(value: boolean): void {
    this.searchBtnEnabled = true;
    console.log('you submitted value: ', value);
  }

  Number(id){
    return Number(id)
  }

  transfer (value){

    console.log('you submitted value: ', value);

    this.AuthService.getTransferCandidate(value).then(resp => {

      console.log(resp,'response')

      if(resp.status == 'error'){

        console.log(resp.message)
      }
      else {
        if (resp.error == null) {

          this.toastr.success('Candidate transfer successfully');
          this.navBarService.setCurrentPage(7);
          this.searchForm = this.fb.group({
            firstName: ['', Validators.required],
            lastName: ['', Validators.required],
            email: ['', Validators.required],
            employeeNo: ['', Validators.required]
          });
          this.searchBtnEnabled = false


        }
        else {

        }
      }


    }).catch(err => {

    })
  }



  getAllLocation(){


    this.AuthService.getAllLocations().then(resp =>{

      console.log(resp,'getAllLocations')

      this.locations = resp.pgLocationList


    }).catch(err =>{

      console.log(err)
    })

  }

}

