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 {  FormBuilder, FormControl, FormGroup, 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-meal-add-on',
  templateUrl: './hotel-update-meal-add-on.component.html',
})
export class HotelUpdateMealAddOnComponent implements OnInit {

  Extranet_Hotel_Id;
  Edit_Id;
  AllMeal;
  MealUpdateForm:FormGroup;
  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.MealUpdateForm = this.fb.group({
      is_meal_addon_applicable:false
    })
    this.GetAllMeal();
    this.GetById();
  }
  GetAllMeal(){
    this.apiserv.Get('/ExtranetMealAddon/GetAllExtranetMealAddonByOrg/'+ this.Extranet_Hotel_Id).subscribe(resp=>{
      if(resp && resp.data){
        this.AllMeal = resp.data;
      }
    })
  }
  MealHideShow(check){
    if(check){
      this.MealUpdateForm.addControl('extranet_meal_addon_id',new FormControl('',Validators.required))
    }else {
      this.MealUpdateForm.removeControl('extranet_meal_addon_id');
    }
  }
  GetById(){
    this.apiserv.Get('/ExtranetRoomContract/GetExtranetRoomContractById/'+ this.Edit_Id).subscribe(res=>{
      if(res && res.data){
        this.MealUpdateForm.get('is_meal_addon_applicable').setValue(res.data.is_meal_addon_applicable);
        if(res.data.is_meal_addon_applicable){
          this.MealHideShow(res.data.is_meal_addon_applicable);
          this.MealUpdateForm.get('extranet_meal_addon_id').setValue((res.data.extranet_meal_addon_id.split(',')));

        }
      }
    })
  }
  BackButton(){
    this.hotelmanageserv.Active_tab = 'RoomContract';
    this.router.navigateByUrl(`Services/Hotel/HotelManage/${this.Extranet_Hotel_Id}`);
  }
  Save(){
    if (this.MealUpdateForm.status == "INVALID") {
      this.validserv.showValidationsMsg(this.MealUpdateForm);
      setTimeout(() => {
        this.validserv.scrollToError();
      }, 50);
      return;
    }
    let val = this.MealUpdateForm.getRawValue();
    let reqObj = {
        extranet_contract_id : this.Edit_Id,
        is_meal_addon_applicable : val.is_meal_addon_applicable,
        extranet_meal_addon_id : val.extranet_meal_addon_id ? val.extranet_meal_addon_id.join(',') : '',
      }
    this.apiserv.Post('/ExtranetRoomContract/UpdateExtranetMealAddOn',reqObj).subscribe(resp=>{
      if(resp){
        this.commonMasterSer.showMessage(resp.status,resp.message);
        resp.status ? this.BackButton() : '';
      }
    })
  }

}
