CNLiveShopGoodsDetailsTableHeaderView.swift 13 KB
//
//  CNLiveShopGoodsDetailsTableHeaderView.swift
//  metaCode
//
//  Created by zx on 2024/3/13.
//

import Foundation
import YYImage
import BKCycleScrollView

typealias clickGiveGiftCallBackBlock = () -> Void

// MARK: - 顶部轮播图
class CNLiveShopGoodsDetailsTableHeaderView:UIView{
    
    let kCNLliveShopDetailBannerCellID = "kCNLliveShopDetailBannerCellID"
    
    /// 顶部轮播图
    lazy var bannerView: BKCycleScrollView = {
        let bannerView = BKCycleScrollView(frame: CGRect(x: 0, y: 0, width: self.width, height: self.width))
//        bannerView.placeholderImage = [UIImage get1_1PlaceholderImage];
        bannerView.isAutoScroll = false
        return bannerView
    }()
    
    /// 数据
    var _detailsModel:CNLiveShopGoodsDetailsModel?
    var detailsModel:CNLiveShopGoodsDetailsModel?{
        get{
            _detailsModel
        }
        set{
            _detailsModel = newValue!
            let bannerDataArr = NSMutableArray()
            if _detailsModel?.video?.video_url?.count ?? 0 > 0{
                let model = BKCycleScrollDataModel()
                model.videoUrl = _detailsModel!.video!.video_url!
                model.imageUrl = _detailsModel?.video?.video_img ?? ""
                model.image = _detailsModel?.video?.video_image ?? UIImage()
                bannerDataArr.add(model)
            }
            for imageModel in detailsModel!.image {
                let model = BKCycleScrollDataModel()
                model.imageUrl = imageModel.simg ?? ""
                bannerDataArr.add(model)
            }
            self.bannerView.displayDataArr = bannerDataArr as? [BKCycleScrollDataModel]
            self.titleLab.text = self.detailsModel?.title
            self.titleLab.height = self.titleLab.text?.calculateHeightWith(width: self.titleLab.width, font: self.titleLab.font) ?? 0
            self.subtitleLab.text = self.detailsModel?.ads
            self.subtitleLab.y = self.titleLab.bottom+15
            self.subtitleLab.height = self.subtitleLab.text?.calculateHeightWith(width: self.titleLab.width, font: self.titleLab.font) ?? 0
            self.initTopStyleSubUI()
            self.initGuigeSubUI()
            self.initBottomDescSubUI()
            self.height = self.bottomDescView.bottom
        }
    }
    
    /**
     点击送礼回调
     */
    var clickGiveGiftBlock:clickGiveGiftCallBackBlock?
    lazy var titleLab: UILabel = {
        let titleLab = UILabel.init(frame: CGRectMake(20, self.bannerView.bottom+15, self.width-40, 0))
        titleLab.textColor = HexColor("#3a3a3a")
        titleLab.textAlignment = .center
        titleLab.numberOfLines = 0
        titleLab.font = Font_17
        titleLab.adjustsFontSizeToFitWidth=true
        return titleLab
    }()

    lazy var subtitleLab: UILabel = {
        let subtitleLab = UILabel.init(frame: CGRectMake(20, self.titleLab.bottom+15, self.width-40, 0))
        subtitleLab.textColor = HexColor("#828282")
        subtitleLab.textAlignment = .center
        subtitleLab.numberOfLines = 0
        subtitleLab.font = Font_14
        subtitleLab.adjustsFontSizeToFitWidth=true
        return subtitleLab
    }()

    lazy var topStyleView :UIView = {
        let topStyleView = UIView(frame: CGRect(x: 0, y: self.subtitleLab.bottom+15, width: self.width, height: 0))
        return topStyleView
    }()

    lazy var guigeView :UIView = {
        let guigeView = UIView(frame: CGRect(x: 0, y: self.topStyleView.bottom, width: self.width, height: 0))
        return guigeView
    }()

    lazy var bottomDescView :UIView = {
        let bottomDescView = UIView(frame: CGRect(x: 0, y: self.guigeView.bottom, width: self.width, height: 0))
        return bottomDescView
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        self.initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        self.addSubview(self.bannerView)
        self.addSubview(self.titleLab)
        self.addSubview(self.subtitleLab)
        self.addSubview(self.topStyleView)
        self.addSubview(self.guigeView)
        self.addSubview(self.bottomDescView)
    }
   
