//
//  YHCertificateSearchBar.swift
//  galaxy
//
//  Created by edy on 2024/3/13.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHCertificateSearchBar: UIView {

    static let height = 40.0
    static let cancelBtnHeight = height-8.0
    
    var textChange:((String?)->Void)?
    var cancelBlock:(()->Void)?
    var placeHolder:String? {
        didSet {
            if let placeholder = placeHolder {
                textField.attributedPlaceholder = NSAttributedString(string: placeholder, attributes: [NSAttributedString.Key.foregroundColor : UIColor.placeHolderColor])
            }
        }
    }
    private lazy var contentView = {
        let view = UIView()
        view.backgroundColor = .contentBkgColor
        view.layer.cornerRadius = Self.height/2.0
        view.clipsToBounds = true
        return view
    }()
    
    lazy var textField:UITextField = {
        let textField = UITextField()
        textField.backgroundColor = .clear
        textField.attributedPlaceholder = NSAttributedString(string: "请输入", attributes: [NSAttributedString.Key.foregroundColor : UIColor.placeHolderColor])
        textField.font = UIFont.PFSC_M(ofSize: 12)
        textField.tintColor = UIColor(hexString: "#3D88F8")
        textField.textColor = UIColor.mainTextColor
        textField.clearButtonMode = .whileEditing
        textField.addTarget(self, action: #selector(textFieldChanged(textField:)), for: .editingChanged)
        return textField
    }()
    
    lazy var cancelBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("取消".local, for: .normal)
        btn.titleLabel?.font = UIFont.PFSC_M(ofSize: 12)
        btn.setTitleColor(.white, for: .normal)
        btn.backgroundColor = UIColor(hexString: "#3D88F8")
        btn.layer.cornerRadius = Self.cancelBtnHeight/2.0
        btn.addTarget(self, action: #selector(cancelBtnClicked), for: .touchUpInside)
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.createUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    static func createBar() -> YHCertificateSearchBar {
        let view = YHCertificateSearchBar(frame: CGRect(x: 0, y: 0, width: KScreenWidth-32, height: height))
        return view
    }
    
    @objc func cancelBtnClicked() {
        if let cancelBlock = cancelBlock {
            cancelBlock()
        }
    }
    
    func createUI() {
        
        self.backgroundColor = .white
        
        contentView.addSubview(textField)
        contentView.addSubview(cancelBtn)
        self.addSubview(contentView)
        
        contentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        textField.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(Self.height/2.0)
            make.right.equalTo(cancelBtn.snp.left).offset(-4)
            make.centerY.equalToSuperview()
        }
        
        cancelBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 66, height: Self.height-8.0))
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-4)
        }
    }
    
    @objc func textFieldChanged(textField:UITextField) {
        if let textChange = textChange {
            textChange(textField.text)
        }
    }
}
