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

import UIKit

class YHFormItemTitleCell: UITableViewCell {

    static let cellReuseIdentifier = "YHFormItemTitleCell"
    let editTag = 9527
    let cancelTag = 9528
    
    var rightClickBlock:((YHFormTitleItemEditType)->Void)?


    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hexString:"#333333")
        label.textAlignment = .left
        label.numberOfLines = 0
        label.font = UIFont.PFSCR(ofSize: 16)
        return label
    }()
    
    lazy var subTitleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hexString:"#888F98")
        label.textAlignment = .left
        label.numberOfLines = 0
        label.font = UIFont.PFSCR(ofSize: 14)
        return label
    }()
    
    lazy var cancelButton: UIButton = {
        
        let btn = UIButton()
        btn.setTitle("取消操作".local, for: .normal)
        btn.titleLabel?.font = UIFont.PFSCR(ofSize: 14)
        btn.setTitleColor(UIColor(hex: 0x4487F9), for: .normal)
        btn.tag = cancelTag
        btn.addTarget(self, action: #selector(didClickOperatorBtn(btn:)), for: .touchUpInside)
        btn.isHidden = true
        return btn
    }()
    
    lazy var editButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "form_edit"), for: .normal)
        btn.tag = editTag
        btn.addTarget(self, action: #selector(didClickOperatorBtn(btn:)), for: .touchUpInside)
        btn.isHidden = true
        return btn
    }()
    
    lazy var bottomLineView:UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: 0xF0F0F0)
        return view
    }()
    
    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(subTitleLabel)
        contentView.addSubview(bottomLineView)
        contentView.addSubview(editButton)
        contentView.addSubview(cancelButton)

        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16)
            make.right.equalTo(subTitleLabel.snp.left).offset(-3)
            make.centerY.equalToSuperview()
        }
        
        subTitleLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
        }
        
        editButton.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(24, 24))
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-18)
        }
        
        editButton.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(24, 24))
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-18)
        }
        
        cancelButton.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(92, 40))
            make.centerY.equalToSuperview()
            make.right.equalToSuperview()
        }
        
        bottomLineView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1.0)
        }
    }
    
    func showEditType(type: YHFormTitleItemEditType) {
        switch type {
            case .none:
                cancelButton.isHidden = true
                editButton.isHidden = true
            case .canEdit:
                cancelButton.isHidden = true
                editButton.isHidden = false
            case .canCancel:
                cancelButton.isHidden = false
                editButton.isHidden = true
        }
    }
    
    @objc func didClickOperatorBtn(btn:UIButton) {
        
        if let clickBlock = rightClickBlock {
            if btn.tag == editTag {
                clickBlock(.canEdit)
                
            } else if btn.tag == cancelTag {
                clickBlock(.canCancel)
            }
        }
    }
    
}
