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

import UIKit

class YHSingleLineInputCell: UITableViewCell {

    static let cellReuseIdentifier = "YHSingleLineInputCell"
    
    // 是否必填 如必填title会展示红色*
    var isMust = false
    
    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)
                ]
                var 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 textField:UITextField = {
        let textField = UITextField()
        textField.backgroundColor = .clear
        textField.placeholder = "请如实填写"
        textField.font = UIFont.PFSCR(ofSize: 14)
        textField.tintColor = UIColor(hex: 0xC0C0C0)
        textField.textColor = UIColor(hex: 0x222222)
//        textField.delegate = self
//        textField.addTarget(self, action: #selector(smsCodeInputChanged), for: .editingChanged)
        return textField
    }()
    
    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(textField)
        
        isMust = true
        title = "曾用名"
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16)
            make.right.equalTo(textField.snp.left).offset(-8)
            make.centerY.equalToSuperview()
        }
        
        textField.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(contentView.snp.right).offset(-16)
        }
    }

}
