import { Component, OnInit } from '@angular/core';
import { ModalController, MenuController, NavController } from '@ionic/angular';
import { EventService } from '../../../services/event.service';
import { EnvService } from 'src/app/services/env.service';
import { AuthService } from 'src/app/services/auth.service';
import { IchambaService } from 'src/app/services/ichamba.service';
import { TranslateService } from '@ngx-translate/core';
import { AlertService } from 'src/app/services/alert.service';


@Component({
  selector: 'app-cards',
  templateUrl: './cards.page.html',
  styleUrls: ['./cards.page.scss'],
  standalone: false
})
export class CardsPage implements OnInit {

  cards: any[] = [];

  constructor(
      private modalController: ModalController,
      private menu: MenuController,
      private navCtrl: NavController,
      private events: EventService,
      private authService: AuthService,
      private alertService: AlertService,
      private ichambaService: IchambaService,
      private translateService: TranslateService,
      private env: EnvService,
  ) {
    this.events.subscribe('refreshcards', (data) => {
      this.getcards();
    });
  }

  ngOnInit() {
    this.getcards();
  }

  newcard() {
    this.navCtrl.navigateForward('/addcard');
  }


  getcards() {
    this.ichambaService.getCard().subscribe(
      data => {
        this.cards = data;
      }, error => {
        this.alertService.presentToast(this.translateService.instant('alerts.error') + error['status']);
      });
  }

  deletecard(id: String) {
    this.ichambaService.deleteCard(id).subscribe(
      data => {
        this.getcards();
        this.alertService.presentToast(this.translateService.instant('alerts.deleted_card'));
      }, error => {
        this.alertService.presentToast(this.translateService.instant('alerts.error') + error['status']);
      });
  }

}
