import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { API } from 'src/app/core/services/api.service';
import { ValidationService } from 'src/app/core/common/validation.service';
import { AbstractControl, FormArray, FormBuilder, FormControl, FormGroup, ValidatorFn, Validators } from '@angular/forms';
import { CommonFunctionService } from 'src/app/core/common/common-function.service';
import { ManageHotelService } from 'src/app/core/services/HotelServices/manage-hotel.service';

@Component({
  selector: 'hotel-update-nationality',
  templateUrl: './hotel-update-nationality.component.html',
})
export class HotelUpdateNationalityComponent implements OnInit {

  Extranet_Hotel_Id;
  Edit_Id;
  NationalityForm:FormGroup;
  ddLoader = {
    Country:false,
    Residence:false,
  };
  CountryList;
  timer;
  Nationality_type = [{Label:'All',Value:'ALL'},{Label:'Specific',Value:'SPECIFIC'}];
  Residence_type = [{Label:'All',Value:'ALL'},{Label:'Specific',Value:'SPECIFIC'}];

  constructor(private activateRoute:ActivatedRoute,private apiserv:API,public validserv:ValidationService,public fb:FormBuilder,public commonMasterSer:CommonFunctionService,
    private hotelmanageserv:ManageHotelService,private router:Router) { }

  ngOnInit(): void {
    this.Extranet_Hotel_Id = this.activateRoute.snapshot.paramMap.get('Hotel_Eaxtranet_Id') as string;
    this.Edit_Id = this.activateRoute.snapshot.paramMap.get('RuleId') as string;
    this.NationalityForm = this.fb.group({
      nationality_type:'',
      nationality_applicable_type:'',
      residence_type:'',
      residence_applicable_type:''
    });
    this.GetById();
  }
  GetById(){
    this.apiserv.Get('/ExtranetRoomContract/GetExtranetRoomContractById/'+ this.Edit_Id).subscribe(res=>{
      if(res && res.data){
        
        this.NationalityForm.get('nationality_type').setValue(res.data.nationality_type);
        this.NationalityForm.get('nationality_applicable_type').setValue(res.data.nationality_applicable_type);
        if(res.data.nationality_applicable_type == 'SPECIFIC'){
          this.NationalityShowhide(res.data.nationality_applicable_type);
          this.NationalityForm.get('nationality').setValue((res.data.nationality.split(',')));
          this.CountryList = res.data.country ? JSON.parse(res.data.country) : [];
        }
        this.NationalityForm.get('residence_type').setValue(res.data.residence_type);
        this.NationalityForm.get('residence_applicable_type').setValue(res.data.residence_applicable_type);
        if(res.data.residence_applicable_type == 'SPECIFIC'){
          this.ResidenceShowhide(res.data.residence_applicable_type);
          this.NationalityForm.get('residence').setValue((res.data.residence.split(',')));
          this.CountryList = res.data.country ? JSON.parse(res.data.country) : [];
        }
      }
    })
  }
  NationalityShowhide(event){
    if(event == 'SPECIFIC'){
      this.NationalityForm.addControl('nationality',new FormControl('',Validators.required))
    }else{
      this.NationalityForm.removeControl('nationality');
    }
  }
  NationalityTypeCheck(event){
    if(event == 'INCLUDE'){
      this.Nationality_type = [{Label:'All',Value:'ALL'},{Label:'Specific',Value:'SPECIFIC'}];
    }else{
      this.Nationality_type = [{Label:'Specific',Value:'SPECIFIC'}];
      if(this.NationalityForm.controls['nationality_applicable_type'].value == 'ALL'){
        this.NationalityForm.get('nationality_applicable_type').setValue('');
      }
    }
  }
  getCountryList(event,name) {
    if (event.value.length >= 2) {
      this.ddLoader[name] = true;
      clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        this.apiserv.AdminGet(`/MasterSearch/GetAllCountry/en/${event.value}`).subscribe(res => {
          if (res) {
            this.CountryList = res;
            this.ddLoader[name] = false;
          }
        });
      }, 500);
    }
  }
  Save(){
    if (this.NationalityForm.status == "INVALID") {
      this.validserv.showValidationsMsg(this.NationalityForm);
      setTimeout(() => {
        this.validserv.scrollToError();
      }, 50);
      return;
    }
    let val = this.NationalityForm.getRawValue();
    let reqObj = {
        extranet_contract_id : this.Edit_Id,
        nationality_type : val.nationality_type ?  val.nationality_type : '',
        nationality_applicable_type : val.nationality_applicable_type ? val.nationality_applicable_type : '',
        nationality : val.nationality ? val.nationality.join(',') : '',
        residence_type:val.residence_type,
        residence_applicable_type:val.residence_applicable_type,
        residence:val.residence_applicable_type == 'SPECIFIC' ? val.residence.join(',') : '',
      }
    this.apiserv.Post('/ExtranetRoomContract/UpdateExtranetNationality',reqObj).subscribe(resp=>{
      if(resp){
        this.commonMasterSer.showMessage(resp.status,resp.message);
        resp.status ? this.BackButton() : '';
      }
    })
  }
  BackButton(){
    this.hotelmanageserv.Active_tab = 'RoomContract';
    this.router.navigateByUrl(`Services/Hotel/HotelManage/${this.Extranet_Hotel_Id}`);
  }
  ResidenceTypeCheck(event){
    if(event == 'INCLUDE'){
      this.Residence_type = [{Label:'All',Value:'ALL'},{Label:'Specific',Value:'SPECIFIC'}];
    }else{
      this.Residence_type = [{Label:'Specific',Value:'SPECIFIC'}];
      if(this.NationalityForm.controls['residence_applicable_type'].value == 'ALL'){
        this.NationalityForm.get('residence_applicable_type').setValue('');
      }
    }
  }
  ResidenceShowhide(event){
    if(event == 'SPECIFIC'){
      this.NationalityForm.addControl('residence',new FormControl('',Validators.required))
    }else{
      this.NationalityForm.removeControl('residence');
    }
  }
}
