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

@Component({
  selector: 'hotel-room',
  templateUrl: './hotel-room.component.html',
})
export class HotelRoomComponent implements OnInit {
  displayPosition;
  Extranet_Hotel_Id;
  Result;
  cols;
  Room_Name;
  AllRoomView;
  Clone_Room_data;
  Clone_Room_view;
  Room_View;
  Extra_bed;
  Free_Child;
  constructor(public activateRoute:ActivatedRoute,private router:Router, public apiserv:API,public _commonService:CommonFunctionService,
    public validserv:ValidationService, public fb:FormBuilder) { }

  ngOnInit(): void {
    this.Extranet_Hotel_Id = this.activateRoute.snapshot.paramMap.get('Hotel_Eaxtranet_Id') as string;
    this.cols = [
      { field: 'room_name', header: 'Room Name' },
      { field: 'room_view', header: 'Room View' },
      { field: 'max_occupancy_type', header: 'max Occupancy Type' },
      { field: "max_adult_occupancy", header: 'Max Adult Occupancy' },
      { field: 'max_child_occupancy', header: 'Max Child Occupancy' },
      { field: 'max_child_age', header: 'Max Child Age' },
      { field: 'max_total_occupancy', header: 'Max Total Occupancy' },
      { field: 'is_extra_bed_supported', header: 'Extrabed Supported' },
      { field: 'is_free_child', header: 'Free Child Allowed' },
      { field: 'extranet_room_id', header: 'Room 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.GetAllRoomView()
  }

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

  Search(){
    let ReObj = {
      is_free_child:this.Free_Child ? this.Free_Child : '' ,
      is_extra_bed_supported:this.Extra_bed ? this.Extra_bed : '',
      extranet_room_view_id:this.Room_View ? this.Room_View : '',
      room_name:this.Room_Name ? this.Room_Name : '',
      hotel_id:this.Extranet_Hotel_Id
    }
    this.apiserv.Post('/ExtranetHotelRoom/GetAllExtranetHotelRoom',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['is_extra_bed_supported'] = e.is_extra_bed_supported ? 'Yes' : 'No';
            e['is_free_child'] = e.is_free_child ? 'Yes' : 'No';
            if(e.max_occupancy_type){
              e['max_occupancy_type'] = e.max_occupancy_type == 'MAXOCCUPANCYCOMBINATIONS' ? 'Max Occupancy Combinations' : 'Max Total Occupancy';
            }
            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();
          });
        }
      }
    })
  }
  Clone(Id){
    this.router.navigateByUrl(`Services/Hotel/HotelManage/CloneRoom/${this.Extranet_Hotel_Id}/${Id}`);
  }
  updateStatus(id, val) {
    let obj = {
      Id: id,
      Status: val ? false : true,
    };
    this.apiserv.Post('/ExtranetHotelRoom/UpdateExtranetHotelRoomStatus', obj).subscribe(res => {
      if (res) {
        this._commonService.showMessage(res['status'], res['message']);
        res['status'] ? this.Search() : '';
      }
    });
  }
  GetAllRoomView(){
    this.apiserv.Get('/ExtranetHotelRoom/GetAllExtranetHotelRoomView').subscribe(res=>{
      if(res && res.data){
        this.AllRoomView = res.data ? res.data : undefined;
      }
    })
  }
  Reset(){
    this.Room_Name = undefined;
    this.Room_View = undefined;
    this.Extra_bed = undefined;
    this.Free_Child = undefined;
  }
}
