import { Component, OnInit, 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-coach-nomination',
  templateUrl: './coach-nomination.component.html',
  styleUrls: ['./coach-nomination.component.css']
})
export class CoachNominationComponent implements OnInit {
    searchForm: FormGroup;
    img_path = environment.imageUrl+'/assets/img/admin/';
    selectedFilter = 2;
    searchBtnEnabled = false;
    public result: any;
    candidateDetails:any
    skills = []
    candidateList = []
    dismiss = 'none'
    errMsg = 'test'

    user = { fullName: 'Oscar Briggs',  email: 'abcd@gmail.com', src: this.img_path + 'profileSample.png', employeeId: '345340'};
    filters = [
      { name: 'Location', pWidth: 'auto' },
      { name: 'Role', pWidth: '40px' },
      { name: 'All', pWidth: '30px' }
    ];

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

    ngOnInit() {
      this.errMsg = ''
      this.navBarService.setCurrentPage(9);
      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;
    }

    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
              this.candidateDetails = resp.userList[0]

              if(resp.userList.length !== 0) {
                if (resp.userList[0].userInfoMstr.pgUserSkillMapping.length == 0) {

                }
                else {
                  this.skills.push(resp.userList[0].userInfoMstr.pgUserSkillMapping[0].pgSkillTrackDtls.pgSkillTrackMstr.skillTrackMstrId)
                }
                console.log(this.candidateList)
                this.searchBtnEnabled = true
              }
            }
            else {

            }
          }


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

    nominate(value) {

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

      this.AuthService.getnominateCoach(value,{skills:[1]}).then(resp => {

        console.log(resp,'response')
        if (resp.status == 'error'){
          this.toastr.warning(resp.message);
        }
        else {

          if (resp.error == null) {
            this.dismiss = 'modal'
            this.toastr.success('Coach nominate successfully');
            this.searchBtnEnabled = false;

          }
          else {


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

      })

    }
}
