CouponsTableViewCell.swift 3.5 KB
//
//  CouponsTableViewCell.swift
//  SwiftWarplyFramework
//
//  Created by Manos Chorianopoulos on 18/5/22.
//

import Foundation
import UIKit

@objc public class CouponsTableViewCell: UITableViewCell {
    @IBOutlet weak var couponBgImage: UIImageView!
    @IBOutlet weak var couponImage: UIImageView!
    @IBOutlet weak var borderView: UIView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var dicountLabel: UILabel!
    @IBOutlet weak var discriptionLabel: UILabel!
    @IBOutlet weak var expirationLabel: UILabel!

    var postImageURL: String? {
        didSet {
            if let url = postImageURL {
                self.couponImage.image = UIImage() // UIImage(named: "loading")

                UIImage.loadImageUsingCacheWithUrlString(url) { image in
                    // set the image only when we are still displaying the content for the image we finished downloading
                    if url == self.postImageURL {
                        self.couponImage.image = image
                    }
                }
            }
            else {
                self.couponImage.image = nil
            }
        }
    }
    
    
    public override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        couponBgImage.image = UIImage(named: "coupon_bg", in: Bundle(for: MyEmptyClass.self), compatibleWith: nil)
        
        borderView.addDashedBorderVertical(color: UIColor(red: 0.44, green: 0.44, blue: 0.44, alpha: 1.00), width: 1.0, height: 110.0)
    }

    public override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        //set the values for top,left,bottom,right margins
        let margins = UIEdgeInsets(top: 0, left: 0, bottom: 8, right: 0)
        contentView.frame = contentView.frame.inset(by: margins)
    }
    
    
    func configureCell(coupon: swiftApi.CouponItemModel) {
//        COUPONSET: desc, img_preview, name, terms, merchant_uuid, discount_type, final_price
//        COUPON: coupon, expiration, discount, status
//        MERCHANT: _img_preview,_admin_name
        
        let merchantList:Array<swiftApi.MerchantModel> = swiftApi().getMerchantList()
        
        let couponSetData: swiftApi.CouponSetItemModel? = coupon.couponset_data
        
        nameLabel.text = ""
        
        for merchant in merchantList {
            if (merchant._uuid == couponSetData?.merchant_uuid) {
                // couponImage.load(link: merchant._img_preview, placeholder: UIImage(), cache: URLCache())
                self.postImageURL = merchant._img_preview
                nameLabel.text = merchant._admin_name
                break;
            }
        }
        
//        couponImage.load(link: couponSetData?.img_preview ?? "", placeholder: UIImage(), cache: URLCache())
//        nameLabel.text = couponSetData?.name ?? ""
        let discount = couponSetData?.discount_type == "value" ? ((coupon.discount ?? "") + "€")
        : couponSetData?.discount_type == "percentage" ? ((coupon.discount ?? "") + "%")
        : couponSetData?.discount_type == "plus_one" ? "1+1"
        : ((coupon.discount ?? "") + "€")
        dicountLabel.text = discount
        let htmlText = couponSetData?.inner_text ?? ""
        discriptionLabel.text = htmlText.htmlToString
        expirationLabel.text = "Ισχύει έως "+(coupon.expiration ?? "")
    }
    
}