// // AutoRefundsViewController.swift // GME Remit // // Created by Mac on 11/20/18. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit class AutoRefundsViewController: UIViewController { struct StringConstants { let headerTitleText = "how_much_would_you_like_to_refund".localized() let availableBalanceTitleText = "available_balance_text".localized() let refundChargeTitleTxty = "per_refund_bank_charge".localized() let navigationTitle = "auto_refund_title_text".localized() let yourRefundText = "you_refund_text".localized() let refundButtonText = "refund_text".localized() } @IBOutlet weak var backgroundView: UIView! @IBOutlet weak var countryBackgroundView: UIView! @IBOutlet weak var availableBalanceTitleLabel: UILabel! @IBOutlet weak var refundChargeTitleLabel: UILabel! @IBOutlet weak var yourRefundLabel: UILabel! @IBOutlet weak var refundButton: UIButton! @IBOutlet weak var availableBalanceLabel: UILabel! @IBOutlet weak var refundChargeLabel: UILabel! @IBOutlet weak var amountTextfield: UITextField! var autorefund: AutoRefund? { didSet { let availableBalance = (autorefund?.currentBalance ?? "0") + "(KRW)" let refundCharge = (autorefund?.chargeAmount ?? "10,000") + "(KRW)" self.availableBalanceLabel.text = availableBalance self.refundChargeLabel.text = refundCharge } } override func viewDidLoad() { super.viewDidLoad() setup() // Do any additional setup after loading the view. } private func setup() { setupNormalNavigation() self.backgroundView.layer.borderColor = UIColor.init(hex: "E0E0E0").cgColor self.backgroundView.layer.borderWidth = 0.5 self.backgroundView.layer.cornerRadius = 6 self.countryBackgroundView.layer.cornerRadius = 6 configureText() } private func configureText() { self.availableBalanceTitleLabel.text = StringConstants().availableBalanceTitleText + " : " self.refundChargeLabel.text = StringConstants().refundChargeTitleTxty + " : " self.yourRefundLabel.text = StringConstants().yourRefundText self.refundButton.setTitle(StringConstants().refundButtonText, for: UIControlState.normal) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.navigationItem.title = StringConstants().navigationTitle fetchInfo() } private func fetchInfo() { self.showProgressHud() let username = Utility.getMyUserName() self.fetchAutoRefundInfo(username: username, success: { (autorefund) in self.autorefund = autorefund self.hideProgressHud() }) { (error) in self.hideProgressHud() self.show(error: error.localizedDescription) } } @IBAction func refund(_ sender: Any) { let amount = self.amountTextfield.text! let minAmnt = self.autorefund?.minAmount ?? "0" if amount.isEmpty {self.show(error: "add refund amount"); return} if !isValidRefundable(amount: amount) { self.show(error: "Amount cannot be minimum than \(Utility.getCommaSeperatedString(numberString: minAmnt) ?? "")") return } let chargeAmt = self.autorefund?.chargeAmount ?? "" let userName = Utility.getMyUserName() let id = Utility.getMyId() self.showProgressHud() self.refund(amount: amount, userName: userName, chargeAmount: chargeAmt, userId: id, success: { (successMessage) in self.hideProgressHud() self.alertWithOk(message: successMessage?.message, title: "Success", okTitle: "Ok", style: UIAlertControllerStyle.alert, OkStyle: UIAlertActionStyle.default, okAction: { self.navigationController?.popViewController(animated: true) self.updateBalance(value: successMessage) }) }) { (error) in self.hideProgressHud() self.show(error: error.localizedDescription) } } private func updateBalance(value: SuccessMessage?) { let balance = value?.extra ?? "" let yearlyLimits = value?.yearlyLimit ?? "" UserDefaults.standard.set(yearlyLimits, forKey: UserKeys.yearlyLimit) UserDefaults.standard.set(balance, forKey: UserKeys.availableBalance) let userInfo = [SideMenuNavigationNotifications.availableBalance : balance] NotificationCenter.default.post(name: self.getAvailableBalanceNotificationName(), object: nil, userInfo: userInfo) } func getAvailableBalanceNotificationName() -> Notification.Name { return Notification.Name.init(rawValue: SideMenuNavigationNotifications.availableBalance) } private func isValidRefundable(amount: String) -> Bool { let _minRefundAmount = self.autorefund?.minAmount ?? "0" let minRefundAmount = _minRefundAmount.replacingOccurrences(of: ",", with: "") print(minRefundAmount) guard let amt = Double(amount), let minRefundAmt = Double(minRefundAmount) else {return false} let result = amt >= minRefundAmt return result } func show(error: String) { self.alert(message: error) } } extension AutoRefundsViewController: FetchAutoRefundInfo { } extension AutoRefundsViewController: RefundService { }