import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, AlertController, ViewController } from 'ionic-angular';
import { HealthProvider } from '../../providers/health/health';
import { DomSanitizer } from '@angular/platform-browser';
import { NativeStorage } from '@ionic-native/native-storage';
/**
 * Generated class for the CouponViewPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-coupon-view',
  templateUrl: 'coupon-view.html',
})
export class CouponViewPage {
    item;
    info: string = "info";
    product;
    token;
    
    constructor(public navCtrl: NavController, public navParams: NavParams, private hp: HealthProvider, private nativeStorage: NativeStorage, private vc: ViewController, private alertCtrl: AlertController, private sanitizer: DomSanitizer) {
        this.item = navParams.data.item;
        this.product = this.item.p;
        this.nativeStorage.getItem('token')
            .then(
                data => { 
                    this.token = data;
                },
                error => {
                    console.log("Error to get token");
                });
    }
    ionViewDidLoad() {
        console.log('ionViewDidLoad CouponViewPage');
    }

    dismiss() {
        this.vc.dismiss();
    }
    useCoupon() {
        console.log(JSON.stringify(this.item));
        this.hp.useCoupon(this.token,this.item.t.id)
            .subscribe( data => {
                this.item.t.used = true;
                this.dismiss();

            }, error => {
                let alert = this.alertCtrl.create({
                    title: 'Oh Sorry',
                    message: "Please try again",
                    buttons: ['Ok']
                });
                alert.present()
            } );
    }
}
