inicio.component.ts 4.2 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';
import { Sinonimo } from 'src/app/wrappers/sinonimo';
import { HostListener } from '@angular/core';

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

  @HostListener('document:keypress', ["$event"]) catchInput(e: KeyboardEvent) {

    if (e.keyCode == 13) {
      this.buscarByCodigoBarras(this.busqueda);
      this.busqueda = '';
    } else {
      this.busqueda += e.key;
    }

  };

  @ViewChild('pop', { static: false }) popoverDirective: PopoverDirective;
  private productoAcargar: Producto;
  private productos: Producto[];
  private promoAcargar: Promocion;
  private tienePromo = false;
  private productoEsPromo = false;
  private busqueda: string = '';
  private sinonimoAcargar: Sinonimo;

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

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

  ngOnInit() {

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

  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 = this.productoService.productoAcargar = undefined;
            }, 2000)
          } else {

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

  showPopover() {

    this.popoverDirective.show();
  }

  getProductos() {
    this.productoService.getAll()
      .subscribe((productos: Producto[]) => {
        this.productos = productos;
      });
  }

  private irBusquedaProductos(verPromociones) {

    this.productoService.verCategoriasProductos = verPromociones;
    this.router.navigate(['busqueda-productos']);
  }

  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: Sinonimo[]) => {

          res.forEach(resSinonimo => {

            this.promoAcargar.productos.forEach(productoPromo => {

              if (productoPromo.idSinonimo === resSinonimo.ID_SIN) {
                resSinonimo.cantidad = productoPromo.cantidad;
                resSinonimo.productoPadre = productoPromo.id;
              }
            });

          })
          this.sinonimos = res;
          this.showPopover();
        })
    }
  }

  sinonimoSeleccionado($event: Sinonimo) {

    console.log($event);
    this.sinonimoAcargar = $event;
  }

  buscarByCodigoBarras(busqueda) {

    let producto = this.productos.filter(producto => {
      return producto.codigoBarra == busqueda;
    });

    if (producto.length) {

      this.productoAcargar = producto[0];
      this.getPromociones();

    } else {
      alert('No se encuentra el producto');
    }

  }

}