    func initTopStyleSubUI(){
        self.topStyleView.subviews.forEach { $0.removeFromSuperview() }
        self.topStyleView.y = (self.detailsModel?.ads?.count ?? 0 > 0 ? self.subtitleLab.bottom : self.titleLab.bottom)+15
        
        let priceLabel = UILabel(frame: CGRect(x: 20, y: 0, width: KSreenWidth-20*2, height: 55))
        priceLabel.textColor = HexColor("#ff725c")
        priceLabel.font = SystemFont_Fit(45)
        priceLabel.text = String(format: "¥%@", self.detailsModel?.price ?? 0)
        priceLabel.textAlignment = .center
        self.topStyleView.addSubview(priceLabel)
        self.topStyleView.height = 55
        
        if self.detailsModel?.prices?.count ?? 0 > 0 && Float(self.detailsModel?.prices ?? "0") ?? 0 > Float(self.detailsModel?.price ?? "0") ?? 0{
            let originalPriceLab = UILabel(frame: CGRect(x: 20, y: self.topStyleView.height, width: KSreenWidth-20*2, height: 20))
            originalPriceLab.textColor = HexColor("#999999")
            originalPriceLab.font = Font_14
            originalPriceLab.textAlignment = .center
            let originalPrice = NSAttributedString(string: String(format: " ¥%@  ", self.detailsModel?.prices ?? "0"), attributes: [NSAttributedString.Key.strikethroughStyle:NSUnderlineStyle.single ])
            originalPriceLab.attributedText = originalPrice
            self.topStyleView.addSubview(originalPriceLab)
            self.topStyleView.height = self.topStyleView.height+20
        }
        if self.detailsModel?.is_gift == true{
            let fAdsImageView = YYAnimatedImageView(frame: CGRect(x: 0, y: self.topStyleView.height+15, width: self.width, height: 50))
            fAdsImageView.clipsToBounds = true
            fAdsImageView.contentMode = .scaleAspectFill
            fAdsImageView.isUserInteractionEnabled = true
            fAdsImageView.kf.setImage(with: URL(string: self.detailsModel?.give_goods_img ?? ""))
            self.topStyleView.addSubview(fAdsImageView)
            
            let giveGoodsTap = UITapGestureRecognizer(target: self, action: #selector(giveGoodsTap))
            fAdsImageView.addGestureRecognizer(giveGoodsTap)
            self.topStyleView.height = self.topStyleView.height + 15 + 50
        }
        self.topStyleView.height = self.topStyleView.height + 20
        let line = YYAnimatedImageView(frame: CGRect(x: 20, y: self.topStyleView.height-DS_RRV_ONE_PIXEL, width: self.topStyleView.width-40, height: DS_RRV_ONE_PIXEL))
        line.backgroundColor = HexColor("#e4e4e4")
        self.topStyleView.addSubview(line)
    }

    func initGuigeSubUI(){
        self.guigeView.y = self.topStyleView.bottom
        self.guigeView.subviews.forEach { $0.removeFromSuperview() }
        
        let titleDataArr = NSMutableArray()
        if self.detailsModel?.attr.count ?? 0 > 0{
            for i in 0..<(self.detailsModel?.attr.count ?? 0){
                let model = self.detailsModel?.attr[i]
                if model?.k?.count ?? 0 > 0 && model?.v?.count ?? 0 > 0{
                    titleDataArr.add(["left":model!.k,"right":model!.v])
                }
            }
        }
        if self.detailsModel?.attrArray.count ?? 0 > 0{
            for i in 0..<(self.detailsModel?.attrArray.count ?? 0){
                let dict = self.detailsModel?.attrArray[i]
                let k = dict?["k"] as? String
                let v = dict?["v"] as? String
                if k?.count ?? 0 > 0 && v?.count ?? 0 > 0{
                    titleDataArr.add(["left":(dict?["k"] as! String),"right":(dict?["v"] as! String)])
                }
            }
        }
        if self.detailsModel?.storageArray.count ?? 0 > 0{
            for i in 0..<(self.detailsModel?.storageArray.count ?? 0){
                let dict = self.detailsModel?.storageArray[i]
                let k = dict?["k"] as? String
                let v = dict?["v"] as? String
                if k?.count ?? 0 > 0 && v?.count ?? 0 > 0{
                    titleDataArr.add(["left":(dict?["k"] as! String),"right":(dict?["v"] as! String)])
                }
            }
        }
        if titleDataArr.count == 0{
            self.guigeView.height = 0
            return
        }
        let markLine = YYAnimatedImageView(frame: CGRect(x: 120, y: 0, width: self.guigeView.width-240, height: DS_RRV_ONE_PIXEL))
        markLine.backgroundColor = HexColor("#b4b4b4")
        self.guigeView.addSubview(markLine)
        
        let markLab = UILabel(frame: CGRect(x: markLine.left+(markLine.width-87.5)/2, y: 17, width: 87.5, height: 20))
        markLab.textColor = HexColor("#666666")
        markLab.textAlignment = .center
        markLab.numberOfLines = 0
        markLab.font = Font_14
        markLab.backgroundColor = .white
        markLab.text = "已选规格"
        self.guigeView.addSubview(markLab)
        
        markLine.centerY = markLab.centerY
        var lastView:UIView?
        for i in 0..<titleDataArr.count {
            let dic = titleDataArr[i] as! Dictionary<String, Any>
            let view = UIView(frame: CGRect(x: 20, y: (lastView != nil) ? lastView!.bottom : (markLab.bottom+13), width: self.guigeView.width-40, height: 35))
            self.guigeView.addSubview(view)
            
            let leftLab = UILabel(frame: CGRect(x: 25, y: 0, width: 60, height: 35))
            leftLab.textColor = HexColor("#6d6d6d")
            leftLab.textAlignment = .center
            leftLab.numberOfLines = 0
            leftLab.font = Font_14
            leftLab.text = dic["left"] as? String
            view.addSubview(leftLab)
            leftLab.height = (leftLab.text?.calculateHeightWith(width: leftLab.width, font:leftLab.font) ?? 0) + 15
            
            let rightLab = UILabel(frame: CGRect(x: 125, y: 0, width: 195, height: 35))
            rightLab.textColor = HexColor("#aaaaaa")
            rightLab.textAlignment = .center
            rightLab.numberOfLines = 0
            rightLab.font = Font_14
            rightLab.text = dic["right"] as? String
            view.addSubview(rightLab)
            rightLab.height = (rightLab.text?.calculateHeightWith(width: rightLab.width, font: rightLab.font) ?? 0) + 15
            
            if rightLab.height > leftLab.height{
                view.height = rightLab.height
            }else{
                view.height = leftLab.height
            }
            let line = YYAnimatedImageView(frame: CGRect(x: 0, y: 0, width: view.width, height: DS_RRV_ONE_PIXEL))
            line.backgroundColor = HexColor("#d4d4d4")
            view.addSubview(line)
            lastView = view
        }
        
        let leftLine = YYAnimatedImageView(frame: CGRect(x: lastView!.x, y: markLab.bottom+13, width: DS_RRV_ONE_PIXEL, height: lastView!.bottom-(markLab.bottom+13)))
        leftLine.backgroundColor = HexColor("#d4d4d4")
        self.guigeView.addSubview(leftLine)
        
        let middleLine = YYAnimatedImageView(frame: CGRect(x: lastView!.x+108, y: markLab.bottom+13, width: DS_RRV_ONE_PIXEL, height: lastView!.bottom-(markLab.bottom+13)))
        middleLine.backgroundColor = HexColor("#d4d4d4")
        self.guigeView.addSubview(middleLine)
        
        let rightLine = YYAnimatedImageView(frame: CGRect(x: lastView!.right-DS_RRV_ONE_PIXEL, y: markLab.bottom+13, width: DS_RRV_ONE_PIXEL, height: lastView!.bottom-(markLab.bottom+13)))
        rightLine.backgroundColor = HexColor("#d4d4d4")
        self.guigeView.addSubview(rightLine)
        
        let bottomLine = YYAnimatedImageView(frame: CGRect(x: lastView!.x, y: lastView!.bottom, width: lastView!.width, height: DS_RRV_ONE_PIXEL))
        bottomLine.backgroundColor = HexColor("#d4d4d4")
        self.guigeView.addSubview(bottomLine)
        
        let line = YYAnimatedImageView(frame: CGRect(x: 20, y: bottomLine.bottom+20, width: self.guigeView.width-40, height: DS_RRV_ONE_PIXEL))
        line.backgroundColor = HexColor("#e4e4e4")
        self.guigeView.addSubview(line)
        self.guigeView.height = line.bottom
    }

    func initBottomDescSubUI(){
        self.bottomDescView.y = self.guigeView.bottom
        self.bottomDescView.subviews.forEach { $0.removeFromSuperview() }

        let markLine = YYAnimatedImageView(frame: CGRect(x: 120, y: 0, width: self.bottomDescView.width-240, height: DS_RRV_ONE_PIXEL))
        markLine.backgroundColor = HexColor("#b4b4b4")
        self.bottomDescView.addSubview(markLine)
        
        let markLab = UILabel.init(frame: CGRectMake(markLine.left+(markLine.width-87.5)/2, 17, 87.5, 20))
        markLab.textColor = HexColor("#666666")
        markLab.textAlignment = .center
        markLab.numberOfLines = 0
        markLab.font = Font_14
        markLab.backgroundColor = .white
        markLab.text = "商品详情"
        self.bottomDescView.addSubview(markLab)
        
        markLine.centerY = markLab.centerY
        self.bottomDescView.height = markLab.bottom+20
    }
    
    @objc func giveGoodsTap(){
        if (self.clickGiveGiftBlock != nil){
            self.clickGiveGiftBlock!()
        }
    }
}