CNLiveShopAddGoodsSingleView.swift 14.9 KB
//
//  CNLiveShopAddGoodsSingleView.swift
//  metaCode
//
//  Created by zx on 2023/11/8.
//

import Foundation

///回调
typealias updateBlock = ()->Void
typealias tapClickBlock = (_ singleView:CNLiveShopAddGoodsSingleView)->Void

class CNLiveShopAddGoodsSingleView: UIView, UITextFieldDelegate {
    ///
    var updateFrameBlock:updateBlock?
    //
    var tapBlock:tapClickBlock?
    //model
    var _addEditGoodsListModel : CNLiveShopAddEditGoodsListModel?
    var addEditGoodsListModel : CNLiveShopAddEditGoodsListModel? {
        get{
            _addEditGoodsListModel
        }
        set{
            _addEditGoodsListModel = newValue!
            self.setupNewUI(model: _addEditGoodsListModel!)
        }
    }
    
    ///标题
    lazy var titleLabel: UILabel = {
        let titleLabel = UILabel.init(frame: CGRectMake(15 , 15, 145, 20))
//        titleLabel.textColor = HexColor("#333333")
//        titleLabel.textAlignment = .left
//        titleLabel.font = Font_15
//        titleLabel.adjustsFontSizeToFitWidth=true
//        titleLabel.text = "啊啊啊啊啊"
        return titleLabel
    }()
    //副标题
    var subButton = UIButton()
    var contentTextField = UITextField()
    var delectButton = UIButton()
    var rightImageView = UIImageView()
    var contentSwitch = UISwitch()
    var uploadImageView = CNLiveAddGoodsSelectUploadImageView(frame: CGRectZero)
    var contentTextBgView = UIView()
    var contentTextView = UITextView()//
    var addBtn = UIButton()
    var subtractBtn = UIButton()
    var countTF = UITextField()
    var descLab = UILabel()
    var line = UIImageView()
    var starImageView = UIImageView()
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUI()
    }
    
    func setUI(){

    }
    
    func setupNewUI(model:CNLiveShopAddEditGoodsListModel){
        self.backgroundColor = model.style == "2" ? HexColor("#F6F6F6") : HexColor("#FFFFFF")
        if (model.isHaveTo ?? false) {
            self.starImageView = UIImageView()
            self.starImageView.image = UIImage(named: "DS_shop_manageGoodsDetails_star")
            self.starImageView.clipsToBounds = true
            self.starImageView.contentMode = .scaleAspectFit
            self.addSubview(self.starImageView)
        }
        self.titleLabel = UILabel()
        if (self.addEditGoodsListModel?.style == "2") {
            
            self.titleLabel.textColor = HexColor("#9D9D9D")
            self.titleLabel.text = self.addEditGoodsListModel?.title
            if (self.addEditGoodsListModel!.touchSubtitle?.count ?? 0 > 0) {
                let  subBtn = UIButton(type: .custom)
                subBtn.frame = CGRect(x: self.width - 50, y: 0, width: 50, height: 50)
                subBtn.setTitle("添加通用参数", for: .normal)
                subBtn.setTitleColor(HexColor("#0091FF"), for: .normal)
                subBtn.titleLabel?.font = Font_14
                subBtn.titleLabel?.textAlignment = .right
                subBtn.addTarget(self, action: #selector(subBtnAction), for: .touchUpInside)
                self.subButton = subBtn
                self.addSubview(self.subButton)
            }
        }else
        {
            self.titleLabel.textColor = HexColor("#333333")
            if (self.addEditGoodsListModel?.subtitle?.count ?? 0 > 0) {
                self.titleLabel.text = "\(self.addEditGoodsListModel?.title ?? "")+\(self.addEditGoodsListModel?.subtitle ?? "")"
            }else{
                self.titleLabel.text = self.addEditGoodsListModel?.title ?? ""
            }
        }
        self.titleLabel.font = Font_14
        self.addSubview(self.titleLabel)
        
        if (self.addEditGoodsListModel?.style == "1") {
            let  contentTextField = UITextField()
            contentTextField.returnKeyType = .done
            contentTextField.placeholder = "请输入"
            contentTextField.font = Font_14
            contentTextField.textColor = HexColor(model.contentTextColor ?? "#787878")
            contentTextField.delegate = self
            
            if (model.content?.count ?? 0 > 0){ contentTextField.text = model.content
            }
                
            if (model.contentTextFieldkeyboardType == "2") {
                contentTextField.keyboardType = .decimalPad
            }else {
                contentTextField.keyboardType = .default
            }
            self.contentTextField = contentTextField
            
            if (model.contentType == "1") {
                self.contentTextField.isUserInteractionEnabled = true
                
                if (model.isDelect ?? false) {
                    self.delectButton = UIButton(type: .custom)
                    self.delectButton.setTitle("删除", for: .normal)
                    self.delectButton.setTitleColor(HexColor("#0091FF"), for: .normal)
                    self.delectButton.titleLabel?.font = Font_14
                    self.delectButton.titleLabel?.textAlignment = .right
                    self.delectButton.addTarget(self, action: #selector(selectDelectParamAction), for: .touchUpInside)
                    self.addSubview(self.delectButton)
                }
            }else if (model.contentType == "2") {
                self.contentTextField.isUserInteractionEnabled = false
                self.rightImageView = UIImageView()
                self.rightImageView.image = UIImage(named: "DS_shop_manageGoodsDetails_rightArrow")
                self.rightImageView.contentMode = .scaleAspectFit
                self.rightImageView.clipsToBounds = true
                self.addSubview(self.rightImageView)
                self.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(selfTap)))
            }else {
                self.contentTextField.isUserInteractionEnabled = false
                self.contentTextField.isHidden = true
                
                self.contentSwitch = UISwitch()
                self.contentSwitch.transform = CGAffineTransformMakeScale(0.7, 0.7)
                //不写到layoutSubviews中是因为 会变位置
                self.contentSwitch.top = (50-self.contentSwitch.height)*0.5
                self.contentSwitch.onTintColor = HexColor("#61C464")
                self.contentSwitch.tintColor = HexColor("#DBDEDB")
                self.contentSwitch.isOn = true
                self.contentSwitch.addTarget(self, action: #selector(switchValueChanged), for: .valueChanged)
                self.addSubview(self.contentSwitch)
            }
            self.addSubview(self.contentTextField)
            self.height = 50
        }else if (model.style == "2") {
            self.height = 50
        }else if (model.style == "3") {
            self.uploadImageView = CNLiveAddGoodsSelectUploadImageView(frame: CGRectZero)
//            self.uploadImageView.column = 3
//            self.uploadImageView.maxSelectImageCount = model.maxSelectImageCount
//            self.uploadImageView.isAbleSelectVideo = model.isAbleSelectVideo
//            self.uploadImageView.isDisplySelectCover = model.isAbleSelectCover
//            if model.id?.isEmpty {
//                self.uploadImageView.deleteImage = UIImage(named: "DS_shop_manageGoodsDetails_delete")
//            }
//            [self.uploadImageView setChangeFrameCallBack:^{
//                weakSelf.height = CGRectGetMaxY(weakSelf.uploadImageView.frame) + DS_adapt_length(15);
//                if (weakSelf.changeFrameCallBack) {
//                    weakSelf.changeFrameCallBack();
//                }
//            }];
            self.addSubview(self.uploadImageView)
        }else if (model.style == "4") {
            self.contentTextBgView = UIView()
//            self.contentTextBgView.fillColor = HexColor("F9F9F9")
//            self.contentTextBgView.radius = 8
            self.addSubview(self.contentTextBgView)
            
//            self.contentTextView = UITextView()
//            self.contentTextView.placeholder = model.placeholder
//            self.contentTextView.placeholderColor = HexColor("c6c6c6")
//            self.contentTextView.font = Font_14
//            self.contentTextView.textColor = HexColor("787878")
//            self.contentTextView.delegate = self
//            if (model.content?.count ?? 0 > 0){ self.contentTextView.text = model.content
//            }
//            self.contentTextBgView.addSubview(self.contentTextView)
            self.height = 120
        }else if (model.style == "5") {
            //添加输入框
            self.addBtn = UIButton(type: .custom)
            self.addBtn.setImage(UIImage(named: "DS_main_add_count"), for: .normal)
            self.addBtn.imageView?.contentMode = .scaleAspectFit
            self.addBtn.addTarget(self, action: #selector(addBtnClick), for: .touchUpInside)
            self.addSubview(self.addBtn)
            
            self.subtractBtn = UIButton(type: .custom)
            self.subtractBtn.setImage(UIImage(named: "DS_main_unadd_count_n"), for: .normal)
            self.subtractBtn.imageView?.contentMode = .scaleAspectFit
            self.subtractBtn.addTarget(self, action: #selector(subtractBtnClick), for: .touchUpInside)
            self.addSubview(self.subtractBtn)
                        
            self.countTF = UITextField()
            self.countTF.text = "1"
            self.countTF.textAlignment = .center
            self.countTF.font = Font_15
            self.countTF.borderStyle =  .none
            self.countTF.delegate = self
            self.countTF.layer.masksToBounds = true
            self.countTF.layer.cornerRadius = 5
            self.countTF.keyboardType = .numberPad
            self.countTF.backgroundColor = HexColor("#F1F1F1")
            self.countTF.addTarget(self, action: #selector(textDidChange(sender:)), for: .editingChanged)
            self.addSubview(self.countTF)
            
            self.descLab = UILabel()
            self.descLab.textColor = HexColor("#C4C4C6)")
            self.descLab.font = Font_10
            self.descLab.text = model.desc
            self.addSubview(self.descLab)
            
            self.height = 60
        }
        if (model.isDisplayLine ?? false) {
            self.line = UIImageView()
            self.line.backgroundColor = HexColor("#e6e6e6)")
            self.addSubview(self.line)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.titleLabel.frame = CGRect(x: 15, y: 15, width: 145, height: 20)
        if (self.addEditGoodsListModel!.desc?.count ?? 0 > 0 && self.addEditGoodsListModel!.style == "5") {
            self.descLab.frame = CGRect(x: 15, y: 35, width: 145, height: 20)
        }
        self.starImageView.frame = CGRect(x: self.titleLabel.left-3-5, y: 0, width: 5, height: 5)
        self.starImageView.centerY = self.titleLabel.centerY
        if (self.addEditGoodsListModel?.style == "2"){
            self.subButton.frame = CGRect(x: KSreenWidth-130, y: 15, width: 120, height: 20)
        }
        if (self.addEditGoodsListModel!.style == "1") {
            if (self.addEditGoodsListModel!.contentType == "1") {
                if (self.addEditGoodsListModel!.isDelect ?? false) {
                    self.delectButton.frame = CGRect(x: KSreenWidth-50, y: 0, width: 40, height: 50)
                    self.contentTextField.frame = CGRect(x: self.titleLabel.right+15, y: 0, width: self.width-self.titleLabel.right+15*2-40, height: 50)
                }else{
                    self.contentTextField.frame = CGRect(x: self.titleLabel.right+15, y: 0, width: self.width-self.titleLabel.right-15*2, height: 50)
                }
            }else if (self.addEditGoodsListModel!.contentType == "2") {
                let contentTextFieldMaxW = self.width - self.titleLabel.right-15*3-2
                self.contentTextField.sizeToFit()
                if (self.contentTextField.width > contentTextFieldMaxW) {
                    self.contentTextField.width = contentTextFieldMaxW
                }
                self.contentTextField.left = self.titleLabel.right + 15
                self.contentTextField.centerY = self.titleLabel.centerY
                self.rightImageView.frame = CGRect(x: self.contentTextField.right+2, y: 0, width: 15, height: 15)
                self.rightImageView.centerY = self.titleLabel.centerY
            }else {
                self.contentSwitch.left =  self.titleLabel.right+15
            }
            self.addSubview(self.contentTextField)
        }else if (self.addEditGoodsListModel!.style == "3") {
            self.uploadImageView.frame = CGRect(x: 0, y: self.titleLabel.bottom+2, width: self.width, height: 0)
            if (self.height == 0) {
                self.height = self.uploadImageView.bottom+15
                if ((self.updateFrameBlock) != nil) {
                    self.updateFrameBlock!()
                }
            }
        }else if (self.addEditGoodsListModel!.style == "4") {
            self.contentTextBgView.frame = CGRect(x: 15, y: self.titleLabel.bottom+10, width: self.width - 15*2, height: 60)
            self.contentTextView.frame = CGRect(x: 10, y: 10, width: self.contentTextBgView.width-10*2, height: self.contentTextBgView.height-10*2)
        }else if (self.addEditGoodsListModel!.style == "5") {
            self.subtractBtn.frame = CGRect(x: 15+self.titleLabel.right, y: 15, width: 30, height: 30)
            self.countTF.frame = CGRect(x: self.subtractBtn.right+2, y: self.subtractBtn.top+5, width: 55, height: 20)
            self.addBtn.frame = CGRect(x: self.countTF.right+2, y: self.subtractBtn.top, width: 30, height: 30)
        }
        self.line.frame = CGRect(x: 15, y: self.height-1, width: self.width-15*2, height: 1)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - BtnAction
    @objc func subBtnAction(){
        if ((self.updateFrameBlock) != nil) {
            self.updateFrameBlock!()
        }
    }
    @objc func selectDelectParamAction(){
        AlertTool.showSystemAlert(title: "确认删除?", message: nil, actionTitles: ["取消","确定"], style: .alert, actionStyles:[.cancel,.default] ) { index in
            if (index == 1) {
                if ((self.updateFrameBlock) != nil) {
                    self.updateFrameBlock!()
                }
            }
        }
    }
    @objc func selfTap(){
        
        if ((self.tapBlock) != nil) {
            self.tapBlock!(self)
        }
    }
    @objc func switchValueChanged(){
        
    }
    @objc func addBtnClick(){
        
    }
    @objc func subtractBtnClick(){
        
    }
    
    @objc func textDidChange(sender:UITextField) {
        if (sender == self.countTF) {
            if (self.countTF.text?.count ?? 0 > 0 &&  (self.countTF.text?.isPurnInt() == false)) {
                let count:Int = (self.countTF.text! as NSString).integerValue
                if (count <= 1) {
                    self.subtractBtn.setImage(UIImage(named: "DS_main_unadd_count_n"), for: .normal)
                }else{
                    self.subtractBtn.setImage(UIImage(named: "DS_main_unadd_count"), for: .normal)
                }
            }
        }
    }
    
}