CNLiveShopBodyCustomView.swift 11.1 KB
//
//  CNLiveShopBodyCustomView.swift
//  metaCode
//
//  Created by open on 2023/11/6.
//

import Foundation
import SnapKit
import Kingfisher

typealias clickAgreeCallBack = (_ refundView: ShopBodyOrderRefundView,_ desc: String) -> Void
class ShopBodyOrderRefundView: UIView, UITextViewDelegate {
    
    var clickBlock: clickAgreeCallBack!
    
    lazy var shadowView: UIView = {
        let shadowView = UIView.init()
        shadowView.backgroundColor = UIColor(white: 0, alpha: 0.38)
        return shadowView
    }()
    
    lazy var alertView: UIView = {
        let alertView = UIView.init()
        alertView.backgroundColor = .white
        alertView.layer.cornerRadius = adapt_length(length: 4)
        alertView.layer.masksToBounds = true
        return alertView
    }()
    
    lazy var titleLab: UILabel = {
        let titleLab = UILabel.init()
        titleLab.font = medium_adapt_font(pointSize: 17)
        titleLab.textColor = HexColor("#444444")
        titleLab.textAlignment = .center
        titleLab.text = "拒绝原因"
        return titleLab
    }()
    
    lazy var contentBgView: UIView = {
        let contentBgView = UIView.init()
        contentBgView.backgroundColor = HexColor("#F5F6F8")
        contentBgView.layer.cornerRadius = adapt_length(length: 4)
        contentBgView.layer.masksToBounds = true
        return contentBgView
    }()
    
    lazy var contentTextView: UITextView = {
        let contentTextView = UITextView.init(frame: CGRect.zero)
        contentTextView.text = "拒单原因,最多不超过100字…"
        contentTextView.backgroundColor = HexColor("#F5F6F8")
        contentTextView.textColor = UIColor.lightGray
        contentTextView.font = regular_adapt_font(pointSize: 14)
        contentTextView.delegate = self
        contentTextView.returnKeyType = .done
        contentTextView.showsVerticalScrollIndicator = false
        return contentTextView
    }()
    
