import {AlertController, NavController} from '@ionic/angular';
import { Router } from '@angular/router';
import { Component, OnInit } from '@angular/core';
import {SessionDataProviderService} from "../../store/session-data-provider.service";
import {WrenchService} from "../../services/wrench.service";

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

  tabs = 'cards';
  constructor(
    private router: Router,
    private navctr: NavController,
    public sessionDataProviderService: SessionDataProviderService,
    private wrenchService: WrenchService,
    private alertController: AlertController
  ) { }

  ngOnInit() {
    this.getUsersCardsList();
    this.getMyRecipientsData();
  }

  onAddCard() {
    this.router.navigate(['new-card']);
  }

  onBack() {
    this.navctr.back();
  }

  usrData: {
    action:number, member_id: number, uid: string, sessionid: string, limit:20, page:1,offset: 0
  };

  usersCardsTotalData:any;
  usersCardsData: [];
  getUsersCardsList(){
    this.usrData =
        {
          action:11055,
          member_id: this.sessionDataProviderService.member_id,
          uid: this.sessionDataProviderService.member_uid,
          sessionid: this.sessionDataProviderService.session ,
          limit:20, page:1,offset: 0
        };

    this.wrenchService.getusersCardList(this.usrData).subscribe(
        usersCardsTotalData => {
          this.usersCardsTotalData = usersCardsTotalData;
          console.log("usersCardsTotalData TOTAL  RETURN->", this.usersCardsTotalData);
          this.usersCardsData = this.usersCardsTotalData.result_list;
          // debugger;
          console.log("usersCardsData RETURN DATA->", this.usersCardsData);
          //this.total_family = this.familyData.length;
        }
    );

  }

    myRecipientsTotalData:any;
    myRecipientsData:any;
    getMyRecipientsData(){
        this.usrData = {action:11175,
            member_id: this.sessionDataProviderService.member_id,
            uid: this.sessionDataProviderService.member_uid,
            sessionid: this.sessionDataProviderService.session ,
            limit:20, page:1,
            offset: 0}

        this.wrenchService.recipientAccount(this.usrData).subscribe(
            myRecipientsTotalData => {
                this.myRecipientsTotalData = myRecipientsTotalData;
                console.log("myRecipientsTotalData  RETURN->", this.myRecipientsTotalData);
                this.myRecipientsData = this.myRecipientsTotalData.result_list;
                // debugger;
                console.log("myRecipientsData RETURN DATA->", this.myRecipientsData);
            }
        );

    }

    cardDelData:{
        action : number,
        card_uid : string,
        member_id: number,
        sessionid : string,
        uid: string
    }
    myDeleteCardResult:any;
    async deleteCard(item){
        const alert = await this.alertController.create({
            header: "Delete Card",
            message: "Confirm you are ready delete the card ?",
            buttons: [
                {
                    text: "Cancel",
                    role: "cancel",
                    handler: () => {
                        // this.getMyOffersData();
                    },
                },
                {
                    text: "Delete",
                    handler: () => {
                        this.cardDelData = {
                            action: 11057,
                            card_uid: item.card_uid,
                            member_id: this.sessionDataProviderService.member_id,
                            sessionid: this.sessionDataProviderService.session,
                            uid: this.sessionDataProviderService.member_uid
                        };

                        this.wrenchService.deleteUserCard(this.cardDelData).subscribe(
                            myDeleteCardResult => {
                                this.myDeleteCardResult = myDeleteCardResult;
                                console.log("myDeleteCardResult  RETURN->", this.myDeleteCardResult);
                                this.getUsersCardsList();

                            }
                        );
                    },
                },
            ],
        });
        await alert.present();
    }

}
