// // CartPaymentOptionsVM.swift // WOKA // // Created by MacBook Pro on 23/07/24. // import UIKit import Alamofire class CartPaymentOptionsVM{ weak var vc : CartPaymentOptionsVC! // var cartListData = [CartListingDM.ResultData]() var couponData = [CouponListDM.ResultData]() var isExpanded = false var cartTotalPrice = 0.0 var couponCodeSelected = "" var couponCodeApplied = String() func initView(){ vc.couponCodeTF.roundCorner() addGradient() vc.scrollView.indicatorStyle = .white setupCell() updateCart() getCouponAndOffers() self.vc.totalCartPrice.text = K.ConstantString.rupeeSign + " " + cartTotalPrice.toString() } func setupCell(){ vc.cartTableView.register(UINib(nibName: K.CellIdentifier.Cart.cartPaymentOptionsCell, bundle: nil), forCellReuseIdentifier: K.CellIdentifier.Cart.cartPaymentOptionsCell) vc.cartTableView.delegate = vc.self vc.cartTableView.dataSource = vc.self vc.couponTableView.register(UINib(nibName: K.CellIdentifier.Cart.couponCell, bundle: nil), forCellReuseIdentifier: K.CellIdentifier.Cart.couponCell) vc.couponTableView.delegate = vc.self vc.couponTableView.dataSource = vc.self } func addGradient(){ self.vc.title = "PAYMENT OPTIONS".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue) let color1 = #colorLiteral(red: 0.6745098039, green: 0.6235294118, blue: 0.1725490196, alpha: 1) let color2 = #colorLiteral(red: 0.5450980392, green: 0.6745098039, blue: 0.1725490196, alpha: 1) vc.view.applyGradient(colors: [color2, color1], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0.8, y: 0)) } func updateCart(){ guard let self = self.vc else{return} self.cartTableView.reloadData() self.cartTableHeight.constant = self.cartTableView.contentSize.height + 1000 self.cartTableView.layoutIfNeeded() self.cartTableHeight.constant = self.cartTableView.contentSize.height } // MARK: - Get SuperCategory func getCouponAndOffers(){ let headers : HTTPHeaders = ["access-token" : AuthFunc.shareInstance.getAccessToken()] NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Cart.coupon_listing, method: .get,headers: headers) { [weak self](result : Result, NetworkManager.APIError>) in switch result{ case .success(let data): guard let self else{ Utilities.dismissProgressHUD() return } switch data.success{ case 0: /* Error */ Utilities.dismissProgressHUD() self.vc.noCouponFound.isHidden = false case 1: Utilities.dismissProgressHUD() guard let data = data.data?.result else{return} self.couponData = data self.vc.noCouponFound.isHidden = true self.vc.couponTableView.reloadData() self.vc.couponTableHeght.constant = self.vc.couponTableView.contentSize.height + 100 self.vc.couponTableView.layoutIfNeeded() self.vc.couponTableHeght.constant = self.vc.couponTableView.contentSize.height default: Utilities.dismissProgressHUD() break } case .failure(let error): guard let self else{ Utilities.dismissProgressHUD() return } Utilities.dismissProgressHUD() Utilities.alertWithBtnCompletion(title: "Error", msgBody: error.localizedDescription, okBtnStr: "Retry?", vc: self.vc) { isDone in if isDone{ self.getCouponAndOffers() } } } } } func applyCoupon(code : String){ Utilities.startProgressHUD() let headers : HTTPHeaders = ["access-token" : AuthFunc.shareInstance.getAccessToken()] let params : Parameters = ["coupon_code" : code, "total_amount" : self.cartTotalPrice] NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Cart.applied_coupon_discount, method: .post,parameters: params,headers: headers) { [weak self](result : Result, NetworkManager.APIError>) in switch result{ case .success(let data): guard let self else{ Utilities.dismissProgressHUD() return } switch data.success{ case 0: /* Error */ Utilities.dismissProgressHUD() case 1: Utilities.dismissProgressHUD() guard let data = data.data else{return} self.vc.subtotalPrice.text = data.cartTotalAmount self.vc.discountPrice.text = data.discountValue self.vc.totalCartPrice.text = data.cartDiscountedAmount self.vc.couponAplliedStack.isHidden = false self.couponCodeApplied = code default: Utilities.dismissProgressHUD() break } case .failure(let error): guard let self else{ Utilities.dismissProgressHUD() return } Utilities.dismissProgressHUD() Utilities.alertWithBtnCancelCompletion(title: "Error", msgBody: error.localizedDescription, okBtnStr: "Retry?", vc: self.vc) { isDone in if isDone{ self.applyCoupon(code: self.vc.couponCodeTF.text ?? "") } } } } } }