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

import UIKit

class YHFormItemAddCell: UITableViewCell {

    static let cellReuseIdentifier = "YHFormItemAddCell"

    let btnTitleColor:UIColor = .brandMainColor
    let btnHeight = 44.0
    let cornerRadius = 4.0
    
    var clickBlock:(()->Void)?
    
    var title:String? {
        didSet {
            addBtn.setTitle(title, for: .normal)
        }
    }
    
    lazy var addBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("新增信息".local, for: .normal)
        btn.titleLabel?.font = UIFont.PFSC_M(ofSize: 14)
        btn.setTitleColor(btnTitleColor, for: .normal)
        btn.iconInLeft(spacing: 6)
        
        btn.setImage(UIImage(named: "form_add_info"), for: .normal)
        btn.addTarget(self, action: #selector(didClickAddBtn), for: .touchUpInside)
        return btn
    }()
    
    lazy var dotLineLayer:CAShapeLayer = {
        
        let borderLayer =  CAShapeLayer()
        borderLayer.position = CGPoint(x: addBtn.bounds.midX, y: addBtn.bounds.midY);
        borderLayer.path = UIBezierPath(roundedRect: borderLayer.bounds, cornerRadius: addBtn.layer.cornerRadius).cgPath
        borderLayer.lineWidth = 1
        //[数组第一个数字表示单个虚线长度，第二个数字表示间隔]
        borderLayer.lineDashPattern = [3,1]  as [NSNumber]?
        //前边是虚线的长度，后边是虚线之间空隙的长度
        borderLayer.lineDashPhase = 0.1
        //实线边框
        borderLayer.fillColor = UIColor.clear.cgColor
        borderLayer.strokeColor = btnTitleColor.cgColor
        return borderLayer
        
    }()
    
    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(addBtn)
        addBtn.layer.addSublayer(dotLineLayer)

        addBtn.snp.makeConstraints { make in
            make.left.top.equalToSuperview().offset(18)
            make.right.bottom.equalToSuperview().offset(-18)
            make.height.equalTo(btnHeight)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        print(addBtn.bounds)
        dotLineLayer.path = UIBezierPath(roundedRect: addBtn.bounds, cornerRadius: cornerRadius).cgPath
    
    }
    
    @objc func didClickAddBtn() {
     
        if let clickBlock = clickBlock {
            clickBlock()
        }
    }
}
