File: /var/www/vhost/disk-apps/pwa.sports-crowd.com/src/app/pages/symbolic-ballot/symbolic-ballot.page.ts
import { Component, OnInit } from "@angular/core";
import { ActivatedRoute } from "@angular/router";
import { HomeService } from "../../services/home.service";
import { StorageService } from '../../services/storage.service';
import { UtilsService } from "../../services/utils.service";
import { CorporateIdentityService } from "../../services/corporate-identity.service";
import { TranslateService } from "@ngx-translate/core";
import { NavController, AlertController } from "@ionic/angular";
import { InAppBrowser, UrlEvent } from '@capgo/inappbrowser'
@Component({
selector: "app-symbolic-ballot",
templateUrl: "./symbolic-ballot.page.html",
styleUrls: ["./symbolic-ballot.page.scss"],
})
export class SymbolicBallotPage implements OnInit {
ballot_id: any;
list_tickets: any;
symbolic_ticket_price_id: any;
current_symbolic: any;
quantity: number = 1;
total: number = 0;
current_ticket: any;
isSubscriber: boolean;
currency: string;
constructor(
public route: ActivatedRoute,
public homeProvider: HomeService,
private storage: StorageService,
public utilsService: UtilsService,
private translateService: TranslateService,
public navCtrl: NavController,
public alertController: AlertController,
private corporateIdentityService: CorporateIdentityService
) {
this.currency = this.corporateIdentityService.getCurrency();
}
ngOnInit() {
this.storage.get("infoUser").then((info) => {
this.isSubscriber = (info.is_subscriber == 1);
});
this.ballot_id = this.route.snapshot.paramMap.get("ballot_id");
this.current_symbolic = this.homeProvider._listSymbolicBallots.find((item: any) => item.id == this.ballot_id);
this.getPriceTicket();
}
getPriceTicket() {
this.storage.get("token").then(async (token) => {
(await this.homeProvider.getSymbolicPriceTicket(this.ballot_id, token.access_token)).subscribe(
(resp) => {
this.list_tickets = resp;
},
(error) => {
console.log("error", error);
}
);
});
}
buyTicket() {
if (this.symbolic_ticket_price_id) {
this.storage.get("token").then(async (token) => {
this.utilsService.presentLoading(this.translateService.instant("all.sending"));
(await this.homeProvider.symbolicTicketPending(this.ballot_id, this.symbolic_ticket_price_id, this.quantity, token.access_token)).subscribe(
async (resp) => {
if (resp['total'] == 0 && this.isSubscriber) {
(await this.homeProvider.purchaseSubscriberSymbolicTicket(resp['id'], token.access_token)).subscribe(
(resp) => {
this.utilsService.dismissLoading();
this.alertForceBack(this.translateService.instant("simbolic_tickets.msg_purchaseSuccess"));
},
(error) => {
this.utilsService.dismissLoading();
console.log("error symbolicTicketPending", error);
}
);
} else {
this.homeProvider.purchaseSymbolicTicket(resp["id"]);
this.utilsService.dismissLoading();
InAppBrowser.addListener("urlChangeEvent", (state: UrlEvent) => {
if (state.url.indexOf("/close") != -1) {
InAppBrowser.close();
this.navCtrl.back();
}
});
}
},
(error) => {
this.utilsService.dismissLoading();
console.log("error", error);
}
);
});
}
}
selectTicket() {
this.quantity = 1;
setTimeout(() => {
this.current_ticket = this.list_tickets.find((item: any) => item.id == this.symbolic_ticket_price_id);
if (this.current_ticket) {
this.calculateTotal();
}
}, 500);
}
moreQuantity() {
this.quantity += 1;
this.calculateTotal();
}
removeQuantity() {
if (this.quantity > 1) {
this.quantity -= 1;
this.calculateTotal();
}
}
calculateTotal() {
this.total = 0;
if (this.current_ticket) {
this.total = this.current_ticket.price * this.quantity;
}
}
async alertForceBack(m: string) {
const alert = await this.alertController.create({
header: this.translateService.instant("simbolic_tickets.title_header"),
message: m,
backdropDismiss: false,
buttons: [
{
text: "Ok",
handler: () => {
this.back();
},
},
],
});
await alert.present();
}
back() {
this.navCtrl.back();
}
}