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

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

  private tienePromo = false;
  private productoEsPromo = false;
  private busqueda: string = '';
  private productoAcargar: Producto;
  private promoAcargar: Producto;
  private productos: Producto[] = [];
  private promociones: Producto[] = [];
  private sinonimos: Sinonimo[] = [];
  private apiImagenes: string = appSettings.apiImagenes;

  @ViewChild('pop', { static: false }) popoverDirective: PopoverDirective;

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

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

  };

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

  ngOnInit() {

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

  ngAfterViewInit() {

    setTimeout(() => {
      if (!this.productoAcargar) return;

      if (this.productoAcargar.PRO) {
        this.promociones.push(this.productoAcargar);
        this.promoSeleccionada(this.productoAcargar, true);
      }
      else {
        this.getPromociones();
      }
    })
  }

  getProductos() {

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

  getPromociones() {

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

        if (res.length === 0) {

          this.productoAcargar.cantidad = 1;
          this.productoService.setProductos(this.productoAcargar);
          this.productoAcargar = this.productoService.productoAcargar = undefined;
        } else {

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

  confirmarProducto() {

    let producto = this.promoAcargar ? this.promoAcargar : this.productoAcargar;

    producto.cantidad = producto.cantidad ? producto.cantidad : 1;

    this.productoService.setProductos(producto);
    this.productoService.productoAcargar = this.promoAcargar = this.productoAcargar = undefined;
    this.productoService.esPromoPersonalizada = false;
    this.promociones = [];
    this.popoverDirective.hide();
  }

  promoSeleccionada($event: Producto, showPopover: boolean) {

    this.productoService.getProductoById($event.id)
      .subscribe((res: Producto) => {

        $event.imagenes = res.imagenes.length == 0 ? [{ imagen: 'noImage.jpg' }] : res.imagenes;
        this.promoAcargar = $event;

        if ($event.tieneSinonimos) {
          this.getSinonimos(this.promoAcargar.CodSec, this.promoAcargar.CodArt);
        } else if (showPopover) {
          this.popoverDirective.show();
        } else {
          this.popoverDirective.hide();
        }

      },
        error => { console.log(error); })
  }

  getSinonimos(sector, codigo) {

    this.productoService.getPromocionSinonimos(sector, codigo)
      .subscribe((res: Sinonimo[]) => {

        res.forEach(sinonimo => {

          sinonimo.cantidad = 0;
          this.promoAcargar.productos.forEach(productoPromo => {

            sinonimo.productos.forEach(productoSinonimo => {

              if (productoSinonimo.id === productoPromo.id) {
                productoSinonimo.cantidad = productoPromo.cantidad;
                sinonimo.cantidad += productoPromo.cantidad;
              }
            })
          })
        })
        this.sinonimos = res;
        (this.sinonimos.length > 0) ? this.popoverDirective.show() : this.popoverDirective.hide();
      })
  }

  productosPersonalizados($event: Producto[]) {

    let productosPersonalizados = $event;
    this.promoAcargar.productos = productosPersonalizados;
    this.confirmarProducto();
  }

  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');
    }

  }

  irBusquedaProductos(value) {

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

  deshacerCarga() {

    if (this.sinonimos.length > 0) {
      this.sinonimos = [];
      this.productoService.esPromoPersonalizada = false;
      this.popoverDirective.hide();
    }

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

}