import { Component, HostListener, ViewChild } from '@angular/core'; import { BsModalRef, BsModalService } from 'ngx-bootstrap'; import { CartService } from '../../../services/cart.service'; import { LoginModalComponent } from '../../custom-portal-pages/header-components/login-modal/login-modal.component'; import { ToastrService } from 'ngx-toastr'; @Component({ selector: 'espl-cart', templateUrl: 'cart.component.html', styleUrls: ['cart.css'] }) export class CartComponent { itemsInCart: []; subTotalDiscountPrice: number; subTotalSalesPrice: number; cartItemsCount: number; upfrontDiscount: number = 0; handlingFee: number; checkOutAmount: number = 0; isWalletAmountAvailable: boolean; isWalletAmountUsed: boolean; usedWalletAmount: number = 0; max_wallet_applied: number; config: any; user: any; bsModalRef: BsModalRef; @ViewChild('esplcart') esplcart; hidecart() { this.esplcart.nativeElement.style.width = '0px'; } constructor(private toastr: ToastrService, private cartService: CartService, private modalService: BsModalService) { } ngOnInit() { this.config = JSON.parse(localStorage.getItem('config')); this.user = JSON.parse(localStorage.getItem('user')); this.cartService.getItemsInCart().subscribe(result => { this.itemsInCart = JSON.parse(result) || []; this.subTotalDiscountPrice = this.cartService.getSum(this.itemsInCart, "totalDiscountPrice"); this.calculateUpfrontDiscount(); this.subTotalSalesPrice = this.cartService.getSum(this.itemsInCart, "totalSalesPrice"); this.cartItemsCount = this.cartService.getSum(this.itemsInCart, "numItems"); this.calculateHandlingFee(); this.checkOutAmount = this.subTotalDiscountPrice + this.handlingFee - this.upfrontDiscount; this.calculateAvailableWalletAmount(); }); this.cartService.getIsWalletAmountUsed().subscribe(isUsed => { this.isWalletAmountUsed = isUsed; }) this.cartService.getUsedWalletAmount().subscribe(amount => { this.usedWalletAmount = amount; }) } private calculateUpfrontDiscount() { let config = this.config; if (config) { if (config.upfront_discount) { if (this.subTotalDiscountPrice >= config.upfront_min_cart_value) { this.upfrontDiscount = config.upfront_discount_value; } else this.upfrontDiscount = 0; } } } private calculateHandlingFee() { let config = this.config; if (config) { if (this.subTotalDiscountPrice < config.handling_min_cart_value) this.handlingFee = config.handling_fee; else this.handlingFee = 0; } } useWalletOption(event) { this.cartService.setIsWalletAmountUsed(event.srcElement.checked); if (this.isWalletAmountUsed && this.isWalletAmountAvailable) { if (this.checkOutAmount < this.max_wallet_applied) this.cartService.setUsedWalletAmount(this.checkOutAmount); else this.cartService.setUsedWalletAmount(this.max_wallet_applied); } else { this.cartService.setUsedWalletAmount(0); this.cartService.setIsWalletAmountUsed(false); } } calculateAvailableWalletAmount() { let config = this.config; let user = this.user; if (user && user.wallet && config.wallet_credit_discount && this.checkOutAmount >= config.wallet_dicount_min_cart) { this.isWalletAmountAvailable = true; this.max_wallet_applied = config.max_wallet_applied; } else { this.isWalletAmountAvailable = false; this.max_wallet_applied = 0; this.cartService.setUsedWalletAmount(0); this.cartService.setIsWalletAmountUsed(false); } } updateItemsInCart(product, operationType) { this.cartService.updateItemsInCart(product, operationType); }; //On click of checkout button checkout() {  let user = JSON. parse(localStorage.getItem('user')); if(user === null) { this.login(); } else { const payload = { itemsInCart: this.itemsInCart, user: this.user, checkOutAmount: this.checkOutAmount, isWalletAmountUsed: this.isWalletAmountUsed, subTotalDiscountPrice: this.subTotalDiscountPrice, subTotalSalesPrice: this.subTotalSalesPrice, upfrontDiscount:this.upfrontDiscount, handlingFee: this.handlingFee, usedWalletAmount: this.usedWalletAmount } this.cartService.checkout(payload).subscribe( response => { const data: any = response as any[]; if (data.status === 'success') { this.toastr.success(data.message); this.clearCart(); } else { this.toastr.error(data.message); } }); } } clearCart() { this.itemsInCart = []; localStorage.cartItems = JSON.stringify(this.itemsInCart); this.cartService.setItemsInCart(JSON.stringify(this.itemsInCart)) } /* @usage: called click on login button @purpose:To open the login Dialog. */ login() { this.bsModalRef = this.modalService.show(LoginModalComponent, { initialState: { title: 'Login', } }); this.bsModalRef.content.event.subscribe(modelResult => { if (modelResult) { if (localStorage.getItem('user')) { //write logic for checkout after login } } }); } }