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

@Component({
  selector: 'minimum-stay-view',
  templateUrl: './minimum-stay-view.component.html',
})
export class MinimumStayViewComponent implements OnInit{
  Extranet_Hotel_Id;
  AllRoomsData;
  Room_Id;
  Result;
  cols;
  constructor(
    public activateRoute:ActivatedRoute,
    public apiserv:API,
    public _commonService:CommonFunctionService,
    public router:Router
  ) { }

  ngOnInit(): void {
    this.Extranet_Hotel_Id = this.activateRoute.snapshot.paramMap.get('Hotel_Eaxtranet_Id') as string;
    this.GetAllRooms()
    this.cols = [
      { field: 'extranet_room_id', header: 'Room Id' },
      { field: 'room_name', header: 'Room Name' },
      { field: 'date_range_arr', header: 'Minimum Stay Date Range' },
      { field: 'created_by', header: 'Created By' },
      { field: 'Custom_updated_on', header: 'Created On' },
      { field: 'updated_by', header: 'Updated By' },
      { field: 'Custom_updated_on', header: 'Updated On' },
    ];
  }
  GetAllRooms(){
    let ReqObj = {
      extranet_hotel_id : this.Extranet_Hotel_Id,
      extranet_room_id : '',
      room_name : '',
      room_view_name : '',
    }
    this.apiserv.Post('/ExtranetRoomContract/GetAllExtranetHotelRoomName',ReqObj).subscribe(res=>{
      this.AllRoomsData = res ? res.data : [];
    })
  }
  
  Search(){
    let ReqObj = {
      extranet_room_id:this.Room_Id ? this.Room_Id : '',
      extranet_hotel_id:this.Extranet_Hotel_Id
    }
    this.apiserv.Post('/ExtranetMinStay/GetAllExtranetRoomMinStay',ReqObj).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['date_range_arr'] = e['date_ranges'] ? JSON.parse(e['date_ranges']) : [];
            if(typeof(e['date_range_arr']) != 'string'){
              e.date_range_arr.forEach((data,ind)=>{
                e.date_range_arr[ind] =  moment(data.date_from).format('DD MMM YYYY') + ' - ' + moment(data.date_to).format('DD MMM YYYY');
              })
            }
            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();
          });
        }
      }
    })
  }

  Edit(Id){
  this.router.navigateByUrl(`Services/Hotel/HotelManage/EditMinStay/${this.Extranet_Hotel_Id}/${Id}`);
  }

}
