import { Component, OnInit } from '@angular/core'; import { FormGroup } from '@angular/forms'; import { Cartdto } from 'src/app/model/Cartdto'; import { CartItem } from 'src/app/model/CartItem'; import { Orderdto } from 'src/app/model/Orderdto'; import { CartService } from 'src/app/service/cart.service'; declare const calc:any @Component({ selector: 'app-cart', templateUrl: './cart.component.html', styleUrls: ['./cart.component.css'] }) export class CartComponent implements OnInit { public products : any = []; cartItem!:CartItem[]; cartdto!:Cartdto[]; orderdto!:Orderdto[]; cartdto2!:Cartdto[]; uid!:any; pid!:any; cid!:any; oid!:any; role!:any; public grandTotal : number = 0; public grandTotal1 : any; qty!:any; qty1!:any; cartForm!:FormGroup constructor(private cartService:CartService) { } ngOnInit(): void { this.cartService.getProducts().subscribe(res=>{ this.products = res; this.grandTotal = this.cartService.getTotalPrice(); }) this.role = window.sessionStorage.getItem('role'); console.log(this.role); this.uid = window.sessionStorage.getItem('userid'); this.cartService.getCartdto(this.uid).subscribe(res=>{ this.cartdto=res; }) this.uid = window.sessionStorage.getItem('userid'); this.cartService.getCartsum(this.uid).subscribe(res=>{ this.grandTotal1=res; }) } removeItem(cid:number) { this.cartService.deleteCartItem(cid).subscribe(data => { this.cartdto.forEach((i, index) => { if (i.cid == cid) { this.cartdto.splice(index, 1); this.cartService.getCartsum(this.uid).subscribe(res=>{ this.grandTotal1=res; }) } }) }, (err: any) => { console.log(err); }) this.cartService.deleteOrderItem(cid).subscribe(data => { this.cartdto.forEach((i, index) => { if (i.cid == cid) { this.cartdto.splice(index, 1); } }) }, (err: any) => { console.log(err); }) } inc(prod:any,ccid:number){ prod.quantity = prod.quantity + 1; this.qty = prod.quantity this.cid=ccid; console.log(this.qty,this.cid); this.cartService.addquantity(this.cid,this.qty).subscribe(data=>{ this.qty1=data; console.log(this.qty1); this.uid = window.sessionStorage.getItem('userid'); this.cartService.getCartsum(this.uid).subscribe(res=>{ this.grandTotal1=res; }) }) } dec(prod:any,ccid:number){ if(prod.quantity != 1) { prod.quantity = prod.quantity - 1; this.qty = prod.quantity this.cid=ccid; console.log(this.qty,this.cid); this.cartService.addquantity(this.cid,this.qty).subscribe(data=>{ this.qty1=data; console.log(this.qty1); this.uid = window.sessionStorage.getItem('userid'); this.cartService.getCartsum(this.uid).subscribe(res=>{ this.grandTotal1=res; }) }) } } addquantity(id:number,quantity:number) { this.qty = quantity this.cid=id; console.log(this.qty,this.cid); this.cartService.addquantity(this.cid,this.qty).subscribe(data=>{ this.qty=data; console.log(this.qty); }) } getMultiply(price:number, quantity:number) { return price * quantity; } }