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

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

  formTitle:string = 'Add Location';
  @Output() cancelLocEvent = new EventEmitter();
  isLocEdit: boolean;
  countries = [ {id: 1, info: 'INDIA'},{id: 2, info: 'USA'}, {id: 3, info: 'UK'} ];
  errMsg = '';
  newLocFormDets: any = {};
  @Input() locDetEdit: any;

  constructor(private AuthService: AuthService,
    private toastr: ToastsManager) { }

  ngOnInit() {
    this.newLocFormDets = {};
    if(this.locDetEdit){
      console.log(this.locDetEdit);
      this.newLocFormDets.address = this.locDetEdit.address;
      this.newLocFormDets.city = this.locDetEdit.city;
      this.newLocFormDets.state = this.locDetEdit.state;
      this.newLocFormDets.country = this.locDetEdit.country;
      this.newLocFormDets.locationId = this.locDetEdit.locationId;
      this.newLocFormDets.zipCode = this.locDetEdit.zipCode;
      this.isLocEdit = true;
      this.formTitle = "Edit Location";
    }
  }

  createLocation(){
    this.errMsg = ''
    if ( this.newLocFormDets.address == '' ||  this.newLocFormDets.address== 'undefined' ||  this.newLocFormDets.address == null ){
      this.errMsg = "Please enter valid address";
    }else if ( this.newLocFormDets.city == '' ||  this.newLocFormDets.city == 'undefined' ||  this.newLocFormDets.city == null ){
      this.errMsg = "Please enter valid city";
    }else if ( this.newLocFormDets.state == '' ||  this.newLocFormDets.state == 'undefined' ||  this.newLocFormDets.state == null ){
      this.errMsg = "Please enter valid state";
    }else  if ( this.newLocFormDets.zipCode == '' ||  this.newLocFormDets.zipCode == 'undefined' ||  this.newLocFormDets.zipCode == null ){
      this.errMsg = "Please enter valid zipCode";
    }else if ( this.newLocFormDets.country == 'Select'){
      this.errMsg = "Please select country";
    }else {
      this.AuthService.getAddLocation(this.newLocFormDets).then(resp => {
        console.log(resp, 'Response for getAddLocation')
        if ( resp.pgLocation == undefined) {
          this.toastr.error(resp.message);
        }
        else {
          console.log(resp, 'Response for getAddLocation in else')
          this.toastr.success("Location added successfully");
          this.newLocFormDets = {};
          this.cancelLocEvent.emit('true');
          }
        }).catch(err => {
        console.log(err, 'Error')
      })
    }
  }

  updateLocation(){
    this.errMsg = ''
    if ( this.newLocFormDets.address == '' ||  this.newLocFormDets.address== 'undefined' ||  this.newLocFormDets.address == null ){
      this.errMsg = "Please enter valid address";
    }else if ( this.newLocFormDets.city == '' ||  this.newLocFormDets.city == 'undefined' ||  this.newLocFormDets.city == null ){
      this.errMsg = "Please enter valid city";
    }else if ( this.newLocFormDets.state == '' ||  this.newLocFormDets.state == 'undefined' ||  this.newLocFormDets.state == null ){
      this.errMsg = "Please enter valid state";
    }else  if ( this.newLocFormDets.zipCode == '' ||  this.newLocFormDets.zipCode == 'undefined' ||  this.newLocFormDets.zipCode == null ){
      this.errMsg = "Please enter valid zipCode";
    }else if ( this.newLocFormDets.country == 'Select'){
      this.errMsg = "Please select country";
    }else {
      this.AuthService.getUpdateLocation(this.newLocFormDets).then(resp => {
        console.log(resp, 'Response for getAddLocation')

        if ((resp.status == 200 && resp.error == null) || (resp.error == null)) {
          this.toastr.success('Location Update successfully');
          this.newLocFormDets = {};
          this.isLocEdit = false;
          this.locDetEdit = {};
          this.cancelLocEvent.emit('true');          
        }
        else {
          console.log(resp, 'Response for getAddLocation');
          this.toastr.error(JSON.stringify(resp.error));
        }
      }).catch(err => {
        console.log(err, 'Error');
        this.toastr.error(JSON.stringify(err));
      })
    }
  }
  cancelForm(){
    this.cancelLocEvent.emit('true');
    }
}
