//
//  YHSingleLineSelectCell.swift
//  galaxy
//
//  Created by edy on 2024/1/22.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHSingleLineSelectCell: UITableViewCell {

    static let cellReuseIdentifier = "YHSingleLineSelectCell"
    
    // 是否必填 如必填title会展示红色*
    var isMust = false
    var deleteBlock:(()->Void)?
    var isShowDeleteBtn:Bool = false {
        didSet {
            deleteButton.isHidden = !isShowDeleteBtn
            detailLabel.isHidden = isShowDeleteBtn
            arrowImgView.isHidden = isShowDeleteBtn
        }
    }

    var title:String? {
        didSet {
            
            if let title = title {
                let str = (isMust ? ("*"+title) : title)
                let attributes: [NSAttributedString.Key: Any] = [
                    .font: UIFont.PFSCR(ofSize: 14),
                    .foregroundColor: UIColor(hex:0x222222)
                ]
                let questionAttrStr = NSMutableAttributedString(string: str, attributes: attributes)
                if isMust {
                    let starRange = NSRange(location: 0, length: 1)
                    questionAttrStr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor(hex:0xFF3A3A), range: starRange)
                }
                titleLabel.attributedText = questionAttrStr
            }
        }
    }

    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hexString:"#333333")
        label.textAlignment = .left
        label.numberOfLines = 0
        label.font = UIFont.PFSCR(ofSize: 14)
        return label
    }()
    
    lazy var detailLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hexString:"#333333")
        label.textAlignment = .right
        label.numberOfLines = 0
        label.font = UIFont.PFSCR(ofSize: 14)
        return label
    }()
    
    lazy var arrowImgView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "form_right_arrow"))
        return imgView
    }()
    
    lazy var deleteButton: UIButton = {
        let btn = UIButton.bs_button(title: "删除".local, font: UIFont.PFSCR(ofSize: 14), normalColor: UIColor(hex: 0xF81D22))
        btn.addTarget(self, action: #selector(didClickDeleteBtn(btn:)), for: .touchUpInside)
        btn.isHidden = true
        return btn
    }()
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    func setupUI() {
        
        self.selectionStyle = .none
        contentView.addSubview(titleLabel)
        contentView.addSubview(detailLabel)
        contentView.addSubview(arrowImgView)
        contentView.addSubview(deleteButton)

        title = "婚姻状况"
        detailLabel.text = "沙发上发顺丰"
        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16)
            make.right.equalTo(detailLabel.snp.left).offset(-8)
            make.centerY.equalToSuperview()
        }
        
        detailLabel.snp.makeConstraints { make in
            make.right.equalTo(arrowImgView.snp.left)
            make.centerY.equalToSuperview()
        }
        
        arrowImgView.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(20, 20))
            make.right.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
        }
        
        deleteButton.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(64, 40))
            make.centerY.equalToSuperview()
            make.right.equalToSuperview()
        }
    }
    
    @objc func didClickDeleteBtn(btn:UIButton) {
        
        if let deleteBlock = deleteBlock {
            deleteBlock()
        }
    }
}
