pago.component.ts 2.16 KB
import { Component, OnInit, OnDestroy } from '@angular/core';
import { appSettings } from 'src/etc/AppSettings';
import { Location } from '@angular/common';
import { ProductoService } from 'src/app/services/producto.service';
import { Router } from '@angular/router';
import { Producto } from 'src/app/wrappers/producto';

@Component({
  selector: 'app-pago',
  templateUrl: './pago.component.html',
  styleUrls: ['./pago.component.scss']
})
export class PagoComponent implements OnInit, OnDestroy {

  private apiImagenes: string = appSettings.apiImagenes;
  private verQR: boolean = false;
  private productos: Producto[] = [];
  private total: number = 0;

  private compraConQRfinalizada: boolean = false;
  private compraConEfectivofinalizada: boolean = false;
  private timerReposo: any;

  constructor(
    private productoService: ProductoService,
    private location: Location,
    private router: Router,
  ) { }

  ngOnInit() {

    this.timerReposo = setTimeout(() => {

      this.router.navigate(['cancelar-compra']);
    }, 90000)
    this.productos = this.productoService.productos;
  }

  ngOnDestroy() {

    clearTimeout(this.timerReposo);
  }

  pagoEfectivo() {

    this.compraConEfectivofinalizada = true;
    clearTimeout(this.timerReposo);
    setTimeout(() => {

      this.router.navigate(['mensaje-final']);
    }, 3000);
  }

  pagar() {

    this.verQR = true;

    this.productoService.pagar()
      .subscribe(() => {

        clearTimeout(this.timerReposo);
        this.compraConQRfinalizada = true;
        setTimeout(() => {

          this.router.navigate(['mensaje-final']);
        }, 3000);
      }, err => {
        console.log(err);
        alert('algo salió mal');
      })
  }

  volverPreviousPage() {

    if (this.verQR) {
      this.verQR = false;
      return;
    }
    this.location.back();
  }

  getTotal() {

    var subTotal = 0;
    this.productos.forEach(producto => {

      subTotal = subTotal + (producto.PreVen * producto.cantidad);
    });
    return this.total = subTotal;
  }

  reiniciarTimer() {

    clearTimeout(this.timerReposo);
    this.timerReposo = setTimeout(() => {

      this.router.navigate(['cancelar-compra']);
    }, 90000)
  }

}