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 { API } from 'src/app/core/services/api.service';
@Component({
  selector: 'offers-view',
  templateUrl: './offers-view.component.html',
})
export class OffersViewComponent implements OnInit {

  AllRoomsData;
  Result;
  cols;
  OfferSearchForm:FormGroup
  constructor(
    public activateRoute:ActivatedRoute,
    public apiserv:API,
    public _commonService:CommonFunctionService,
    public router:Router,
    private fb:FormBuilder
  ) { }
  Extranet_Hotel_Id;
  ngOnInit(): void {
    this.Extranet_Hotel_Id = this.activateRoute.snapshot.paramMap.get('Hotel_Eaxtranet_Id') as string;
    this.GetAllRooms();
    this.OfferSearchForm = this.fb.group({
      extranet_hotel_id:this.Extranet_Hotel_Id,
      extranet_room_id:'',
      contract_name:'',
      offer_name:'',
      offer_type:'',
      discount_code:'',
      is_combinable_offer:''
    });
    this.cols = [
      { field: 'offer_name', header: 'Offer Name' },
      { field: 'offer_type', header: 'Offer Type' },
      { field: 'room_name', header: 'Room Name' },
      { field: 'is_combinable_offer', header: 'Combinable Offer' },
      { field: 'discount_code', header: 'Discount Code' },
      { field: 'offer_days', header: 'Offer days' },
      { field: 'status', header: 'Status' },
      { field: 'extranet_offer_id', header: 'Offer 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' },
    ];
  }

  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=>{
      if(res && res.data){
        this.AllRoomsData = res.data;
      }
    })
  }
  Search(){
    let val = this.OfferSearchForm.getRawValue();
    let obj = {
      extranet_hotel_id:val.extranet_hotel_id,
      extranet_room_id:val.extranet_room_id,
      contract_name:val.contract_name,
      offer_name:val.offer_name,
      offer_type:val.offer_type,
      discount_code:val.discount_code,
      is_combinable_offer:val.is_combinable_offer,
    }
    this.apiserv.Post('/ExtranetOffer/GetAllExtranetOffer',obj).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['offer_type'] = this.getOfferType(e['offer_type']);
            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();
          });
        }
      }
    })
   }
   updateStatus(id, val) {
    let obj = {
      Id: id,
      status: val ? false : true,
    };
    this.apiserv.Post('/ExtranetOffer/UpdateExtranetOfferStatus', obj).subscribe(res => {
      if (res) {
        this._commonService.showMessage(res['status'], res['message']);
        res['status'] ? this.Search() : '';
      }
    });
  }
  getOfferType(value){
    if(value == 'EARLYBIRD'){
      return 'Early Bird'
    }else if(value == 'STAYPAY'){
      return 'Stay Pay'
    }else if(value == 'LONGSTAY'){
      return 'Long Stay'
    }else if(value == 'DISCOUNT'){
      return 'Discount'
    }else if(value == 'CONTRACTREMARK'){
      return 'Contract Remark'
    }
    return ''
  }
  

  Edit(Id){
  this.router.navigateByUrl(`Services/Hotel/HotelManage/EditOffer/${this.Extranet_Hotel_Id}/${Id}`);
  }
  Reset(){
    this.OfferSearchForm.get('extranet_room_id').setValue('');
    this.OfferSearchForm.get('contract_name').setValue('');
    this.OfferSearchForm.get('offer_name').setValue('');
    this.OfferSearchForm.get('offer_type').setValue('');
    this.OfferSearchForm.get('discount_code').setValue('');
    this.OfferSearchForm.get('is_combinable_offer').setValue('');
  }
}