    lazy var cancelBtn: UIButton = {
        let cancelBtn = UIButton.init(type: .custom)
        cancelBtn.setTitle("取消", for: .normal)
        cancelBtn.setTitleColor(HexColor("#ffffff"), for: .normal)
        cancelBtn.titleLabel?.font = medium_adapt_font(pointSize: 14)
        cancelBtn.layer.cornerRadius = adapt_length(length: 4)
        cancelBtn.layer.masksToBounds = true
        cancelBtn.backgroundColor = HexColor("#F5A623")
        cancelBtn.addTarget(self, action: #selector(cancelBtnClick), for: .touchUpInside)
        return cancelBtn
    }()
    
    lazy var agreeBtn: UIButton = {
        let agreeBtn = UIButton.init(type: .custom)
        agreeBtn.setTitle("确认拒绝", for: .normal)
        agreeBtn.setTitleColor(HexColor("#ffffff"), for: .normal)
        agreeBtn.titleLabel?.font = medium_adapt_font(pointSize: 14)
        agreeBtn.layer.cornerRadius = adapt_length(length: 4)
        agreeBtn.layer.masksToBounds = true
        agreeBtn.backgroundColor = HexColor("#28C1C6")
        agreeBtn.addTarget(self, action: #selector(agreeBtnClick), for: .touchUpInside)
        return agreeBtn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(shadowView)
        shadowView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        addSubview(alertView)
        alertView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(adapt_length(length: 30))
            make.right.equalToSuperview().offset(-adapt_length(length: 30))
            make.center.equalToSuperview()
            make.height.equalTo(KSreenWidth-adapt_length(length: 60))
        }
        alertView.addSubview(titleLab)
        titleLab.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(adapt_length(length: 20))
            make.top.equalToSuperview().offset(adapt_length(length: 20))
            make.right.equalToSuperview().offset(-adapt_length(length: 20))
        }
        alertView.addSubview(contentBgView)
        contentBgView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(adapt_length(length: 25))
            make.top.equalTo(titleLab.snp.bottom).offset(adapt_length(length: 15))
            make.right.equalToSuperview().offset(-adapt_length(length: 25))
            make.bottom.equalToSuperview().offset(-adapt_length(length: 60))
        }
        contentBgView.addSubview(contentTextView)
        contentTextView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(adapt_length(length: 15))
            make.top.equalToSuperview().offset(adapt_length(length: 12))
            make.right.equalToSuperview().offset(-adapt_length(length: 15))
            make.bottom.equalToSuperview().offset(-adapt_length(length: 12))
        }
        alertView.addSubview(cancelBtn)
        cancelBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset((KSreenWidth-adapt_length(length: 228))/3)
            make.bottom.equalToSuperview().offset(-adapt_length(length: 15))
            make.width.equalTo(adapt_length(length: 84))
            make.height.equalTo(adapt_length(length: 30))
        }
        alertView.addSubview(agreeBtn)
        agreeBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-((KSreenWidth-adapt_length(length: 228))/3))
            make.bottom.equalToSuperview().offset(-adapt_length(length: 15))
            make.width.equalTo(adapt_length(length: 84))
            make.height.equalTo(adapt_length(length: 30))
        }
    }
    
    @objc func agreeBtnClick() {
        if (clickBlock != nil) {
            self.clickBlock(self, self.contentTextView.text)
        }
    }
    
    func textViewDidBeginEditing(_ textView: UITextView) {
        if textView.textColor == UIColor.lightGray {
            textView.text = nil
            textView.textColor = HexColor("#444444")
        }
    }
    
    func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
        if textView.text.isEmpty {
            textView.text = "拒单原因,最多不超过100字…"
            textView.textColor = UIColor.lightGray
        } 
        return true
    }
    
    func textViewDidChange(_ textView: UITextView) {
        if self.contentTextView == textView && self.contentTextView.text.count > 100 {
            showMessage(message: "最多不超过100字!!!")
        }
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        if self.contentTextView == textView && self.contentTextView.text.count > 100 {
            self.contentTextView.text = self.contentTextView.text.prefix(100).lowercased()
        }
    }
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text == "\n" {
            self.endEditing(true)
            return false
        }
        return true
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        guard let contentTextViewRect = self.contentTextView.superview?.convert(self.contentTextView.frame, to: self) else { return self.contentTextView }
        
        if !CGRectContainsPoint(contentTextViewRect, point) {
            self.endEditing(true)
        }
        return super.hitTest(point, with: event)
    }
    
    @objc func cancelBtnClick() {
        removeFromSuperview()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


class ShopBodyOrderSpliteTableViewCell: UITableViewCell {
    
    
    lazy var goodsImageView: UIImageView = {
        let goodsImageView = UIImageView.init()
        goodsImageView.backgroundColor = .white
        goodsImageView.contentMode = .scaleAspectFill
        goodsImageView.layer.masksToBounds = true
        return goodsImageView
    }()
    
    lazy var goodsTitle: UILabel = {
        let goodsTitle = UILabel.init()
        goodsTitle.lineBreakMode = .byClipping
        goodsTitle.numberOfLines = 2
        goodsTitle.textColor = HexColor("#333333")
        goodsTitle.font = regular_adapt_font(pointSize: 14)
        return goodsTitle
    }()
    
    lazy var priceLabel: UILabel = {
        let priceLabel = UILabel.init()
        priceLabel.textColor = HexColor("#FC1541")
        priceLabel.font = medium_adapt_font(pointSize: 21)
        return priceLabel
    }()
    
    lazy var numberLabel: UILabel = {
        let numberLabel = UILabel.init()
        numberLabel.textAlignment = .right
        numberLabel.textColor = HexColor("#222833")
        numberLabel.font = regular_adapt_font(pointSize: 15)
        return numberLabel
    }()
    
    lazy var lineLabel: UILabel = {
        let lineLabel = UILabel.init()
        lineLabel.backgroundColor = HexColor("#EAEAEA")
        return lineLabel
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.backgroundColor = .clear
        selectionStyle = .none
        
        
        addSubview(goodsImageView)
        goodsImageView.snp.makeConstraints { make in
            make.left.top.equalToSuperview().offset(adapt_length(length: 15))
            make.size.equalTo(adapt_length(length: 100))
        }
        addSubview(goodsTitle)
        goodsTitle.snp.makeConstraints { make in
            make.top.equalTo(goodsImageView)
            make.left.equalTo(goodsImageView.snp.right).offset(adapt_length(length: 15))
            make.right.equalToSuperview().offset(-adapt_length(length: 15))
        }
        addSubview(priceLabel)
        priceLabel.snp.makeConstraints { make in
            make.left.equalTo(goodsTitle)
            make.bottom.equalTo(goodsImageView)
        }
        addSubview(numberLabel)
        numberLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-adapt_length(length: 10))
            make.bottom.equalTo(goodsImageView)
        }
        addSubview(lineLabel)
        lineLabel.snp.makeConstraints { make in
            make.left.equalTo(goodsImageView)
            make.right.equalTo(numberLabel)
            make.bottom.equalToSuperview().offset(-adapt_length(length: 0.5))
            make.height.equalTo(adapt_length(length: 0.5))
        }
    }
    
    func setupDataSource(goodsModel: ShopOrderListGoodsModel) {
        goodsImageView.kf.setImage(with: URL(string: goodsModel.simg!), placeholder: UIImage.imageWithColor(color: HexColor("#F8F8F8")!, size: CGSize(width: adapt_length(length: 100), height: adapt_length(length: 100))))
        goodsTitle.text = goodsModel.title
        priceLabel.text = \(goodsModel.price ?? 0.00)"
        priceLabel.sizeToFit()
        numberLabel.text = "x \(goodsModel.num ?? 0)"
        numberLabel.sizeToFit()
        setNeedsLayout()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


class ShopBodyOrderSelectTableViewCell: UITableViewCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}