import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { forkJoin } from 'rxjs';
import { CommonFunctionService } from 'src/app/core/common/common-function.service';
import { API } from 'src/app/core/services/api.service';

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

  Extranet_Hotel_Id;
  Result;
  AllMealPlan;
  AllMealAddon;
  MealPlan;
  MealAddon;
  cols;
  constructor(public activateRoute:ActivatedRoute,private router:Router, public apiserv:API,public _commonService:CommonFunctionService) { }

  ngOnInit(): void {
    this.Extranet_Hotel_Id = this.activateRoute.snapshot.paramMap.get('Hotel_Eaxtranet_Id') as string;
    this.cols = [
      { field: 'addon_name', header: 'Meal Add-On Name' },
      { field: 'meal_plan_name', header: 'Meal Plan' },
      { field: 'currency', header: 'Currency' },
      { field: 'adult_rate', header: 'Adult Rate' },
      { field: 'currency', header: 'Currency' },
      { field: 'is_paid_child', header: 'Paid Child' },
      { field: 'status', header: 'Status' },
      { field: 'extranet_meal_addon_id', header: 'Meal Add-On Id' },
      { field: 'created_by', header: 'Created By' },
      { field: 'Custom_created_on', header: 'Created On' },
      { field: 'updated_by', header: 'Updated By' },
      { field: 'Custom_updated_on', header: 'Updated On' },
    ];
    this.getAllData();
  }

  Search(){
    let ReObj = {
      addon_name:this.MealAddon ? this.MealAddon : '',
      extranet_meal_plan_id:this.MealPlan ? this.MealPlan : '',
      hotel_id:this.Extranet_Hotel_Id
    }
    this.apiserv.Post('/ExtranetMealAddon/GetAllExtranetMealAddon',ReObj).subscribe(res=>{
      if (res) {
        this.Result = res.data ? res.data : [];
        if (res.data) {
          this.Result.forEach(e => {
            e['status'] = e.is_active ? 'Active' : 'Deactive';
            e['Custom_updated_on'] = this._commonService.convertDateWithTimeToShow(e.updated_on);
            e['Custom_created_on'] = e['created_on'] ?  this._commonService.convertDateWithTimeToShow(e.created_on) : e['created_on'];
          });
          this.Result.sort((a: any, b: any) => {
            // Sort by status: Active first, then Deactive
            if (a.status !== b.status) {
                return a.status === 'Active' ? -1 : 1;
            }
    
            // If both are Active, sort by activeDate
            if (a.status === 'Active') {
                return new Date(b.updated_on).getTime() - new Date(a.updated_on).getTime();
            }
    
            // If both are Deactive, sort by deactiveDate
            return new Date(b.updated_on).getTime() - new Date(a.updated_on).getTime();
          });
        }
      }
    })
  }
  getAllData() {
    forkJoin(
      this.GetAllMealPlan(),
      this.GetAllMealAddon(),
    ).subscribe(result => {
      if (result && result.length > 0) {
          this.AllMealPlan = result[0].data;
          this.AllMealAddon = result[1].data;
      }
    })
  }
  GetAllMealPlan(){
    return this.apiserv.Get('/ExtranetMealAddon/GetAllExtranetMeal/EN')
  }
  GetAllMealAddon(){
    return this.apiserv.Get('/ExtranetMealAddon/GetAllExtranetMealAddonByOrg/'+ this.Extranet_Hotel_Id)
  }
  updateStatus(id, val) {
    let obj = {
      Id: id,
      Status: val ? false : true,
    };
    this.apiserv.Post('/ExtranetMealAddon/UpdateExtranetMealAddOnStatus', obj).subscribe(res => {
      if (res) {
        this._commonService.showMessage(res['status'], res['message']);
        res['status'] ? this.Search() : '';
      }
    });
  }
  Edit(Id){
    this.router.navigateByUrl(`Services/Hotel/HotelManage/HotelMealEdit/${this.Extranet_Hotel_Id}/${Id}`);
  }
  Reset(){
    this.MealAddon = undefined;
    this.MealPlan = undefined;
  }
  btnClick() {
    this.router.navigateByUrl("Services/FlightInventory/Add");
   }
}
