import { Component, OnInit } from '@angular/core';
import { NavBarService } from '../../../admin.service';
import { AuthService } from '../../../../services/auth.service';
import { FormBuilder, FormGroup, Validators, AbstractControl } from '@angular/forms';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';
import  _ from 'lodash';
import { Pipe, PipeTransform } from '@angular/core';
import { environment } from '../../../../../environments/environment.dev';

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

  locations = [];
  topRowCountries: any;
  locForm: boolean = false;
  img_path = environment.imageUrl+'/assets/img/Taflow/';
  dialogData: any;
  locationId: number;
  UpdateBtn: boolean;
  addNewLocation: boolean = true;
  locDetEdit: any;
  myColor = ["#009587","#01BCD5", "#663AB7","#E6A9E6", "#3F51B5", "#F8A9CA", "#6FDCDE", "#F2D2CD", "#F2CCDB", "#BFE2F5", "#D8D0F7", "#6FDCDE", "#EC8ECC"];

  constructor(private AuthService: AuthService,
    private toastr: ToastsManager) { 
      this.getAllLocation()
    }

  ngOnInit() {
    this.dialogData = { title: 'Delete Location Confirmation', text: 'Would you like to delete this Location from the list?'}
  }

  getAllLocation(){
    this.AuthService.getAllLocations().then(resp =>{
    this.locations = _.orderBy(resp.pgLocationList,['locationId'],['country']);
    console.log(this.locations);
    let topTiles = [];
    let countriesAvb= [];
    this.locations.forEach(country => {
      countriesAvb.push({country:country.country, address:[]})
    });
    countriesAvb= _.uniqBy(countriesAvb,'country');
    this.topRowCountries = countriesAvb;
    this.topRowCountries.forEach(locAdd =>{
      this.locations.forEach(country => {
        if(locAdd.country == country.country){
          locAdd['address'].push(country)
      }
      });
    })
    console.log(this.topRowCountries);
    }).catch(err =>{
      console.log(err)
    })
  }

  addLocation(){
    this.locForm = true;
    this.addNewLocation = true;
  }
  formCancelled(e){
    this.locForm = false;
    this.getAllLocation();
  }

  deleteLocation(e){
    console.log('Deleting Location: ', JSON.stringify(e));
    if(e.isDelete){
      document.getElementById("delLocModal").click();
      //this.confirmDelete
    }
    this.locationId = e.id
  }
  confirmDelete(){
    console.log(this.locationId);
    this.AuthService.getDeleteLocation(this.locationId).then(resp => {

      if (resp.status == 'error') {
        this.toastr.error(resp.message);
      } else {
        this.toastr.success(resp.successMessage);
        //this.tableData.rows.splice(index, 1);
        this.getAllLocation();
      }
    }).catch(err => {
      console.log(err)
    })
  }

  editLocationDet(e){
    this.UpdateBtn = true;
    this.addNewLocation = false;
    this.locForm = true; 
   this.locDetEdit = e;
  }

  showSearch(ctry, list){
    console.log("ctry", ctry);
    console.log("list", list);

  }
}
