File: /var/www/vhost/disk-apps/pwa.sports-crowd.com/src/app/pages/academy-period/academy-period.page.ts
import { Component, OnInit } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { AcademyService } from '../../services/academy.service';
import { UtilsService } from '../../services/utils.service';
import { AlertController, ModalController } from '@ionic/angular';
@Component({
selector: 'app-academy-period',
templateUrl: './academy-period.page.html',
styleUrls: ['./academy-period.page.scss'],
})
export class AcademyPeriodPage implements OnInit {
academyUserId;
periods: any = [];
months;
availableMonths: any = [];
showMonthSelect;
academyPurchaseId = null;
location: number;
isHideMonth: boolean = true;
//---------------------------------------------------ng_models--------------------------------------------------------
period;
month = null;
amount = 1;
//-------------------------------------------------END_ng_models------------------------------------------------------
constructor(
private utilsService: UtilsService,
public translateService: TranslateService,
private academyService: AcademyService,
public modalCtrl: ModalController,
public alertCtrl: AlertController,
) { }
ngOnInit() {
this.loadInit();
}
cancel(status?) {
this.modalCtrl.dismiss(status);
}
loadInit(time = 0) {
setTimeout(() => {
return new Promise((resolve, reject) => {
this.utilsService.presentLoading(this.translateService.instant("all.loading")).then(() => {
Promise.all([
this.setMonths(),
this.getPeriods()
]).then(() => {
this.utilsService.dismissLoading();
resolve(false);
}).catch((error) => {
this.utilsService.dismissLoading();
console.log('Error loadInit AcademyPeriodPage', error);
reject(error);
});
});
});
}, time);
}
setMonths() {
this.months = Array.from({ length: 12 }, (item, i) => {
return new Date(0, i).toLocaleString('es', { month: 'long' })
});
}
async getPeriods() {
let params = {
academyUserId: this.academyUserId,
location: this.location
};
(await this.academyService.getAcademyPeriods(params)).subscribe(
(resp) => {
this.periods = resp;
},
(error) => {
console.log("error", error);
}
);
}
createPaymentSchedule() {
let info = {
academyUserId: this.academyUserId,
periodId: this.period,
initMonth: this.month,
amount: this.amount,
academyPurchaseId: this.academyPurchaseId
};
this.utilsService.presentLoading(this.translateService.instant("all.loading")).then(async () => {
(await this.academyService.createPaymentSchedule(info)).subscribe({
next: (res: any) => {
if (!res.r) {
this.utilsService.closeAllAlerts();
this.presentConfirmationAlert(res.m);
return;
} else {
this.utilsService.closeAllAlerts();
this.utilsService.presentToast(4000, "success", "top", res.m);
this.cancel(true);
}
},
error: (err) => {
console.error("error provider createPaymentSchedule: ", err);
},
complete: () => { },
});
});
}
async presentConfirmationAlert(message: string) {
const alert = await this.alertCtrl.create({
header: this.translateService.instant("error.alert"),
message: message,
backdropDismiss: false,
buttons: [
{
text: this.translateService.instant("all.accept"),
handler: () => {
this.cancel(true);
}
}
]
});
await alert.present();
}
}