sidebar.component.ts 2.34 KB
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { Producto } from 'src/app/wrappers/producto';
import { appSettings } from 'src/etc/AppSettings';


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

  private cont: number = 1;
  private min: number = 1;
  private max: number = 50;
  private total: number = 0;
  private apiUrl: string = appSettings.apiUrl;

  public productosCarrito: Producto[] = [];

  constructor() { }

  ngOnInit() { }

  getCantidadProductos() {

    for (let i = 0; i < this.productosCarrito.length; i++) {
      this.productosCarrito[i].cantidad = 1
      this.cont++;
    }
    return this.cont;
  }

  getTotal() {

    let subTotal = 0;
    for (let i = 0; i < this.productosCarrito.length; i++) {
      subTotal = subTotal + (this.productosCarrito[i].PreVen * this.productosCarrito[i].cantidad);
    }
    return this.total = subTotal;
  }

  public aumentarContador(index) {

    this.cont++;
    for (let i = 0; i < this.productosCarrito.length; i++) {
      if (i === index) {
        this.total = this.total + this.productosCarrito[i].PreVen;
        return (this.productosCarrito[i].cantidad === this.max) ?
          this.productosCarrito[i].cantidad : this.productosCarrito[i].cantidad++;

      }
    }
  }

  decrementarContador(index) {

    for (let i = 0; i < this.productosCarrito.length; i++) {
      if (i === index && this.productosCarrito[i].cantidad > 1) {
        this.productosCarrito[i].cantidad--;
        this.cont--;
        break;
      }
    }

    this.getTotal()
  }

  setCantidadItems() {

    this.cont = 0;
    for (let i = 0; i < this.productosCarrito.length; i++) {
      this.cont += this.productosCarrito[i].cantidad;
    }
  }

  deleteProducto(index) {

    for (let i = 0; i < this.productosCarrito.length; i++) {
      if (i === index) {
        this.cont -= this.productosCarrito[i].cantidad;
        //Elimina del total el PreVen de todo el item
        this.total = this.total - (this.productosCarrito[i].PreVen * this.productosCarrito[i].cantidad);
        this.productosCarrito.splice(i, 1);
        return;
      }
    }
  }

  cleanCarrito() {

    this.productosCarrito = [];
    this.total = 0;
    this.cont = 0;
  }
}