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

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

  @Input() coEditDet: any;
  @Output() closeCoForm = new EventEmitter<string>();
  @Output() closeCoJrForm = new EventEmitter<any>();
  newCoFormdet: any = {};
  reqData: any;
  rex = /^[a-zA-Z ]*$/;
  isError: boolean = false;
  errorMsg: string;
  UpdateBtn: boolean = false;

  constructor(protected localStorageService: LocalStorageService, private AuthService: AuthService, private toastr: ToastsManager) {
    this.reqData = this.localStorageService.get('userInfo');
   }

  ngOnInit() {
    if(this.coEditDet){
      this.newCoFormdet.collegeName = this.coEditDet.collegeName;
      this.newCoFormdet.city = this.coEditDet.city;
      this.UpdateBtn = true;
    }
  }

  cancelForm(){
    this.newCoFormdet = {};
    this.closeCoForm.emit('true');
    this.isError = false;
  }

  createNewCo(){
    this.isError = false;
    this.errorMsg = '';
    if(this.reqData.emailId){
      this.newCoFormdet.loggedInUser = this.reqData.emailId;
    }
    if(this.reqData.email){
      this.newCoFormdet.loggedInUser =  this.reqData.email;
    }
    if(!this.newCoFormdet.collegeName || !this.rex.test(this.newCoFormdet.collegeName)){
      this.isError = true;
      this.errorMsg = "Please provide a valid College Name.";
    }
    if(!this.newCoFormdet.city || !this.rex.test(this.newCoFormdet.city)){
      this.isError = true;
      this.errorMsg = "Please provide a valid City Name.";
    }
    if(!this.isError){
      if(this.UpdateBtn == true){
        this.newCoFormdet.id = this.coEditDet.id;
        this.AuthService.updateCollege(this.newCoFormdet).then(resp =>{
          if (resp.status == 'error' || resp.errorBean) {
            this.errorMsg = resp.errorBean.message;
            this.toastr.error(resp.status, resp.message);
          }
          else {
            this.toastr.success('College Info Updated successfully');
            this.cancelForm();
          }
        }).catch(err => {
          console.log(err, "Error");
          this.toastr.error(err.error);
        })
      }
      else{
        this.newCoFormdet.status = 'Active';
        this.AuthService.addNewCollege(this.newCoFormdet).then(resp =>{
          console.log(resp);
          if (resp.status == 'error') {
            this.errorMsg = resp.message;
            this.toastr.error(this.errorMsg);
          }
          else {
            this.toastr.success('College Info Created successfully');
            this.cancelForm();
          }
        }).catch(err => {
          console.log(err, "Error");
          this.toastr.error(err.error);
        })
      }
    }
  }
  collegeStat(e){
    console.log(this.newCoFormdet.status);
    if(e.checked == true){
      this.newCoFormdet.status = 'Active';
    }
    else{
      this.newCoFormdet.status = 'Inactive';
    }
  }
}
