CNLiveShopClassifySectionHeaderView.swift 5.18 KB
//
//  CNLiveShopClassifySectionHeaderView.swift
//  metaCode
//
//  Created by zx on 2023/12/12.
//

import Foundation


typealias showChildCalssifyActionBlock = (_ section:Int ,_ classifyModel:CNLiveShopClassifyModel)->Void
typealias addSubClassifyBtnActionBlock = (_ classifyModel:CNLiveShopClassifyModel)->Void
typealias editBtnActionBlock = (_  classifyModel:CNLiveShopClassifyModel)->Void

class CNLiveShopClassifySectionHeaderView:UITableViewHeaderFooterView{
    ///当前分区
    var section:Int = 0

    /// 分类model
    var _classifyModel:CNLiveShopClassifyModel?
    var classifyModel:CNLiveShopClassifyModel?{
        get{
            _classifyModel
        }
        set{
            _classifyModel = newValue
            self.classifyNameBtn.isSelected = _classifyModel!.showChild
            self.classifyNameBtn.setTitle(_classifyModel!.title, for: .normal)
            self.classifyNameBtn.setupButtonImageAndTitlePossitionWith(padding: 5, style: .right)

            self.sortLab.text = _classifyModel!.ord
            if Int(_classifyModel!.state ?? "") == 2{
                self.showSwitch.isOn = true
            }else{
                self.showSwitch.isOn = false
            }
        }
    }
    
    ///点击显示/隐藏 子分类事件回调
    var showChildCalssifyBlock:showChildCalssifyActionBlock?

    ///点击添加子分类事件回调
    var addSubClassifyBtnBlock:addSubClassifyBtnActionBlock?

    ///点击编辑事件回调
    var editBtnBlock:editBtnActionBlock?

    ///分类名称
    lazy var classifyNameBtn: UIButton = {
        let  classifyNameBtn = UIButton(type: .custom)
        classifyNameBtn.frame = CGRect(x: 0, y: 0, width: KSreenWidth*0.3, height: 50)
        classifyNameBtn.setImage(UIImage(named: "DS_shopclassily_down"), for: .normal)
        classifyNameBtn.setImage(UIImage(named: "DS_shopclassily_up"), for: .selected)
        classifyNameBtn.setTitleColor(HexColor("#333333"), for: .normal)
        classifyNameBtn.setTitle("分类", for: .normal)
        classifyNameBtn.setupButtonImageAndTitlePossitionWith(padding: 5, style: .right)
        classifyNameBtn.titleLabel?.font = Font_14
        classifyNameBtn.addTarget(self, action: #selector(showChildCalssifyAction), for: .touchUpInside)
        return classifyNameBtn
    }()

    ///排序名称
    lazy var sortLab: UILabel = {
        let sortLab = UILabel.init(frame: CGRectMake(self.classifyNameBtn.right, 0, KSreenWidth*0.2, 50))
        sortLab.text = "排序"
        sortLab.textColor = HexColor("#333333")
        sortLab.textAlignment = .center
        sortLab.font = Font_14
        sortLab.adjustsFontSizeToFitWidth=true
        return sortLab
    }()

    ///是否显示
    lazy var showSwitch :UISwitch = {
        let showSwitch = UISwitch(frame: CGRect(x: self.sortLab.right+10, y: 10, width: KSreenWidth*0.2, height: 50))
        showSwitch.isEnabled = false
        return showSwitch
    }()

    ///添加子分类
    lazy var addSubClassifyBtn: UIButton = {
        let  addSubClassifyBtn = UIButton(type: .custom)
        addSubClassifyBtn.frame = CGRect(x: self.showSwitch.right, y: 0, width: KSreenWidth*0.21, height: 50)
        addSubClassifyBtn.setTitle("添加子类", for: .normal)
        addSubClassifyBtn.setTitleColor(HexColor("#0091FF"), for: .normal)
        addSubClassifyBtn.titleLabel?.font = Font_14
        addSubClassifyBtn.addTarget(self, action: #selector(addSubClassifyBtnAction), for: .touchUpInside)
        return addSubClassifyBtn
    }()

    ///编辑
    lazy var editBtn: UIButton = {
        let  editBtn = UIButton(type: .custom)
        editBtn.frame = CGRect(x: self.addSubClassifyBtn.right, y: 0, width: KSreenWidth*0.09, height: 50)
        editBtn.setTitle("编辑", for: .normal)
        editBtn.setTitleColor(HexColor("#0091FF"), for: .normal)
        editBtn.titleLabel?.font = Font_14
        editBtn.addTarget(self, action: #selector(editBtnAction), for: .touchUpInside)
        return editBtn
    }()

    /// 底部线
    lazy var bottomLine: UILabel = {
        let bottomLine = UILabel.init(frame: CGRectMake(0, 49, KSreenWidth, 1))
        bottomLine.backgroundColor = HexColor("#E4E4E4")
        return bottomLine
    }()

    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        self.initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 初始化UI
    func initUI(){
        self.contentView.backgroundColor = .white
        self.contentView.addSubview(self.classifyNameBtn)
        self.contentView.addSubview(self.sortLab)
        self.contentView.addSubview(self.showSwitch)
        self.contentView.addSubview(self.addSubClassifyBtn)
        self.contentView.addSubview(self.editBtn)
        self.contentView.addSubview(self.bottomLine)
    }
    // MARK: - response method
    ///显示子分类
    @objc func showChildCalssifyAction(){
        self.showChildCalssifyBlock!(self.section, self.classifyModel!)
    }
    ///添加子分类
    @objc func addSubClassifyBtnAction(){
        self.addSubClassifyBtnBlock!(self.classifyModel!)
    }

    /// 编辑
    @objc func editBtnAction(){
        self.editBtnBlock!(self.classifyModel!)
    }
}