import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
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';

@Component({
  selector: 'app-location-form',
  templateUrl: './location-form.component.html',
  styleUrls: ['./location-form.component.css']
})
export class LocationFormComponent implements OnInit {
  private sub: any;
  userForm: FormGroup;
  prefs = [
    {name: 'INDIA'},
    {name: 'USA'}
  ];
  countries = [ {id: 1, info: 'INDIA'},{id: 2, info: 'USA'} ];
  editForm = false;
  formTitle = 'Add New Location';
  errMsg = '';
  constructor(
    private fb: FormBuilder,
    private navBarService: NavBarService,
    private router: Router,
    private route: ActivatedRoute,
    private AuthService: AuthService,
    private toastr: ToastsManager
  ) {
    this.userForm = this.fb.group({
      address: ['', Validators.required],
      city: ['', Validators.required],
      state: ['', Validators.required],
      zipCode: ['', Validators.required],
      locationId: ['', Validators.required],
      country : ['Select']
    });
   }

  ngOnInit() {
    this.navBarService.setCurrentPage(11);

    this.sub =  this.route.params.subscribe(params => {
      console.log('id ', params['id']);
      if (params['id'] !== undefined) {


        this.AuthService.getActiveLocationBy(params['id']).then(resp => {

          console.log(resp,'location Detailssss')

          var locationDtls = resp.pgLocationList[0]

          console.log(locationDtls);

          this.editForm = true;
          this.formTitle = 'Edit Location';
          this.userForm.setValue({
            address: locationDtls.address,
            city: locationDtls.city,
            state: locationDtls.state,
            zipCode: locationDtls.zipCode,
            country: locationDtls.country,
            locationId: locationDtls.locationId
          });


        }).catch(err => {

        })

      }
    });
  }

  onSubmit(value: string): void {
    console.log('you submitted value: ', value);

    this.errMsg = ''
    if (value['address'] == '' || value['address'] == 'undefined' || value['address']== null ){
      this.errMsg = "Please enter valid address";
    }else if (value['city'] == '' || value['city'] == 'undefined' || value['city']== null ){
      this.errMsg = "Please enter valid city";
    }else if (value['state'] == '' || value['state'] == 'undefined' || value['state']== null ){
      this.errMsg = "Please enter valid state";
    }else  if (value['zipCode'] == '' || value['zipCode'] == 'undefined' || value['zipCode']== null ){
      this.errMsg = "Please enter valid zipCode";
    }else if (value['country'] == 'Select'){
      this.errMsg = "Please select country";
    }else {


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

        console.log(resp, 'Response for getAddLocation')

        if (resp.error == null) {

          this.userForm = this.fb.group({
            address: ['', Validators.required],
            city: ['', Validators.required],
            state: ['', Validators.required],
            country: ['Select', Validators.required],
            zipCode: ['', Validators.required],

          });

          this.toastr.success('Location added successfully');

        }
        else {
          console.log(resp, 'Response for getAddLocation')

        }

      }).catch(err => {

        console.log(err, 'Error')

      })
    }
  }

  onEditSubmit(value: string): void {
    console.log('you submitted value: ', value);

    this.errMsg = ''
    if (value['address'] == '' || value['address'] == 'undefined' || value['address']== null ){
      this.errMsg = "Please enter valid address";
    }else if (value['city'] == '' || value['city'] == 'undefined' || value['city']== null ){
      this.errMsg = "Please enter valid city";
    }else if (value['state'] == '' || value['state'] == 'undefined' || value['state']== null ){
      this.errMsg = "Please enter valid state";
    }else  if (value['zipCode'] == '' || value['zipCode'] == 'undefined' || value['zipCode']== null ){
      this.errMsg = "Please enter valid zipCode";
    }else if (value['country'] == 'Select'){
      this.errMsg = "Please select country";
    }else {


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

        console.log(resp, 'Response for getAddLocation')

        if (resp.error == null) {

          this.userForm = this.fb.group({
            address: ['', Validators.required],
            city: ['', Validators.required],
            state: ['', Validators.required],
            country: ['Select', Validators.required],
            zipCode: ['', Validators.required],

          });

          this.router.navigate(['admin/location'])

          this.toastr.success('Location Update successfully');

        }
        else {
          console.log(resp, 'Response for getAddLocation')

        }

      }).catch(err => {

        console.log(err, 'Error')

      })
    }
  }
}
