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: 'hotel-block-date',
  templateUrl: './hotel-block-date.component.html',
})
export class HotelBlockDateComponent implements OnInit {

  Extranet_Hotel_Id;
  Result;
  cols;
  Rule_Name;
  Date_Type;
  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: 'rule_name', header: 'Rule Name' },
      { field: 'block_date_type', header: 'Block Date Type' },
      { field: 'date_range_arr', header: 'Dates' },
      { field: 'status', header: 'status' },
      { 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' },
    ];
  }

  Search(){
    let ReqObj = {
      rule_name:this.Rule_Name ? this.Rule_Name : '',
      block_date_type:this.Date_Type ? this.Date_Type : '',
      hotel_id:this.Extranet_Hotel_Id
    }
    this.apiserv.Post('/ExtranetBlockDate/GetAllExtranetBlockDate',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['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');
              })
            }
            e['Custom_created_on'] = this._commonService.convertDateWithTimeToShow(e.created_on);
            e['block_date_type'] = e.block_date_type == 'CHECKINDATE' ? 'Check-in Date' : 'Check-out Date';
          });
          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('/ExtranetBlockDate/UpdateExtranetBlockDateStatus', obj).subscribe(res => {
      if (res) {
        this._commonService.showMessage(res['status'], res['message']);
        res['status'] ? this.Search() : '';
      }
    });
  }

  Edit(Id){
  this.router.navigateByUrl(`Services/Hotel/HotelManage/HotelEditBlockDate/${this.Extranet_Hotel_Id}/${Id}`);
  }
  Reset(){
    this.Rule_Name = undefined;
    this.Date_Type = undefined;
  }
  btnClick() {
    this.router.navigateByUrl("Services/FlightInventory/Add");
   }
}
