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

import UIKit

class YHResignAppointSelectTimeItemView: UIView {
    
    var titleMaxWidth = 112.0
    
    var clickBlock: (() -> Void)?
    
    var title: String = "" {
        didSet {
            titleLabel.text = title
        }
    }
    
    var placeHolder: String = ""
    
    var errorTips: String = "" {
        didSet {
            errorLabel.text = errorTips
        }
    }
    
    var isNeedShowErrorTips: Bool = false {
        didSet {
            errorLabel.isHidden = !isNeedShowErrorTips
            errorLabel.snp.remakeConstraints { make in
                make.top.equalTo(detailLabel.snp.bottom).offset(isNeedShowErrorTips ? 5 : 0)
                make.left.equalTo(titleLabel)
                make.right.equalTo(0)
                make.bottom.equalTo(-16)
                if !isNeedShowErrorTips {
                    make.height.equalTo(0)
                }
            }
            self.setNeedsLayout()
            self.layoutIfNeeded()
        }
    }
    
    func updateLineMargin(_ margin: CGFloat) {
        lineView.snp.updateConstraints { make in
            make.left.equalTo(margin)
            make.right.equalTo(-margin)
        }
        self.setNeedsLayout()
        self.layoutIfNeeded()
    }
    
    var detail: String = "" {
        didSet {
            
            if detail.isEmpty {
                // 显示placeHolder
                detailLabel.text = self.placeHolder
                detailLabel.textColor = .placeHolderColor
                detailLabel.textAlignment = .left
                return
            }
            detailLabel.text = detail
            detailLabel.textColor = .mainTextColor
            detailLabel.textAlignment = .left
        }
    }
    
//    var isShowDetailOneline:Bool = false {
//        didSet {
//            detailLabel.textAlignment = .left
//            detailLabel.numberOfLines = isShowDetailOneline ? 1 : 0
//            detailLabel.snp.remakeConstraints { make in
//                make.top.equalTo(16)
//                make.left.equalTo(titleLabel.snp.right).offset(0)
//                make.right.equalTo(arrowImgView.snp.left)
//                if isShowDetailOneline {
//                    make.height.equalTo(20)
//                }
//            }
//
//            errorLabel.snp.remakeConstraints { make in
//                make.top.equalTo(detailLabel.snp.bottom).offset(0)
//                make.left.equalTo(titleLabel)
//                make.right.equalTo(0)
//                make.bottom.equalTo(-16)
//            }
//
//            self.setNeedsLayout()
//            self.layoutIfNeeded()
//        }
//    }
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .separatorColor
        return view
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.font = UIFont.PFSC_R(ofSize: 14)
        label.textColor = .mainTextColor
        return label
    }()
    
    private lazy var detailLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.font = UIFont.PFSC_M(ofSize: 14)
        label.textColor = .mainTextColor
        return label
    }()
    
    private lazy var errorLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.font = UIFont.PFSC_R(ofSize: 12)
        label.textColor = .failColor
        return label
    }()
    
    lazy var arrowImgView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "family_info_arrow"))
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        createUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
     
    @objc func didSelectView() {
        clickBlock?()
    }
    
    func createUI() {
        self.addSubview(titleLabel)
        self.addSubview(detailLabel)
        self.addSubview(arrowImgView)
        self.addSubview(lineView)
        self.addSubview(errorLabel)
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(didSelectView))
        self.addGestureRecognizer(tap)

        lineView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(0)
            make.height.equalTo(0.5)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.top.equalTo(16)
            make.width.equalTo(titleMaxWidth)
        }
        
        detailLabel.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.left.equalTo(titleLabel.snp.right).offset(28.0)
            make.right.equalTo(arrowImgView.snp.left)
        }
        
        errorLabel.snp.makeConstraints { make in
            make.top.equalTo(detailLabel.snp.bottom).offset(0)
            make.left.equalTo(titleLabel)
            make.right.equalTo(0)
            make.bottom.equalTo(-16)
        }
        
        arrowImgView.snp.makeConstraints { make in
            make.width.height.equalTo(22)
            make.top.equalTo(15)
            make.right.equalTo(0)
        }
    }

}
