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

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

  tabs = 'about';

  constructor(
      private navctr: NavController,
      private router: Router,
      private alertController:AlertController,
      public sessionDataProviderService: SessionDataProviderService,
      private wrenchService: WrenchService,
  ) { }

  ngOnInit() {
    this.getMyCouponHxData();
    this.getMyCouponPendData();
  }

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

  onMentor() {
    this.router.navigate(['mentor']);
  }

  onEnroll() {
    this.router.navigate(['enroll']);
  }
  //getCouponHx(usrData)
  usrData: {
    action:number, member_id: number, uid: string, sessionid: string, limit:20, page:1,offset: 0
  };

  myCouponHxTotalData:any;
  myCouponHx: [];
  getMyCouponHxData(){
    this.usrData = {action:85025,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session ,
      limit:20, page:1,offset: 0}
//debugger;
    this.wrenchService.getCouponHx(this.usrData).subscribe(
        myCouponHxTotalData => {
          this.myCouponHxTotalData = myCouponHxTotalData;
          console.log("jobManagerJobsTotalData  RETURN->", this.myCouponHxTotalData);
          this.myCouponHx = this.myCouponHxTotalData.result_list;
          // debugger;
          console.log("myCouponHx RETURN DATA->", this.myCouponHx);
        }
    );

  }

    myCouponPendTotalData:any;
    myCouponPend: [];
    pending_coupon_count:number =0 ;
  getMyCouponPendData(){
    this.usrData = {action:85030,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session ,
      limit:20, page:1,offset: 0}
    this.wrenchService.getCouponPending(this.usrData).subscribe(
        myCouponPendTotalData => {
        this.myCouponPendTotalData = myCouponPendTotalData;
        console.log("myCouponPendTotalData  RETURN->", this.myCouponPendTotalData);
        this.myCouponPend = this.myCouponPendTotalData.result_list;
        this.pending_coupon_count = this.myCouponPend.length;
        console.log("myCouponPend RETURN DATA->", this.myCouponPend);
        }
    );

  }

  redeemData: {
    action:number,
    member_id: number,
    uid: string,
    sessionid: string,
    code:string,
    code_id: number
  };
  redeemResult:any;
  async startRedeem(item){

    const redMsg  =  "Confirm you are ready to redeem " + item.amount*0.01 + " " + item.currency;
    const alert = await this.alertController.create({
      header: "Redeem Coupon",
      message: redMsg,
      buttons: [
        {
          text: "Cancel",
          role: "cancel",
          handler: () => {
            // this.getMyOffersData();
          },
        },
        {
          text: "Redeem",
          handler: () => {
            this.redeemData = {
                action:85020,
                member_id: this.sessionDataProviderService.member_id,
                uid: this.sessionDataProviderService.member_uid,
                sessionid: this.sessionDataProviderService.session ,
                code: item.code,
                code_id: item.coupon_id
            };
            this.wrenchService.redeemCoupon(this.redeemData).subscribe(
                redeemResult => {
                  this.redeemResult = redeemResult;
                  console.log("redeemResult  RETURN->", this.redeemResult);
                   if ( this.redeemResult != null && this.redeemResult.status=='COMPLETED'){
                       this.getMyCouponHxData();
                       this.getMyCouponPendData();
                      //alert(this.redeemResult.status_text);
                   }
                }
            );
          },
        },
      ],
    });
    await alert.present();
  }


}
