import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { Router } from '@angular/router';
import { MessageService } from 'primeng/api';
import { ValidationService } from 'src/app/core/common/validation.service';
import { API } from 'src/app/core/services/api.service';

@Component({
  selector: 'app-hotel-add',
  templateUrl: './hotel-add.component.html',
})
export class HotelAddComponent implements OnInit {

  constructor(private apiserv: API,public fb:FormBuilder, public validserv:ValidationService,public MsgService:MessageService,    private router: Router) { }
  HotelList;
  CurrencyList;
  timer;
  selectedRow;
  ddLoader = {
    hotel_list:false,
    currency_list:false
  };
  HotelDetailForm:FormGroup;

  ngOnInit(): void {
    this.HotelDetailForm = this.fb.group({
      hotel_code_mapping:this.validserv.required,
      currency:this.validserv.required,
      star_rating:'',
      country_code:'',
      city_code:'',
      city_name:'',
      country_name:''
    })
  }

  GetHotels(event){
      if (event.value.length >= 2) {
        let obj ={
          languageCode:'en',
          cityName:event.value,
          lattitude:'0',
          longitude:'0',
        }
        this.ddLoader['hotel_list'] = true;
        clearTimeout(this.timer);
        this.timer = setTimeout(() => {
          this.apiserv.AdminGoPost(`/Hotel/GetHotels`,obj).subscribe(res => {
            if (res) {
              this.HotelList = res.hotels;
              this.ddLoader['hotel_list'] = false;
            }
          });
        }, 500);
      }
  }
  GetCurrency(event){
      if (event.value.length >= 2) {
        let obj ={
          langcode:'en',
          query:event.value
        }
        this.ddLoader['currency_list'] = true;
        clearTimeout(this.timer);
        this.timer = setTimeout(() => {
          this.apiserv.AdminGoPost(`/MasterSearch/GetAllCurrency`,obj).subscribe(res => {
            if (res) {
              this.CurrencyList = res.Result;
              this.ddLoader['currency_list'] = false;
            }
          });
        }, 500);
      }
  }
  SetHotelDet(event){
    this.selectedRow = this.HotelList.find(row => row.hotelId === event);
    if(this.selectedRow){
      this.HotelDetailForm.get('country_name').setValue(this.selectedRow.countryName);
      this.HotelDetailForm.get('city_name').setValue(this.selectedRow.cityName);
      this.HotelDetailForm.get('city_code').setValue(this.selectedRow.cityCode);
      this.HotelDetailForm.get('country_code').setValue(this.selectedRow.countryCode);
      this.HotelDetailForm.get('star_rating').setValue(this.selectedRow.Rating);
    }
  }

  Save(){
    if (this.HotelDetailForm.status == "INVALID") {
      this.validserv.showValidationsMsg(this.HotelDetailForm);
      setTimeout(() => {
        this.validserv.scrollToError();
      }, 50);
      return;
    }
    let val = this.HotelDetailForm.getRawValue();
    let ReqObj={
      hotel_code_mapping:val.hotel_code_mapping,
      currency:val.currency,
      star_rating:val.star_rating ? parseInt(val.star_rating) : 0,
      country_code:val.country_code ? val.country_code : '',
      city_code:val.city_code ? val.city_code : ''
    }
    this.apiserv.Post('/ExtranetHotel/SaveExtranetHotel',ReqObj).subscribe(data=>{
      if(data){
        this.MsgService.add({
          severity: data.status ? "success" : "error",
          detail: data.message,
        });
        data.status ? this.backbutton() : '';
      }
    })
  }

  backbutton(){
    this.router.navigateByUrl("Services/Hotel/HotelSearch");
  }
  backClick() {
    this.router.navigateByUrl("Services/Hotel/HotelSearch");
   }
}
