inicio.component.ts 1.95 KB
import { Component, OnInit, ViewChild } from '@angular/core';
import { PopoverDirective } from 'ngx-bootstrap';
import { appSettings } from 'src/etc/AppSettings';
import { Producto } from 'src/app/wrappers/producto';
import { ProductoService } from 'src/app/services/producto.service';
import { Router } from '@angular/router';
import { Promocion } from 'src/app/wrappers/promocion';


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

  @ViewChild('pop', { static: false }) popoverDirective: PopoverDirective;
  private productoAcargar: Producto;
  private tienePromo = false;

  popoverContent: Promocion[] = []
  apiUrl: string = appSettings.apiUrl

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

  ngOnInit() {

    this.productoAcargar = this.productoService.productoAcargar;
    if (this.productoAcargar) {
      var sector = this.productoAcargar.CodSec;
      var codigo = this.productoAcargar.CodArt;
      this.productoService.getPromocion(sector, codigo)
        .subscribe((promociones: Promocion[]) => {

          if (promociones.length === 0) {
            //Si no tiene promociones la cargará al carrito despues de un tiempo
            setTimeout(() => {
              this.productoService.productos.push(this.productoAcargar);
              this.productoAcargar = undefined;
              this.tienePromo = false;
            }, 2000)
          } else {

            this.popoverContent = promociones;
            this.tienePromo = true;
            this.popoverDirective.show();
          }
        }, error => { console.error(error); })
    }
  }

  showPopover() {

    this.popoverDirective.show();
  }

  private goPage(pageUrl) {
    this.router.navigate([pageUrl]);
  }

  deshacerCarga() {

    this.productoAcargar = undefined;
    this.tienePromo = false;
    this.popoverDirective.hide();
  }

}