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 { ManageHotelService } from 'src/app/core/services/HotelServices/manage-hotel.service';
import { API } from 'src/app/core/services/api.service';

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

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

  ngOnInit(): void {
    this.Extranet_Hotel_Id = this.activateRoute.snapshot.paramMap.get('Hotel_Eaxtranet_Id') as string;
    this.cols = [
      { field: 'season_name', header: 'Season Name' },
      { field: 'date_range', header: 'Date Range' },
      { field: 'extranet_season_id', header: 'Season Id' },
      { field: 'updated_by', header: 'Created By' },
      { field: 'Custom_updated_on', header: 'Created On' },
      { field: 'updated_by', header: 'Updated By' },
      { field: 'Custom_updated_on', header: 'Updated On' },
      { field: 'status', header: 'Status' },
    ];
  }

  Search(){
    let ReqObj = {
      season_name:this.Season_Name ? this.Season_Name : '',
      hotel_id:this.Extranet_Hotel_Id
    }
    this.apiserv.Post('/ExtranetSeason/GetAllExtranetSeason',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['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'];
            e['date_range_arr'] = e['date_range'] ? e['date_range'].split(', ') : e['date_range'];
            if(typeof(e['date_range_arr']) != 'string'){
              e.date_range_arr.forEach((data,ind)=>{
                e.date_range_arr[ind] =  moment(data.split(' - ')[0]).format('DD MMM YYYY') + ' - ' + moment(data.split(' - ')[1]).format('DD MMM YYYY');
              })
            }
          });
          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();
          });
        }
      }
    })
  }
  updateStatus(id, val) {
    let obj = {
      Id: id,
      status: val ? false : true,
    };
    this.apiserv.Post('/ExtranetSeason/UpdateExtranetSeasonStatus', obj).subscribe(res => {
      if (res) {
        this._commonService.showMessage(res['status'], res['message']);
        res['status'] ? this.Search() : '';
      }
    });
  }

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

}
