import { Component, OnInit } from '@angular/core';
import * as FileSaver from 'file-saver';
import {
  ConfirmationService,
  MessageService,
  PrimeNGConfig,
} from 'primeng/api';
import { API } from 'src/app/core/services/api.service';

import { Product } from 'src/app/core/services/product';
import { CommonService } from 'src/app/core/services/common.service';
import { Router } from '@angular/router';
import { ActivityAllContract } from 'src/app/core/models/Experience-Service/experience-rate-config-Edit/edit-rate-confi/edit-rate-confi.model';
import * as moment from 'moment';

@Component({
  selector: 'rate-config',
  templateUrl: './rate-config.component.html',
})
export class RateConfigComponent implements OnInit {
  products!: Product[];
  selectedProducts!: Product[];

  product!: Product;
  rateDetails;
  OptionName = '';
  is_pickup_included = false;
  is_drop_off_included = false;
  constructor(public ApiServ: API, private MsgService: MessageService, private router: Router) { }
  ngOnInit(): void {
    // this.GetallrateDetails();
  }


  GetallrateDetails() {
    let activityid = CommonService.getSessionStoage('activity_id');
    let postdata = new ActivityAllContract();
    postdata.activity_id = activityid;
    postdata.is_drop_off_included = this.is_drop_off_included;
    postdata.is_pickup_included = this.is_pickup_included;
    postdata.option_name = this.OptionName;
    this.ApiServ.Post('/ActivityContract/GetAllActivityRates', postdata).subscribe(data => {
      if (data) {
        this.rateDetails = data.responseObject;
      }
    })
  }

  UpdateStatus(ID, Status) {
    let Obj = {
      id: ID,
      updatedBy: "",
      "status": Status ? false : true
    }
    this.ApiServ.Post('/ActivityContract/UpdateStatus', Obj).subscribe(data => {
      if (data) {
        this.MsgService.add({
          severity: data["status"] ? "success" : "error",
          detail: data["message"],
        });
        this.GetallrateDetails();
      }
    });
  };

  EditCancelPoli(Id) {
    let obj = { id: Id };
    this.router.navigate(['/Services/ActivityServices/ExpServices/Manage/EditCancelletionPolicy'], { queryParams: obj })
  }
  EditAllocation(Id) {
    let obj = { id: Id };
    this.router.navigate(['/Services/ActivityServices/ExpServices/Manage/EditAllocation'], { queryParams: obj })
  }
  EditRateConfig(Id) {
    let obj = { id: Id };
    this.router.navigate(['/Services/ActivityServices/ExpServices/Manage/EditModifyRate'], { queryParams: obj })
  }
  
  EditDetails(Id) {
    let obj = { id: Id };
    this.router.navigate(['/Services/ActivityServices/ExpServices/Manage/EditDetails'], { queryParams: obj })
  }

  exportExcel() {
    import('xlsx').then((xlsx) => {
      const worksheet = xlsx.utils.json_to_sheet(this.products);
      const workbook = { Sheets: { data: worksheet }, SheetNames: ['data'] };
      const excelBuffer: any = xlsx.write(workbook, {
        bookType: 'xlsx',
        type: 'array',
      });
      this.saveAsExcelFile(excelBuffer, 'products');
    });
  }
  reset(){
    this.is_drop_off_included = false;
    this.is_pickup_included = false;
    this.OptionName = '';
  }


  SoltsDetailsConvParse(data) {
    let obj = data ? JSON.parse(data) : '';
    let html = [];
    obj ? obj.forEach(e => html.push({ 'date': moment(e.start_time).format('hh:mm') + '-' + moment(e.end_time).format('hh:mm') })) : ''
    return html
  }
  saveAsExcelFile(buffer: any, fileName: string): void {
    import('file-saver').then((abc) => {
      let EXCEL_TYPE =
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
      let EXCEL_EXTENSION = '.xlsx';
      const data: Blob = new Blob([buffer], {
        type: EXCEL_TYPE,
      });
      FileSaver.saveAs(
        data,
        fileName + '_export_' + new Date().getTime() + EXCEL_EXTENSION
      );
    });
  }
}
