import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
// import { InnovationHttpService } from 'app/services/innovation/innovation-http.service';
import { EmployeeHttpService } from 'app/services/employee/employee-http.service';
import { LocalStorageService } from 'angular-2-local-storage';
import { AuthService } from 'app/services/auth.service';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';

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

  errMsg= '';
  invCats: any = [];
  invRevs: any = [];
  newIrFormdet: any = {};
  @Input() innoEditDet:any;
  empData: any;
  name: string;
  fullName: string;
  @Input() email= '';
  @Output() closeInnovEvent = new EventEmitter<string>();
  editOpt:boolean = false;
  rvArr:any = [];
  UpdateBtn:boolean = false;
  
  constructor(private AuthService: AuthService, private employeeService: EmployeeHttpService, protected localStorageService: LocalStorageService, private toastr: ToastsManager) {
// private innovationHttpService: InnovationHttpService,
    this.empData = this.localStorageService.get('userInfo');
    console.log("l9∂Inß u$rDt -- ",this.empData);
    this.name = '';
    this.fullName = '';
    if (window.location.origin === 'http://localhost:8080') {
      this.name = 'ishtiaq';
      this.fullName = 'ishtiaq mohammed m';
      this.empData = {
        accessToken: '',
        loginType: '',
        fullName: 'Ishtiaq Mohammed',
        firstName: 'ishtiaq',
        userInfoMstrId: 9,
        roleName: 'ADMIN',
        roles: [1],
        skills: [1, 2],
        location: 1,
        emailId: 'imohammed3049@altimetrik.com'
      };
    } else {
      if (this.empData['firstName']) {
        this.name = this.empData['firstName'];
        this.fullName = this.empData['fullName'];
        this.email = this.empData['email'];
      } else {
        this.name = '';
        this.fullName = '';
      }
    }
    this.empData = this.localStorageService.get('userInfo');
    if(!this.empData.emailId){
      this.empData.emailId = this.email;
    }
    this.newIrFormdet.loggedInUser = this.empData.emailId;
    
   }

   
  ngOnInit() {
    if(this.innoEditDet){
      // console.log(this.innoEditDet);
      this.editOpt = true;
      this.newIrFormdet.category = this.innoEditDet.category;
      this.innoEditDet.reviewers.forEach(element => {
        this.rvArr.push(element.userEmail);
      });
      this.newIrFormdet.reviewers = this.rvArr;
      // console.log(this.newIrFormdet.reviewers);
      this.UpdateBtn = true;
    }
    this.employeeService.getAllEmployees().subscribe(resp => {
        this.invRevs = resp;
        console.log('resp -- - ', this.invRevs);
    }, err => {
        console.log('Error on loading emplyees list');
    });
  }

  cancelForm(){
    this.errMsg = '';
    this.newIrFormdet = {};
    this.innoEditDet = {};
    this.closeInnovEvent.emit('true');
    this.UpdateBtn = false;
    // console.log("cancel clicked");
  }

  createInnvoC(){
    this.errMsg = '';
    if(!this.newIrFormdet.category){
      this.errMsg = 'Please enter the Category';
    }
    if(this.newIrFormdet.reviewers.length < 0){
      this.errMsg = 'Please select atleast one reviewer';
    }
    if(this.editOpt){
      this.newIrFormdet.categoryId = this.innoEditDet.categoryId;;
    }
    console.log(this.newIrFormdet);
    this.AuthService.createInnovationCat(this.newIrFormdet).then(res =>{
      console.log(res);
      if(res.error == null){
        if(this.editOpt){
        this.toastr.success('Category with reviewers updated successfully');
        }
        else{
          this.toastr.success('Category with reviewers added successfully');
        }
        this.cancelForm();
      }
      else{
        this.toastr.error(res.error, res.error.message);
      }
    })
  }

  editInnvoC(){
   this.createInnvoC();
  }
}
