inicio.component.ts 2.69 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';


@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 tienePromo = false;
  private productoEsPromo = false;

  promociones: Promocion[] = [];
  sinonimos: Sinonimo[] = [];
  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((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;
              this.tienePromo = false;
            }, 2000)
          } else {

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

  showPopover() {

    this.popoverDirective.show();
  }

  private goPage(pageUrl) {

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

  deshacerCarga() {
    if (this.productoEsPromo) {
      this.promoAcargar = undefined;
      this.productoEsPromo = false;
      this.popoverDirective.show();
    } else {
      this.productoAcargar = undefined;
      this.tienePromo = false;
      this.popoverDirective.hide();
    }
  }

  promoSeleccionada($event: Promocion) {

    this.productoEsPromo = true;
    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[]) => {

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

}