inicio.component.ts 3 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 promoAcargar: Promocion;
  private sinonimoAcargar: Producto;

  promociones: Promocion[] = [];
  sinonimos: Producto[] = [];
  apiUrl: string = appSettings.apiUrl

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

  ngOnInit() {

    this.productoAcargar = this.productoService.productoAcargar;
    this.getPromociones();
  }

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

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

            this.promociones = res;
            this.popoverDirective.show();
          }
        }, error => { console.error(error); })
    }
  }

  showPopover() {

    this.popoverDirective.show();
  }

  private goPage(pageUrl) {

    this.router.navigate([pageUrl]);
  }

  deshacerCarga() {

    if (this.sinonimoAcargar || this.sinonimos.length > 0) {
      this.sinonimos = [];
      this.sinonimoAcargar = undefined;
      this.popoverDirective.hide();
    }

    if (this.promoAcargar) {
      this.promoAcargar = undefined;
      this.popoverDirective.show();
    } else {
      this.productoAcargar = undefined;
      this.promociones = [];
      this.popoverDirective.hide();
    }
  }

  promoSeleccionada($event: Promocion) {

    this.promoAcargar = $event;
    this.popoverDirective.hide();
    if (this.promoAcargar.sinonimos) {
      var sector = this.promoAcargar.sector;
      var codigo = this.promoAcargar.codigo;
      this.productoService.getPromocionSinonimos(sector, codigo)
        .subscribe((res: Producto[]) => {
          res.forEach(resSinonimo => {
            this.promoAcargar.productos.forEach(producto => {
              if (producto.id === resSinonimo.id) {
                resSinonimo.esPadre = true;
              }
            });
          })
          this.sinonimos = res;
          this.showPopover();
        })
    }
  }

  sinonimoSeleccionado($event: Producto) {

    this.sinonimoAcargar = $event;
  }

}